Skip to content

Commit

Permalink
Merge pull request #21710 from nextcloud/backport/21074/stable19
Browse files Browse the repository at this point in the history
[stable19] Fix releasing a shared lock multiple times
  • Loading branch information
MorrisJobke authored Jul 6, 2020
2 parents 3417579 + 3f1b055 commit 42d899c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 14 additions & 0 deletions apps/dav/lib/Connector/Sabre/LockPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,21 @@ class LockPlugin extends ServerPlugin {
*/
private $server;

/**
* State of the lock
*
* @var bool
*/
private $isLocked;

/**
* {@inheritdoc}
*/
public function initialize(\Sabre\DAV\Server $server) {
$this->server = $server;
$this->server->on('beforeMethod:*', [$this, 'getLock'], 50);
$this->server->on('afterMethod:*', [$this, 'releaseLock'], 50);
$this->isLocked = false;
}

public function getLock(RequestInterface $request) {
Expand All @@ -67,10 +75,15 @@ public function getLock(RequestInterface $request) {
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
$this->isLocked = true;
}
}

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'])) {
return;
}
Expand All @@ -81,6 +94,7 @@ public function releaseLock(RequestInterface $request) {
}
if ($node instanceof Node) {
$node->releaseLock(ILockingProvider::LOCK_SHARED);
$this->isLocked = false;
}
}
}
6 changes: 5 additions & 1 deletion lib/private/Lock/MemcacheLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ public function acquireLock(string $path, int $type) {
*/
public function releaseLock(string $path, int $type) {
if ($type === self::LOCK_SHARED) {
$ownSharedLockCount = $this->getOwnSharedLockCount($path);
$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
if (!$removed) { //someone else also has a shared lock, decrease only
$newValue = $this->memcache->dec($path);
Expand Down

0 comments on commit 42d899c

Please sign in to comment.