Repository navigation
[6.x] Don't cache shared errors under their own URL as well - #15988
Open
ryanmitchell wants to merge 1 commit into
Open
ryanmitchell wants to merge 1 commit into
ryanmitchell wants to merge 1 commit into
Conversation
With share_errors enabled every error is served from the shared copy, but the middleware also cached each one under its requested URL. That added every distinct erroring URL to the cached URL index, a single value rewritten under a lock on every cache write, so it grew without bound when bots requested endless unique URLs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
share_errorsis enabled, this stops the half measure static caching middleware from also caching each error under its own URL. Every error is served from the one shared copy anyway.Why
With
share_errorson, the first 404 is cached under/__shared-errors/{site}/404and every later 404 is served from it. The middleware still caches each error under its real URL as well, though. So every distinct URL that 404s ends up in the cached URL index (AbstractCacher::cacheUrl()).That index is one value per domain. Every cache write reads the whole thing, adds an entry and writes it back, while holding a single lock (
withUrlsLock(), which blocks for up to 10 seconds). Nothing ever removes an error entry except a fullstatic:clear, so the index grows with every unique bad URL.That's normally slow and harmless. It stops being harmless when something requests endless unique URLs. We hit this on a production site. Some links had been entered without a leading slash (
href="series/foo"), which a crawler resolved against the current page to build ever-deeper URLs (/product/1/series/foo/product/2/series/bar/...). Every 404 page contained the same links, so each one handed the crawler more URLs.Within a few days the index held about 174,000 URLs (18MB serialized), 84% of them from that crawl. Every request that cached a page, error or not, was reading and rewriting 18MB under the shared lock. The web servers sat at 100% CPU with PHP-FPM out of workers, at modest traffic. The same thing can be caused by vulnerability scanners, or by any bot that appends query strings or junk paths.
With shared errors enabled, the per-URL copy buys very little: a repeat request to the same bad URL skips the controller. That isn't worth unbounded index growth, and every error is already served from the shared copy.
What changes
When
share_errorsis enabled and the response isn't successful, the middleware:copyError()), as before;Nothing changes when
share_errorsis off: errors are still cached and tracked per URL, soit_caches_and_tracks_404sand the 404 invalidation tests are untouched.One side effect: a page published at a URL that previously 404'd no longer needs that URL invalidated before it shows, because no 404 was cached there.
Tests
shared_errors_are_only_cached_under_the_shared_errors_urlreplacesnocache_session_is_written_under_the_real_url_for_shared_errors. It checks that two different erroring URLs (one requested twice) leave only the shared-errors URL in the index, that the nocache region still renders, and that the session is only stored under the shared-errors URL. It fails without the change.SharedErrorsStaticCachingTestthat described repeat hits as "cached under its own URL". Those tests still pass, so repeat hits keep fresh CSRF tokens and nocache regions.Not covered
The URL index itself still grows with distinct cached successful URLs, such as pages with many query string variations. That's a bigger design question (e.g. sharding the index, or not tracking per-URL keys at all) and is out of scope here.
🤖 Generated with Claude Code