Skip to content

[Messenger] ensure exception on rollback does not hide previous exception #59103

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

Merged
merged 1 commit into from
Dec 18, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,30 @@ class DoctrineTransactionMiddleware extends AbstractDoctrineMiddleware
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
{
$entityManager->getConnection()->beginTransaction();

$success = false;
try {
$envelope = $stack->next()->handle($envelope, $stack);
$entityManager->flush();
$entityManager->getConnection()->commit();

$success = true;

return $envelope;
} catch (\Throwable $exception) {
$entityManager->getConnection()->rollBack();

if ($exception instanceof HandlerFailedException) {
// Remove all HandledStamp from the envelope so the retry will execute all handlers again.
// When a handler fails, the queries of allegedly successful previous handlers just got rolled back.
throw new HandlerFailedException($exception->getEnvelope()->withoutAll(HandledStamp::class), $exception->getWrappedExceptions());
}

throw $exception;
} finally {
$connection = $entityManager->getConnection();

if (!$success && $connection->isTransactionActive()) {
$connection->rollBack();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,37 @@ public function testMiddlewareWrapsInTransactionAndFlushes()

public function testTransactionIsRolledBackOnException()
{
$this->connection->expects($this->once())
->method('beginTransaction')
;
$this->connection->expects($this->once())
->method('rollBack')
;
$this->connection->expects($this->once())->method('beginTransaction');
$this->connection->expects($this->once())->method('isTransactionActive')->willReturn(true);
$this->connection->expects($this->once())->method('rollBack');

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Thrown from next middleware.');

$this->middleware->handle(new Envelope(new \stdClass()), $this->getThrowingStackMock());
}

public function testExceptionInRollBackDoesNotHidePreviousException()
{
$this->connection->expects($this->once())->method('beginTransaction');
$this->connection->expects($this->once())->method('isTransactionActive')->willReturn(true);
$this->connection->expects($this->once())->method('rollBack')->willThrowException(new \RuntimeException('Thrown from rollBack.'));

try {
$this->middleware->handle(new Envelope(new \stdClass()), $this->getThrowingStackMock());
} catch (\Throwable $exception) {
}

self::assertNotNull($exception);
self::assertInstanceOf(\RuntimeException::class, $exception);
self::assertSame('Thrown from rollBack.', $exception->getMessage());

$previous = $exception->getPrevious();
self::assertNotNull($previous);
self::assertInstanceOf(\RuntimeException::class, $previous);
self::assertSame('Thrown from next middleware.', $previous->getMessage());
}

public function testInvalidEntityManagerThrowsException()
{
$managerRegistry = $this->createMock(ManagerRegistry::class);
Expand Down
Loading