Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpCache] Hit the backend only once after waiting for the cache lock #60502

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
Jun 13, 2025

Conversation

mpdude
Copy link
Contributor

@mpdude mpdude commented May 21, 2025

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues
License MIT

When the HttpCache has to wait for a lock held by another, concurrent process, the lock() method wants to make sure we continue operation based on the most recent cache entry, possibly updated by the concurrent process.

So, after waiting for lock release, it calls lookup() to obtain this cache entry. This is, in fact, a reentrant call up into the current call stack.

Having lookup() multiple times on the call stack opens up a way to call the backend for validation multiple times. I have observed this in practice at least in combination with the no-cache cache-control header, causing surprising side effects™️ ✨.

Also without no-cache you can get strange-looking cache traces like stale, valid, store, fresh. Those occur only when concurrent locking is a issue.

I am not super happy with using an exception for a control flow issue like this. But, rolling back to lookup seems to be the most sensible decision for me, and using special return values to indicate this condition isn't really pretty either.

Comment on lines 222 to 226
try {
$response = $this->lookup($request, $catch);
} catch (CacheWasLockedException) {
$response = $this->lookup($request, $catch);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows to restart the process only once.

In fact, the old code would wait for the lock to be released, but only try to acquire it later on. That could, in theory, allow for repeated cycles without actually reaching the 503 condition in lock().

Maybe it is even better this way to fail with the exception instead of silenty retrying over and over again?

@mpdude mpdude force-pushed the hit-backend-once-cache-lock branch from 110274d to ebe451c Compare May 30, 2025 13:16
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using a dedicated request attribute instead ?

@mpdude
Copy link
Contributor Author

mpdude commented May 30, 2025

You mean, proceed with the re-entrant lookup from inside/after waiting for the lock, but then indicate on the request that it performed validation etc. and so the "outer" method invocations return immediately?

@nicolas-grekas
Copy link
Member

I means something like this:

$response = $this->lookup($request, $catch);
if ($request->attribute->has('foo')) { // lookup again after removing the attribute

With an early return in the lock method indeed.
Should this be in a loop BTW?

@mpdude
Copy link
Contributor Author

mpdude commented May 30, 2025

The return has to be through several methods up the call stack. That would need some kind of check at every level with early returns in each case.

@nicolas-grekas
Copy link
Member

Hum indeed.
Then let's make the added exception internal.
Also : what about a loop instead of just one retry?

@mpdude
Copy link
Contributor Author

mpdude commented May 30, 2025

I am fine with both. Should we be afraid of infinite retries? We could also limit to a certain number of iterations.

@mpdude
Copy link
Contributor Author

mpdude commented May 30, 2025

Exception is now @internal, and also looping until a response can be obtained

@mpdude mpdude requested a review from nicolas-grekas June 4, 2025 09:01
@nicolas-grekas nicolas-grekas force-pushed the hit-backend-once-cache-lock branch from 0b6199e to df064b0 Compare June 13, 2025 07:11
@nicolas-grekas
Copy link
Member

Thank you @mpdude.

@nicolas-grekas nicolas-grekas merged commit aaa67ea into symfony:6.4 Jun 13, 2025
11 checks passed
@mpdude
Copy link
Contributor Author

mpdude commented Jun 13, 2025

Merging minutes before being a conference host in a live video stream 🤓

@mpdude mpdude deleted the hit-backend-once-cache-lock branch June 13, 2025 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants