Issue Bug Resolved

#111029Messenger worker: second message fails with "MySQL server has gone away" because BackendUtility keeps a prepared statement of the closed connection in the runtime cache

Georg Ringer 10/12/26, 19:29 updated 11/14/26, 09:25

Description

Problem

With #110227 the worker closes all open ConnectionPool connections after every
message (DoctrineCloseConnectionMiddleware). BackendUtility::getPageForRootline()
(used by BackendUtility::BEgetRootLine()) puts a prepared statement into the
@runtime@ cache and reuses it for the whole PHP process:

$statement = $runtimeCache->get('getPageForRootlineStatement-' . $statementCacheIdent);
...
$runtimeCache->set('getPageForRootlineStatement-' . $statementCacheIdent, $statement);
...
$statement->executeQuery();

That statement still belongs to the connection the middleware closed after the first
message. The next message handled by the same worker that calls BEgetRootLine()
therefore fails:

Doctrine\DBAL\Exception\ConnectionLost: An exception occurred while executing a query: MySQL server has gone away
#4 typo3/sysext/backend/Classes/Utility/BackendUtility.php(427): Doctrine\DBAL\Statement->executeQuery()
#5 typo3/sysext/backend/Classes/Utility/BackendUtility.php(312): TYPO3\CMS\Backend\Utility\BackendUtility::getPageForRootline()
...
#13 typo3/sysext/core/Classes/Messenger/Middleware/DoctrinePingConnectionMiddleware.php(77): ...HandleMessageMiddleware->handle()
#14 typo3/sysext/core/Classes/Messenger/Middleware/DoctrineCloseConnectionMiddleware.php(59): ...DoctrinePingConnectionMiddleware->handle()

The ping middleware does not help, it only pings connections, not statements cached
elsewhere.

The worker output is misleading: with the default retry strategy the failing
messages are retried and then dropped, messenger:consume -vv prints nothing for
them, and the queue ends up empty although most messages were not processed.

Steps to reproduce

  1. Fresh main (15.0.0-dev) with MySQL/MariaDB, the message is routed to the doctrine transport.
  2. Add a message handler that calls the rootline:
#[AsMessageHandler]
final class RootlineHandler
{
    public function __invoke(RootlineMessage $message): void
    {
        BackendUtility::BEgetRootLine(1);
    }
}
  1. Dispatch two (or more) RootlineMessage messages.
  2. Run bin/typo3 messenger:consume doctrine -vvv --limit=2.

Actual: the first message is handled, the second one throws ConnectionLost and is
retried and finally dropped.

Expected: every message is handled.

(Found in a real extension that checks 25 records per message and calls
@BEgetRootLine()@ for every record: only the first message per worker process had any effect.)

Cause

DoctrineCloseConnectionMiddleware closes the connections but leaves the runtime
cache alone. The runtime cache is per process state and is expected to live for one
request, a worker lives for many messages. Everything in it that is bound to the closed
connection (here a prepared Statement) is broken afterwards, everything else in it
(records, rootlines) is stale for the next message as well.

Suggested fix

Flush the runtime cache together with closing the connections:

--- a/typo3/sysext/core/Classes/Messenger/Middleware/DoctrineCloseConnectionMiddleware.php
+++ b/typo3/sysext/core/Classes/Messenger/Middleware/DoctrineCloseConnectionMiddleware.php
@@
 final readonly class DoctrineCloseConnectionMiddleware implements MiddlewareInterface
 {
     public function __construct(
         private ConnectionPool $connectionPool,
         private EventDispatcherInterface $eventDispatcher,
+        private CacheManager $cacheManager,
     ) {}
@@
         } finally {
+            // The runtime cache holds prepared statements of the connections closed below
+            // (e.g. BackendUtility::getPageForRootline()) and is stale for the next message anyway
+            $this->cacheManager->getCache('runtime')->flush();
             // Resolved after handling, so connections opened while handling the

Not tested as a core patch. The same flush at the start of the handler fixes it for the
extension (tested with 25 messages in one worker, every message is handled).

Alternative: do not keep the Statement in the runtime cache in
@BackendUtility::getPageForRootline()@ (or key it by connection and catch
@ConnectionLost@). The first option also fixes any other statement cached in the
runtime cache and the staleness.

Difference to #111010 (change 96568)

  1. fixes the sending side: the Doctrine transport has its own DBAL connection outside
    of the ConnectionPool, which can be dropped by the server while a long-running process is
    idle (scheduler:run), and the next send() fails. The change 96568 retries the send once
    after closing that connection.

This issue is about the handling side in a worker, on a ConnectionPool connection: the
middleware closes it, but a prepared statement kept in the runtime cache still belongs to it.
Change 96568 does not touch the middleware, the pool connections or the runtime cache, so the
failure above remains with it. The two do not overlap.

Notes

  • Only the worker is affected (the middleware only acts with a ConsumedByWorkerStamp).
  • SQLite is skipped by the middleware, so it does not show there.
  • #110227 has the target version 15.0, I did not check whether it is backported to 14.3.
    The functional tests of the extension run against 14.3.8 and do not show it.
  • The steps above are the minimal form of what the extension does, I reproduced it with the
    extension handler (25 records per message), not with this exact handler.

Reviews 1

History 34

  1. Wolfgang Klinger commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2026-11-14T09:25:12+00:00" > 11/14/26, 09:25 </span>

    Applied in changeset commit:634494b8a7e07e18c43b97973fb4153e73066fe3.