Skip to content

Purge through the purger API Platform's BufferedPurger decorates (#610) - #618

Merged
silverbackdan merged 2 commits into
mainfrom
fix/buffered-purge-failures
Nov 26, 2026
Merged

silverbackdan merged 2 commits into
mainfrom
fix/buffered-purge-failures

Conversation

@silverbackdan

Copy link
Copy Markdown
Collaborator

Fixes #610.

Problem

api-platform/core dev-main (api-platform/core#8869, "purge once per request", in no release yet) adds ApiPlatform\HttpCache\BufferedPurger as api_platform.http_cache.purger.buffered. It decorates api_platform.http_cache.purger. During a main request it holds every purge() call, and HttpCachePurgeBufferListener flushes it on kernel.response (priority -1024). Upstream's goal is to stop a write being purged twice: once by AP's Doctrine listener and once by PurgeTagsProcessor. Outside a request (console, messenger) purges are still immediate. The PR adds no config switch and no event. The only hooks are the service ids.

The bundle injected api_platform.http_cache.purger, so on dev-main it got the buffered purger. HttpCachePurger::send() then only buffered the tags, and the real purge ran in AP's listener. A failure there is a PurgeFailedException the bundle cannot catch, so it became a 500. That broke #311 in two ways:

  • after a write, a failed purge should be logged and swallowed, with Mercure still publishing;
  • the explicit purge endpoints should return 502.

Approach

The bundle's purger is now the buffer's inner purger. ApiPlatformCompilerPass checks whether api_platform.http_cache.purger.buffered is defined and decorates a service. If it does, HttpCachePurger (argument 2) and HttpCacheFlusher (argument 0) get the buffer's inner purger: its renamed inner id, else <id>.inner. Each purge then runs, and fails, inside the bundle's own call, as before.

  • No version branch. It is a container check, like the existing ones for AP's Doctrine purge and Mercure listeners. On 4.4 and 5.0.x there is no such service and nothing changes.
  • Nothing is lost by skipping the buffer. The bundle already removes AP's Doctrine purge listener and purges once per Doctrine flush, so the double purge upstream fixes never came from the bundle. AP's own processors still use the buffered purger.
  • It also protects the flush. HttpCacheFlusher::canFlush() checks instanceof SouinPurger, which is false on the buffered decorator. Without this change, a Souin site's POST /_/http_cache/purge with no body would have become a 501.

I rejected the other options:

  • Decorating the listener: it would catch the failure, but too late. The explicit endpoints have already answered 204 by kernel.response, so they could not return 502.
  • Replacing or decorating the buffered purger: it would change behaviour AP chose for its own code.
  • Calling flush() from the bundle: it would need a BufferedPurger type check in src/, and it would stop AP's buffering for the rest of the request.

Tests

  • ApiPlatformCompilerPassTest (bare ContainerBuilder) covers:
    • the default inner id;
    • a renamed inner id;
    • no buffered purger;
    • a buffered service that decorates nothing;
    • the bundle purger already removed (the flusher is still bypassed);
    • the bundle's HTTP cache services not loaded.
  • These already-failing tests now pass on dev-main and keep passing on stable:
    • HttpCachePurgeFailureTest (both tests);
    • PurgeHttpCacheOperationTest::test_a_scoped_purge_that_fails_is_a_502.
  • Local runs:
    • paratest -p4 on both suites: stable (AP v5.0.9) 6450 OK; NEXT setup (AP dev-main, Symfony 8.2, temporary copy, not committed) 6450 OK. Before the fix, the NEXT setup failed those 3 tests.
    • Functional suite with API_PLATFORM_USE_SYMFONY_LISTENERS=1: 1832 OK.
    • PHPStan and php-cs-fixer are clean.

🤖 Generated with Claude Code

API Platform dev-main (api-platform/core#8869) buffers purges during a main
request and flushes them on kernel.response, where a failure is a 500 the
bundle cannot catch. Give HttpCachePurger and HttpCacheFlusher the buffer's
inner purger so #311 holds: a failed purge after a write is logged, and an
explicit purge is a 502.
@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.25%. Comparing base (e15c463) to head (c99b6db).

Additional details and impacted files
@@            Coverage Diff            @@
##               main     #618   +/-   ##
=========================================
  Coverage     97.25%   97.25%           
- Complexity     5606     5611    +5     
=========================================
  Files           461      461           
  Lines         14366    14378   +12     
=========================================
+ Hits          13971    13984   +13     
+ Misses          395      394    -1     
Flag Coverage Δ
phpunit 97.25% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@silverbackdan
silverbackdan merged commit 119a339 into main Nov 26, 2026
15 checks passed
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.

API Platform dev-main buffers purges on kernel.response: a failed purge after a write is a 500 again

1 participant