Skip to content

fix(symfony): reset the HTTP cache purger between requests - #8857

Open
KevinMartinsDev wants to merge 1 commit into
api-platform:4.4from
KevinMartinsDev:fix/reset-http-cache-purger
Open

KevinMartinsDev wants to merge 1 commit into
api-platform:4.4from
KevinMartinsDev:fix/reset-http-cache-purger

Conversation

@KevinMartinsDev

Copy link
Copy Markdown
Contributor
Q A
Branch? 4.4
Tickets -
License MIT
Doc PR

PurgeHttpCacheListener collects the tags (IRIs) of the changed resources in preUpdate and onFlush, and purges them in postFlush, which then clears the list. Same root cause as #8684: Doctrine only dispatches postFlush when the commit succeeds. When it fails (unique or check constraint, deadlock, lost connection…), the collected tags stay in the listener.

With a long-running process (FrankenPHP worker mode, RoadRunner, Swoole, a Messenger consumer), the listener is shared, so the next successful flush of the worker purges them together with its own tags, whoever made that request. Reproduced on API Platform 5.0.8 with a FrankenPHP worker (one thread) and http_cache.invalidation enabled:

PATCH /api/books/1 {"title": "Dune"}   (title unique, taken by book 2)  -> 422, rolled back
POST  /api/bookmarks {"book": "/api/books/2"}                            -> 201
  worker : purges /api/books /api/books/1 /api/books/2
  classic: purges /api/books/2

The impact is low: superfluous purges, no data exposed (the tags are IRIs). postFlush() also cleared the tags only after the purge, so when the purger throws (cache server down), the list kept growing from one request to the next.

This PR, like #8684:

  • makes the listener implement ResetInterface, with a reset() method that clears the tags and the scheduled insertions;
  • tags the ORM service with kernel.reset (on_invalid: ignore), so the services resetter clears it between requests and Messenger messages;
  • clears the list in a finally after the purge, so a failing purger does not make it grow.

On main, the ODM service (doctrine_odm_http_cache_purger.php) needs the same kernel.reset tag when this is merged up; it does not exist on 4.4.

Tests: ApiPlatformExtensionTest::testHttpCachePurgerIsReset, and two listener tests: the tags of a flush whose commit failed are not purged after reset(), and a purge that throws does not leave its tags for the next flush. They fail without the fix.

Doctrine does not dispatch postFlush when the commit fails, so the tags PurgeHttpCacheListener
collected in onFlush and preUpdate stayed in the shared service and were purged by the next
successful flush of the worker, during another request. The listener now implements
ResetInterface and is tagged kernel.reset, and postFlush() clears its tags even when the purge
throws.
@KevinMartinsDev
KevinMartinsDev force-pushed the fix/reset-http-cache-purger branch from 41ff32c to 65fbc3d Compare November 16, 2026 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant