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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\HttpCache;

/**
* @internal
*/
class CacheWasLockedException extends \Exception
{
}
18 changes: 8 additions & 10 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,13 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
$this->record($request, 'reload');
$response = $this->fetch($request, $catch);
} else {
$response = $this->lookup($request, $catch);
$response = null;
do {
try {
$response = $this->lookup($request, $catch);
} catch (CacheWasLockedException) {
}
} while (null === $response);
}

$this->restoreResponseBody($request, $response);
Expand Down Expand Up @@ -576,15 +582,7 @@ protected function lock(Request $request, Response $entry): bool

// wait for the lock to be released
if ($this->waitForLock($request)) {
// replace the current entry with the fresh one
$new = $this->lookup($request);
$entry->headers = $new->headers;
$entry->setContent($new->getContent());
$entry->setStatusCode($new->getStatusCode());
$entry->setProtocolVersion($new->getProtocolVersion());
foreach ($new->headers->getCookies() as $cookie) {
$entry->headers->setCookie($cookie);
}
throw new CacheWasLockedException(); // unwind back to handle(), try again
} else {
// backend is slow as hell, send a 503 response (to avoid the dog pile effect)
$entry->setStatusCode(503);
Expand Down
60 changes: 60 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;
Expand Down Expand Up @@ -717,6 +718,7 @@ public function testDegradationWhenCacheLocked()
*/
sleep(10);

$this->store = $this->createStore(); // create another store instance that does not hold the current lock
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
Expand All @@ -735,6 +737,64 @@ public function testDegradationWhenCacheLocked()
$this->assertEquals('Old response', $this->response->getContent());
}

public function testHitBackendOnlyOnceWhenCacheWasLocked()
{
// Disable stale-while-revalidate, it circumvents waiting for the lock
$this->cacheConfig['stale_while_revalidate'] = 0;

$this->setNextResponses([
[
'status' => 200,
'body' => 'initial response',
'headers' => [
'Cache-Control' => 'public, no-cache',
'Last-Modified' => 'some while ago',
],
],
[
'status' => 304,
'body' => '',
'headers' => [
'Cache-Control' => 'public, no-cache',
'Last-Modified' => 'some while ago',
],
],
[
'status' => 500,
'body' => 'The backend should not be called twice during revalidation',
'headers' => [],
],
]);

$this->request('GET', '/'); // warm the cache

// Use a store that simulates a cache entry being locked upon first attempt
$this->store = new class(sys_get_temp_dir() . '/http_cache') extends Store {
private bool $hasLock = false;

public function lock(Request $request): bool
{
$hasLock = $this->hasLock;
$this->hasLock = true;

return $hasLock;
}

public function isLocked(Request $request): bool
{
return false;
}
};

$this->request('GET', '/'); // hit the cache with simulated lock/concurrency block

$this->assertEquals(200, $this->response->getStatusCode());
$this->assertEquals('initial response', $this->response->getContent());

$traces = $this->cache->getTraces();
$this->assertSame(['stale', 'valid', 'store'], current($traces));
}

public function testHitsCachedResponseWithSMaxAgeDirective()
{
$time = \DateTimeImmutable::createFromFormat('U', time() - 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class HttpCacheTestCase extends TestCase
protected $responses;
protected $catch;
protected $esi;
protected Store $store;
protected ?Store $store = null;

protected function setUp(): void
{
Expand Down Expand Up @@ -115,7 +115,9 @@ public function request($method, $uri = '/', $server = [], $cookies = [], $esi =

$this->kernel->reset();

$this->store = new Store(sys_get_temp_dir().'/http_cache');
if (! $this->store) {
$this->store = $this->createStore();
}

if (!isset($this->cacheConfig['debug'])) {
$this->cacheConfig['debug'] = true;
Expand Down Expand Up @@ -183,4 +185,9 @@ public static function clearDirectory($directory)

closedir($fp);
}

protected function createStore(): Store
{
return new Store(sys_get_temp_dir() . '/http_cache');
}
}