Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
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
92 changes: 92 additions & 0 deletions 92 src/Symfony/Component/Messenger/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Symfony\Component\Messenger\Stamp\SentStamp;
use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageInterface;
use Symfony\Component\Messenger\Tests\Fixtures\DummyReceiver;
use Symfony\Component\Messenger\Tests\Fixtures\ResettableDummyReceiver;
use Symfony\Component\Messenger\Transport\Receiver\QueueReceiverInterface;
Expand Down Expand Up @@ -581,6 +582,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 @@ -634,3 +681,48 @@ private function process(array $jobs): void
}
}
}

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
{
$this->processedMessages = array_column($jobs, 0);

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

class SecondHandlerDummyMessage implements DummyMessageInterface
{
private string $message;

public function __construct(string $message)
{
$this->message = $message;
}

public function getMessage(): string
{
return $this->message;
}
}

class SecondMessageDummyReceiver extends DummyReceiver
{
}
6 changes: 4 additions & 2 deletions 6 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.