8000 bug #32568 [Messenger] Fix UnrecoverableExceptionInterface handling (… · symfony/symfony@e44eb91 · GitHub
[go: up one dir, main page]

Skip to content

Commit e44eb91

Browse files
committed
bug #32568 [Messenger] Fix UnrecoverableExceptionInterface handling (LanaiGrunt)
This PR was merged into the 4.3 branch. Discussion ---------- [Messenger] Fix UnrecoverableExceptionInterface handling | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #32325 | License | MIT | Doc PR | no Fixed the handling of UnrecoverableExceptionInterface-Exceptions like suggested in [the issue thread](#32325 (comment)). Commits ------- 49bb743 [Messenger] fixed UnrecoverableExceptionInterface handling in Worker (fixes #32325)
2 parents 3f98846 + 49bb743 commit e44eb91

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Symfony/Component/Messenger/Tests/WorkerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
1818
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
1919
use Symfony\Component\Messenger\Event\WorkerStoppedEvent;
20+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
2021
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
2122
use Symfony\Component\Messenger\MessageBusInterface;
2223
use Symfony\Component\Messenger\Retry\RetryStrategyInterface;
@@ -120,6 +121,37 @@ public function testDispatchCausesRetry()
120121
$this->assertSame(1, $receiver->getAcknowledgeCount());
121122
}
122123

124+
public function testUnrecoverableMessageHandlingExceptionPreventsRetries()
125+
{
126+
$envelope1 = new Envelope(new DummyMessage('Unwrapped Exception'), [new SentStamp('Some\Sender', 'transport1')]);
127+
$envelope2 = new Envelope(new DummyMessage('Wrapped Exception'), [new SentStamp('Some\Sender', 'transport1')]);
128+
129+
$receiver = new DummyReceiver([
130+
[$envelope1],
131+
[$envelope2],
132+
]);
133+
134+
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
135+
$bus->expects($this->at(0))->method('dispatch')->willThrowException(new UnrecoverableMessageHandlingException());
136+
$bus->expects($this->at(1))->method('dispatch')->willThrowException(
137+
new HandlerFailedException($envelope2, [new UnrecoverableMessageHandlingException()])
138+
);
139+
140+
$retryStrategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock();
141+
$retryStrategy->expects($this->never())->method('isRetryable')->willReturn(true);
142+
143+
$worker = new Worker(['transport1' => $receiver], $bus, ['transport1' => $retryStrategy]);
144+
$worker->run([], function (?Envelope $envelope) use ($worker) {
145+
// stop after the messages finish
146+
if (null === $envelope) {
147+
$worker->stop();
148+
}
149+
});
150+
151+
// message was rejected
152+
$this->assertSame(2, $receiver->getRejectCount());
153+
}
154+
123155
public function testDispatchCausesRejectWhenNoRetry()
124156
{
125157
$receiver = new DummyReceiver([

src/Symfony/Component/Messenger/Worker.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,20 @@ private function dispatchEvent($event)
191191

192192
private function shouldRetry(\Throwable $e, Envelope $envelope, RetryStrategyInterface $retryStrategy): bool
193193
{
194+
// if ALL nested Exceptions are an instance of UnrecoverableExceptionInterface we should not retry
195+
if ($e instanceof HandlerFailedException) {
196+
$shouldNotRetry = true;
197+
foreach ($e->getNestedExceptions() as $nestedException) {
198+
if (!$nestedException instanceof UnrecoverableExceptionInterface) {
199+
$shouldNotRetry = false;
200+
break;
201+
}
202+
}
203+
if ($shouldNotRetry) {
204+
return false;
205+
}
206+
}
207+
194208
if ($e instanceof UnrecoverableExceptionInterface) {
195209
return false;
196210
}

0 commit comments

Comments
 (0)
0