Skip to content

[HttpCache] Add a waiting trace when finding the cache locked #60498

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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);
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');
}
}
Loading