Skip to content

[Cache] make LockRegistry use static properties instead of static variables #46004

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 12, 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
12 changes: 7 additions & 5 deletions src/Symfony/Component/Cache/LockRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class LockRegistry
{
private static $openedFiles = [];
private static $lockedFiles;
private static $signalingException;
private static $signalingCallback;

/**
* The number of items in this list controls the max number of concurrent processes.
Expand Down Expand Up @@ -92,6 +94,9 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s
return $callback($item, $save);
}

self::$signalingException ?? self::$signalingException = unserialize("O:9:\"Exception\":1:{s:16:\"\0Exception\0trace\";a:0:{}}");
self::$signalingCallback ?? self::$signalingCallback = function () { throw self::$signalingException; };

while (true) {
try {
// race to get the lock in non-blocking mode
Expand Down Expand Up @@ -121,18 +126,15 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s
flock($lock, \LOCK_UN);
unset(self::$lockedFiles[$key]);
}
static $signalingException, $signalingCallback;
$signalingException = $signalingException ?? unserialize("O:9:\"Exception\":1:{s:16:\"\0Exception\0trace\";a:0:{}}");
$signalingCallback = $signalingCallback ?? function () use ($signalingException) { throw $signalingException; };

try {
$value = $pool->get($item->getKey(), $signalingCallback, 0);
$value = $pool->get($item->getKey(), self::$signalingCallback, 0);
$logger && $logger->info('Item "{key}" retrieved after lock was released', ['key' => $item->getKey()]);
$save = false;

return $value;
} catch (\Exception $e) {
if ($signalingException !== $e) {
if (self::$signalingException !== $e) {
throw $e;
}
$logger && $logger->info('Item "{key}" not found while lock was released, now retrying', ['key' => $item->getKey()]);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Traits/ContractsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ trait ContractsTrait
public function setCallbackWrapper(?callable $callbackWrapper): callable
{
if (!isset($this->callbackWrapper)) {
$this->callbackWrapper = [LockRegistry::class, 'compute'];
$this->callbackWrapper = \Closure::fromCallable([LockRegistry::class, 'compute']);;

if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
$this->setCallbackWrapper(null);
Expand Down