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

Skip to content

Commit fdff819

Browse files
bug #38879 [Cache] Fixed expiry could be int in ChainAdapter due to race conditions (phamviet)
This PR was merged into the 4.4 branch. Discussion ---------- [Cache] Fixed expiry could be int in ChainAdapter due to race conditions | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38635 | License | MIT | Doc PR | no This bug is hard to re-produce and seems only happen with ArrayAdapter only. Steps to reproduce: cache.yaml ``` simple.cache: adapters: - cache.adapter.array - cache.adapter.redis ``` ```php if (isset($item->metadata[CacheItem::METADATA_EXPIRY])) { $logger->debug($item->key, $item->metadata); $format = is_int($item->metadata[CacheItem::METADATA_EXPIRY]) ? 'U' : 'U.u'; $item->expiresAt(\DateTime::createFromFormat($format, $item->metadata[CacheItem::METADATA_EXPIRY])); } ``` Refresh webpage multiple time to make web server busy and logs: ``` [2020-10-29T17:04:51.119653+07:00] application.DEBUG: item-key {"expiry":1603965892.118222,"ctime":4} [] [2020-10-29T17:04:54.322937+07:00] application.DEBUG: item-key {"expiry":1603965895.308393,"ctime":17} [] [2020-10-29T17:04:54.745923+07:00] application.DEBUG: item-key {"expiry":1603965895,"ctime":16} [] ``` Commits ------- 268816f [Cache] Fixed expiry maybe int due too race conditions
2 parents 487908a + 268816f commit fdff819

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/Symfony/Component/Cache/Adapter/ChainAdapter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ static function ($sourceItem, $item, $sourceMetadata = null) use ($defaultLifeti
7474
$item->metadata = $item->newMetadata = $sourceItem->metadata = $sourceMetadata;
7575

7676
if (isset($item->metadata[CacheItem::METADATA_EXPIRY])) {
77-
$item->expiresAt(\DateTime::createFromFormat('U.u', $item->metadata[CacheItem::METADATA_EXPIRY]));
77+
if (\is_int($expiry = $item->metadata[CacheItem::METADATA_EXPIRY])) {
78+
$item->expiresAt(\DateTime::createFromFormat('U', $expiry));
79+
} else {
80+
$item->expiresAt(\DateTime::createFromFormat('U.u', sprintf('%.3F', $expiry)));
81+
}
7882
} elseif (0 < $defaultLifetime) {
7983
$item->expiresAfter($defaultLifetime);
8084
}

0 commit comments

Comments
 (0)