Skip to content

Commit fc4ee0b

Browse files
committed
[0.6.x] Handle channel closing mid handling a message
Found this while working on #193
1 parent 307e781 commit fc4ee0b

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Channel.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,15 @@ private function deliveryTick(string $consumerTag): void
636636

637637
$outcome = $callback($message, $this, $this->client);
638638

639+
/**
640+
* Handle channel close getting called while handling a message.
641+
* Also not handling more messages when the channel is closing or when it errors.
642+
* Those put the channel in a state we can't consume any more messages.
643+
*/
644+
if ($this->state === ChannelState::Closing || $this->state === ChannelState::Closed || $this->state === ChannelState::Error) {
645+
return;
646+
}
647+
639648
if (!($outcome instanceof PromiseInterface)) {
640649
$this->consumeConcurrent[$consumerTag]--;
641650
$this->deliveryTick($consumerTag);

test/ChannelTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,27 @@ public function testBigMessage(): void
242242
$c->disconnect();
243243
self::assertFalse($c->isConnected());
244244
}
245+
246+
public function testChannelClosingMidMessageHandling(): void
247+
{
248+
/**
249+
* @var Deferred<bool> $deferred
250+
*/
251+
$deferred = new Deferred();
252+
$body = 'abc';
253+
254+
$c = $this->helper->createClient();
255+
256+
$ch = $c->connect()->channel();
257+
$ch->queueDeclare('test_queue', false, false, false, true);
258+
$ch->consume(static function (Message $msg, Channel $ch, Client $c) use ($body, $deferred): void {
259+
self::assertEquals($body, $msg->content);
260+
$deferred->resolve(true);
261+
$c->disconnect();
262+
});
263+
$ch->publish($body, [], '', 'test_queue');
264+
self::assertTrue(await($deferred->promise()));
265+
266+
self::assertFalse($c->isConnected());
267+
}
245268
}

0 commit comments

Comments
 (0)