From ebab74d26b5185e1b54e30dc56b3721d693effd9 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 21 May 2025 13:01:46 +0200 Subject: [PATCH 1/3] [HttpCache] Add a `waiting` trace when finding the cache locked --- .../HttpKernel/HttpCache/HttpCache.php | 2 ++ .../Tests/HttpCache/HttpCacheTest.php | 28 +++++++++++++++++++ .../Tests/HttpCache/HttpCacheTestCase.php | 11 ++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php index 3ef1b8dcb821f..d5ed6d65597ac 100644 --- a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php +++ b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php @@ -558,6 +558,8 @@ protected function lock(Request $request, Response $entry): bool return true; } + $this->record($request, 'waiting'); + // wait for the lock to be released if ($this->waitForLock($request)) { // replace the current entry with the fresh one diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index e82e8fd81b481..2b553dc875d39 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -17,6 +17,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; @@ -662,6 +663,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()); @@ -680,6 +682,32 @@ public function testDegradationWhenCacheLocked() $this->assertEquals('Old response', $this->response->getContent()); } + public function testTraceAddedWhenCacheLocked() + { + if ('\\' === \DIRECTORY_SEPARATOR) { + $this->markTestSkipped('Skips on windows to avoid permissions issues.'); + } + + // Disable stale-while-revalidate, which circumvents blocking access + $this->cacheConfig['stale_while_revalidate'] = 0; + + // The presence of Last-Modified makes this cacheable + $this->setNextResponse(200, ['Cache-Control' => 'public, no-cache', 'Last-Modified' => 'some while ago'], 'Old response'); + $this->request('GET', '/'); // warm the cache + + $primedStore = $this->store; + + $this->store = $this->createMock(Store::class); + $this->store->method('lookup')->willReturnCallback(fn (Request $request) => $primedStore->lookup($request)); + // Assume the cache is locked at the first attempt, but immediately treat the lock as released afterwards + $this->store->method('lock')->willReturnOnConsecutiveCalls(false, true); + $this->store->method('isLocked')->willReturn(false); + + $this->request('GET', '/'); + + $this->assertTraceContains('waiting'); + } + public function testHitsCachedResponseWithSMaxAgeDirective() { $time = \DateTimeImmutable::createFromFormat('U', time() - 5); diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php index 26a29f16b2b75..88f6bed56f4cf 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php @@ -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 { @@ -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; @@ -183,4 +185,9 @@ public static function clearDirectory($directory) closedir($fp); } + + protected function createStore(): Store + { + return new Store(sys_get_temp_dir() . '/http_cache'); + } } From 55e77944138979275621cb40246aee7676b36546 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 21 May 2025 13:03:39 +0200 Subject: [PATCH 2/3] Update the CHANGELOG --- src/Symfony/Component/HttpKernel/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index 6bf1a60ebc6e2..5df71549449f3 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -4,11 +4,12 @@ CHANGELOG 7.3 --- + * Record a `waiting` trace in the `HttpCache` when the cache had to wait for another request to finish * Add `$key` argument to `#[MapQueryString]` that allows using a specific key for argument resolving * Support `Uid` in `#[MapQueryParameter]` * Add `ServicesResetterInterface`, implemented by `ServicesResetter` * Allow configuring the logging channel per type of exceptions in ErrorListener - + 7.2 --- From 6134f8a94b8d51a4cfbf899c9c06fa2745c76ba1 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 21 May 2025 13:14:40 +0200 Subject: [PATCH 3/3] Update src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php Co-authored-by: Oskar Stark --- .../Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php index 88f6bed56f4cf..fd55d51cf7772 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php @@ -115,7 +115,7 @@ public function request($method, $uri = '/', $server = [], $cookies = [], $esi = $this->kernel->reset(); - if (! $this->store) { + if (!$this->store) { $this->store = $this->createStore(); }