8000 [Messenger] Second batch handler worker returns "The acknowledger was not called by the ... batch handler." by ehoutsma · Pull Request #58433 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Second batch handler worker returns "The acknowledger was not called by the ... batch handler." #58433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 6.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
(fix) Handling of batch messages when there are two batch handlers wi…
…th unacked messages.
  • Loading branch information
Erwin Houtsma committed Oct 2, 2024
commit e1e290c6f5eb4244365839cc8cd35a78d4b11a6a
82 changes: 81 additions & 1 deletion src/Symfony/Component/Messenger/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,52 @@ public function testFlushBatchOnStop()
$this->assertSame($expectedMessages, $handler->processedMessages);
}

public function testFlushMultipleBatchOnStop()
{
$expectedMessages = [
new DummyMessage('Hey'),
];

$secondHandlerExpectedMessages = [
new SecondHandlerDummyMessage('Ho'),
];

$receiver = new DummyReceiver([
[new Envelope($expectedMessages[0])],
]);

$secondHandlerReceiver = new SecondMessageDummyReceiver([
[new Envelope($secondHandlerExpectedMessages[0])],
]);

$handler = new DummyBatchHandler();
$secondHandler = new SecondDummyBatchHandler();

$middleware = new HandleMessageMiddleware(new HandlersLocator([
DummyMessage::class => [new HandlerDescriptor($handler)],
SecondHandlerDummyMessage::class => [new HandlerDescriptor($secondHandler)],
]));

$bus = new MessageBus([$middleware]);

$dispatcher = new EventDispatcher();
$dispatcher->addListener(WorkerRunningEvent::class, function (WorkerRunningEvent $event) use ($receiver, $secondHandlerReceiver) {
static $i = 0;
if (1 < ++$i) {
$event->getWorker()->stop();
}

$this->assertSame(0, $receiver->getAcknowledgeCount());
$this->assertSame(0, $secondHandlerReceiver->getAcknowledgeCount());
});

$worker = new Worker([$receiver, $secondHandlerReceiver], $bus, $dispatcher, clock: new MockClock());
$worker->run();

$this->assertSame($expectedMessages, $handler->processedMessages);
$this->assertSame($secondHandlerExpectedMessages, $secondHandler->processedMessages);
}

public function testGcCollectCyclesIsCalledOnMessageHandle()
{
$apiMessage = new DummyMessage('API');
Expand Down Expand Up @@ -622,7 +668,33 @@ public function __invoke(DummyMessage $message, ?Acknowledger $ack = null)

private function shouldFlush(): bool
{
return 2 <= \count($this->jobs);
return 5 <= \count($this->jobs);
}

private function process(array $jobs): void
{
$this->processedMessages = array_column($jobs, 0);

foreach ($jobs as [$job, $ack]) {
$ack->ack($job);
}
}
}

Class SecondDummyBatchHandler implements BatchHandlerInterface
{
use BatchHandlerTrait;

public array $processedMessages;

public function __invoke(SecondHandlerDummyMessage $message, ?Acknowledger $ack = null)
{
return $this->handle($message, $ack);
}

private function shouldFlush(): bool
{
return 5 <= \count($this->jobs);
}

private function process(array $jobs): void
Expand All @@ -634,3 +706,11 @@ private function process(array $jobs): void
}
}
}

class SecondHandlerDummyMessage extends DummyMessage
{
}

class SecondMessageDummyReceiver extends DummyReceiver
{
}
6 changes: 4 additions & 2 deletions src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,17 @@ private function flush(bool $force): bool

$this->unacks = new \SplObjectStorage();

foreach ($unacks as $batchHandler) {
while ($unacks->valid()) {
$batchHandler = $unacks->current();
[$envelope, $transportName] = $unacks[$batchHandler];
try {
$this->bus->dispatch($envelope->with(new FlushBatchHandlersStamp($force)));
$envelope = $envelope->withoutAll(NoAutoAckStamp::class);
unset($unacks[$batchHandler], $batchHandler);
} catch (\Throwable $e) {
$this->acks[] = [$transportName, $envelope, $e];
}
$unacks->next();
$unacks->detach($batchHandler);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that we now don't call the destructor of the $batchHandler (if any) inside the try/catch.
Is that the fix or a regression?

Copy link
Author
@ehoutsma ehoutsma Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review.

The fix is that we unset the batchHandler after we set the iterator to the next batchHandler. Before it would unset the current batchHandler, and then tries to proceed to the next batchHandler but can't find the current index because it was unset.

See this comment: https://www.php.net/manual/en/splobjectstorage.detach.php#97644

Catching a Throwable of the unset, has nothing to do with the handling of the batch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas Do you have enough information to accept this PR or do you need more information. It would help us a lot if this bug is fixed.

}

return $this->ack();
Expand Down
0