Skip to content

[Cache] Prevent fatal errors on php 8 when running concurrently with TagAwareAdapter v6.1 #46143

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
Apr 25, 2022
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
19 changes: 13 additions & 6 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@ public function hasItem($key)
}

foreach ($this->getTagVersions([$itemTags]) as $tag => $version) {
if ($itemTags[$tag] !== $version && 1 !== $itemTags[$tag] - $version) {
return false;
if ($itemTags[$tag] === $version || \is_int($itemTags[$tag]) && \is_int($version) && 1 === $itemTags[$tag] - $version) {
continue;
}

return false;
}

return true;
Expand Down Expand Up @@ -366,10 +368,11 @@ private function generateItems(iterable $items, array $tagKeys)

foreach ($itemTags as $key => $tags) {
foreach ($tags as $tag => $version) {
if ($tagVersions[$tag] !== $version && 1 !== $version - $tagVersions[$tag]) {
unset($itemTags[$key]);
continue 2;
if ($tagVersions[$tag] === $version || \is_int($version) && \is_int($tagVersions[$tag]) && 1 === $version - $tagVersions[$tag]) {
continue;
}
unset($itemTags[$key]);
continue 2;
}
}
$tagVersions = $tagKeys = null;
Expand Down Expand Up @@ -408,7 +411,7 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = [])
$tags = [];
foreach ($tagVersions as $tag => $version) {
$tags[$tag.static::TAGS_PREFIX] = $tag;
if ($fetchTagVersions || !isset($this->knownTagVersions[$tag])) {
if ($fetchTagVersions || !isset($this->knownTagVersions[$tag]) || !\is_int($version)) {
$fetchTagVersions = true;
continue;
}
Expand All @@ -430,6 +433,10 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = [])
if (isset($invalidatedTags[$tag])) {
$invalidatedTags[$tag] = $version->set(++$tagVersions[$tag]);
}
if (!\is_int($tagVersions[$tag])) {
Copy link
Member

Choose a reason for hiding this comment

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

on the line above, we have a ++ on this entry, shouldn't we check for this earlier?

Copy link
Contributor Author

@sbelyshkin sbelyshkin Apr 22, 2022

Choose a reason for hiding this comment

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

++ operator works with strings
Although it not always changes string values (and thus no invalidation occurs in those cases) but at least it doesn't do the fatality.

unset($this->knownTagVersions[$tag]);
continue;
}
$this->knownTagVersions[$tag] = [$now, $tagVersions[$tag]];
}

Expand Down
105 changes: 105 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,109 @@ private function getNonPruneableMock(): AdapterInterface
{
return $this->createMock(AdapterInterface::class);
}

/**
* @doesNotPerformAssertions
*/
public function testToleranceForStringsAsTagVersionsCase1()
{
$pool = $this->createCachePool();
$adapter = new FilesystemAdapter();

$itemKey = 'foo';
$tag = $adapter->getItem('bar'.TagAwareAdapter::TAGS_PREFIX);
$adapter->save($tag->set("\x00abc\xff"));
$item = $pool->getItem($itemKey);
$pool->save($item->tag('bar'));
$pool->hasItem($itemKey);
$pool->getItem($itemKey);
}

/**
* @doesNotPerformAssertions
*/
public function testToleranceForStringsAsTagVersionsCase2()
{
$pool = $this->createCachePool();
$adapter = new FilesystemAdapter();

$itemKey = 'foo';
$tag = $adapter->getItem('bar'.TagAwareAdapter::TAGS_PREFIX);
$adapter->save($tag->set("\x00abc\xff"));
$item = $pool->getItem($itemKey);
$pool->save($item->tag('bar'));
sleep(100);
$pool->getItem($itemKey);
$pool->hasItem($itemKey);
}

/**
* @doesNotPerformAssertions
*/
public function testToleranceForStringsAsTagVersionsCase3()
{
$pool = $this->createCachePool();
$adapter = new FilesystemAdapter();

$itemKey = 'foo';
$adapter->deleteItem('bar'.TagAwareAdapter::TAGS_PREFIX);
$item = $pool->getItem($itemKey);
$pool->save($item->tag('bar'));
$pool->getItem($itemKey);

$tag = $adapter->getItem('bar'.TagAwareAdapter::TAGS_PREFIX);
$adapter->save($tag->set("\x00abc\xff"));

$pool->hasItem($itemKey);
$pool->getItem($itemKey);
sleep(100);
$pool->getItem($itemKey);
$pool->hasItem($itemKey);
}

/**
* @doesNotPerformAssertions
*/
public function testToleranceForStringsAsTagVersionsCase4()
{
$pool = $this->createCachePool();
$adapter = new FilesystemAdapter();

$itemKey = 'foo';
$tag = $adapter->getItem('bar'.TagAwareAdapter::TAGS_PREFIX);
$adapter->save($tag->set('abcABC'));

$item = $pool->getItem($itemKey);
$pool->save($item->tag('bar'));

$tag = $adapter->getItem('bar'.TagAwareAdapter::TAGS_PREFIX);
$adapter->save($tag->set('001122'));

$pool->invalidateTags(['bar']);
$pool->getItem($itemKey);
}

/**
* @doesNotPerformAssertions
*/
public function testToleranceForStringsAsTagVersionsCase5()
{
$pool = $this->createCachePool();
$pool2 = $this->createCachePool();
$adapter = new FilesystemAdapter();

$itemKey1 = 'foo';
$item = $pool->getItem($itemKey1);
$pool->save($item->tag('bar'));

$tag = $adapter->getItem('bar'.TagAwareAdapter::TAGS_PREFIX);
$adapter->save($tag->set('abcABC'));

$itemKey2 = 'baz';
$item = $pool2->getItem($itemKey2);
$pool2->save($item->tag('bar'));
foreach ($pool->getItems([$itemKey1, $itemKey2]) as $item) {
// run generator
}
}
}