Open
Description
Symfony version(s) affected
Description
SplObjectStorage should not be iterated with foreach if unset is called inside the loop.
https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/Messenger/Worker.php#L273-L282
If I run the following code with two different messages handled by two batch handlers, the exception 'The acknowledger was not called in ...' is thrown.
$eventDispatcher->addListener(WorkerRunningEvent::class, function workerRunning(WorkerRunningEvent $event): void {
if ($event->isWorkerIdle()) {
$event->getWorker()->stop();
}
});
$worker = new Worker($receivers, $this->routableBus, $this->eventDispatcher, $this->logger, $rateLimiters);
$options = ['sleep' => 0];
$worker->run($options);
Probably related to #54600 ?
How to reproduce
$storage = new SplObjectStorage();
$first = new stdClass();
$first->name = 'first';
$storage->attach($first);
$second = new stdClass();
$second->name = 'second';
$storage->attach($second);
foreach ($storage as $item) { // prints "first"
echo $item->name . PHP_EOL;
unset($storage[$item]);
}
echo sprintf('Count: %d' . PHP_EOL, $storage->count()); // prints "Count: 1"
Possible Solution
No response
Additional Context
No response