diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php
index 16074a89ca83a0995138f89f2775dc8bc7bb872f..fba412c37d4e3f3fd05641f8218d5d1d67c5f838 100644
--- a/lib/private/Files/View.php
+++ b/lib/private/Files/View.php
@@ -665,7 +665,13 @@ class View {
 					return false;
 				}
 
-				$this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
+				try {
+					$this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
+				} catch (\Exception $e) {
+					// Release the shared lock before throwing.
+					$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
+					throw $e;
+				}
 
 				/** @var \OC\Files\Storage\Storage $storage */
 				[$storage, $internalPath] = $this->resolvePath($path);