Skip to content

Commit e29e3af

Browse files
committed
MC-32427: Sign locks in order to prevent unlock from parallel request
1 parent 985c679 commit e29e3af

File tree

1 file changed

+39
-3
lines changed
  • lib/internal/Magento/Framework/Lock/Backend

1 file changed

+39
-3
lines changed

lib/internal/Magento/Framework/Lock/Backend/Cache.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class Cache implements \Magento\Framework\Lock\LockManagerInterface
2424
*/
2525
private $cache;
2626

27+
/**
28+
* Sign for locks, helps to avoid removing a lock that was created by another client
29+
*
30+
* @string
31+
*/
32+
private $lockSign;
33+
2734
/**
2835
* @param FrontendInterface $cache
2936
*/
@@ -37,19 +44,48 @@ public function __construct(FrontendInterface $cache)
3744
*/
3845
public function lock(string $name, int $timeout = -1): bool
3946
{
40-
if ((bool)$this->cache->test($this->getIdentifier($name))) {
47+
if (empty($this->lockSign)) {
48+
$this->lockSign = \bin2hex(\random_bytes(8));
49+
}
50+
51+
$data = $this->cache->load($this->getIdentifier($name));
52+
53+
if (false !== $data) {
4154
return false;
4255
}
4356

44-
return $this->cache->save('1', $this->getIdentifier($name), [], $timeout);
57+
$saveResult = $this->cache->save($this->lockSign, $this->getIdentifier($name), [], $timeout * 100);
58+
59+
$data = $this->cache->load($this->getIdentifier($name));
60+
61+
if ($data === $this->lockSign && $saveResult) {
62+
return true;
63+
}
64+
65+
return false;
4566
}
4667

4768
/**
4869
* @inheritdoc
4970
*/
5071
public function unlock(string $name): bool
5172
{
52-
return (bool)$this->cache->remove($this->getIdentifier($name));
73+
if (empty($this->lockSign)) {
74+
return false;
75+
}
76+
77+
$data = $this->cache->load($this->getIdentifier($name));
78+
79+
if (false === $data) {
80+
return false;
81+
}
82+
83+
$removeResult = false;
84+
if ($data === $this->lockSign) {
85+
$removeResult = (bool)$this->cache->remove($this->getIdentifier($name));
86+
}
87+
88+
return $removeResult;
5389
}
5490

5591
/**

0 commit comments

Comments
 (0)