Skip to content
Snippets Groups Projects
Unverified Commit e878c0a0 authored by Morris Jobke's avatar Morris Jobke Committed by GitHub
Browse files

Merge pull request #21074 from jvsalo/shared-lock-multi-release

Fix releasing a shared lock multiple times
parents d72d9ff1 6886b46e
No related branches found
No related tags found
No related merge requests found
...@@ -41,6 +41,13 @@ class LockPlugin extends ServerPlugin { ...@@ -41,6 +41,13 @@ class LockPlugin extends ServerPlugin {
*/ */
private $server; private $server;
/**
* State of the lock
*
* @var bool
*/
private $isLocked;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -48,6 +55,7 @@ class LockPlugin extends ServerPlugin { ...@@ -48,6 +55,7 @@ class LockPlugin extends ServerPlugin {
$this->server = $server; $this->server = $server;
$this->server->on('beforeMethod:*', [$this, 'getLock'], 50); $this->server->on('beforeMethod:*', [$this, 'getLock'], 50);
$this->server->on('afterMethod:*', [$this, 'releaseLock'], 50); $this->server->on('afterMethod:*', [$this, 'releaseLock'], 50);
$this->isLocked = false;
} }
public function getLock(RequestInterface $request) { public function getLock(RequestInterface $request) {
...@@ -67,10 +75,15 @@ class LockPlugin extends ServerPlugin { ...@@ -67,10 +75,15 @@ class LockPlugin extends ServerPlugin {
} catch (LockedException $e) { } catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e); throw new FileLocked($e->getMessage(), $e->getCode(), $e);
} }
$this->isLocked = true;
} }
} }
public function releaseLock(RequestInterface $request) { public function releaseLock(RequestInterface $request) {
// don't try to release the lock if we never locked one
if ($this->isLocked === false) {
return;
}
if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) { if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) {
return; return;
} }
...@@ -81,6 +94,7 @@ class LockPlugin extends ServerPlugin { ...@@ -81,6 +94,7 @@ class LockPlugin extends ServerPlugin {
} }
if ($node instanceof Node) { if ($node instanceof Node) {
$node->releaseLock(ILockingProvider::LOCK_SHARED); $node->releaseLock(ILockingProvider::LOCK_SHARED);
$this->isLocked = false;
} }
} }
} }
...@@ -95,8 +95,12 @@ class MemcacheLockingProvider extends AbstractLockingProvider { ...@@ -95,8 +95,12 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
*/ */
public function releaseLock(string $path, int $type) { public function releaseLock(string $path, int $type) {
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
$ownSharedLockCount = $this->getOwnSharedLockCount($path);
$newValue = 0; $newValue = 0;
if ($this->getOwnSharedLockCount($path) === 1) { if ($ownSharedLockCount === 0) { // if we are not holding the lock, don't try to release it
return;
}
if ($ownSharedLockCount === 1) {
$removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go
if (!$removed) { //someone else also has a shared lock, decrease only if (!$removed) { //someone else also has a shared lock, decrease only
$newValue = $this->memcache->dec($path); $newValue = $this->memcache->dec($path);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment