From 218747e564c95a50219c342c8e3e2aaacc2467a3 Mon Sep 17 00:00:00 2001 From: PeterPablo <4340267+PeterPablo@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:18:59 +0100 Subject: [PATCH] fix(close): release the file lock when no *other* user is still editing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ApiService::close()` cleaned up the document context — and with it the `files_lock` row — only when `getActiveSessions()` was empty. That is not the same as "nobody is editing": the method counts every session row younger than `SESSION_VALID_TIME` (5 minutes), and a reload of the editor leaves such rows behind. Closing the freshest session therefore skipped the cleanup while a leftover row of the *same* user kept the document "active". Measured with the Nextcloud Notes app on Android, note opened through Direct Editing, six session rows for one document, all the same user, the newest 13 s old when its own close ran: POST /apps/text/session//close 200 PUT /apps/notes/api/v1/notes/ 423 "…is locked, existing lock on file: files_lock/0f01f381-…" oc_files_lock 1 row, still there The file stayed locked, so the user could not rename or move the note — for hours, until somebody released the row by hand. A close whose own row cannot be matched leaves that row behind too, so the same gate fails for a second reason. The lock exists to warn other people, so ask about them: * `SessionService::hasRecentSessionsOfOtherUsers()` — any non-guest session of a different user with contact in the last 60 s (a live editor pushes every few seconds, so the window separates "somebody is typing" from leftovers); * `close()` cleans up when no session is left **or** when no other user has been active. Closing one of several tabs of the same user no longer skips it. Assisted-by: DeepSeek:deepseek-v4.1-flash-1017 Signed-off-by: PeterPablo <4340267+PeterPablo@users.noreply.github.com> --- lib/Service/ApiService.php | 8 +++++++- lib/Service/SessionService.php | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/Service/ApiService.php b/lib/Service/ApiService.php index 3a49d6be8ac..f7a972a5a25 100644 --- a/lib/Service/ApiService.php +++ b/lib/Service/ApiService.php @@ -94,7 +94,13 @@ public function close(string $documentId, int $sessionId, string $sessionToken, $this->sessionService->closeSession($documentId, $sessionId, $sessionToken); $this->sessionService->removeInactiveSessionsWithoutSteps($documentId); $activeSessions = $this->sessionService->getActiveSessions($documentId); - if (count($activeSessions) === 0) { + // "No session left" is not the same as "nobody is editing": a reload of the editor leaves + // rows behind that are still inside SESSION_VALID_TIME (5 min), and a close whose row cannot + // be matched leaves its own row too. Either way the lock was never released and the file + // stayed unwritable for everybody else. The lock protects other people, so ask about them. + $closer = $auth instanceof IUser ? $auth->getUID() : null; + $othersEditing = $this->sessionService->hasRecentSessionsOfOtherUsers($documentId, $closer); + if (count($activeSessions) === 0 || !$othersEditing) { $document = $this->documentService->getDocument($documentId); if ($document !== null) { $type = $document->getContextType(); diff --git a/lib/Service/SessionService.php b/lib/Service/SessionService.php index 6ebbb984f74..ef6051096e7 100644 --- a/lib/Service/SessionService.php +++ b/lib/Service/SessionService.php @@ -162,6 +162,29 @@ public function getSession(string $documentId, int $sessionId, string $token): ? return $this->session; } + /** + * Is another user still working on this document? + * + * The file lock exists to warn other people, so that is the question that decides whether it may + * be released. Leftover rows of the same user — one per reload of the editor, plus any row whose + * close could not be matched — must not keep a file locked. + * + * A live editor pushes every few seconds, so a short window separates "somebody is typing" from + * "these rows are leftovers". + */ + public function hasRecentSessionsOfOtherUsers(string $documentId, ?string $userId, int $graceSeconds = 60): bool { + $threshold = $this->timeFactory->now()->getTimestamp() - $graceSeconds; + foreach ($this->sessionMapper->findAll($documentId) as $session) { + if ($session->getLastContact() < $threshold || $session->isGuest()) { + continue; + } + if ($userId === null || $session->getUserId() !== $userId) { + return true; + } + } + return false; + } + public function getValidSession(string $documentId, int $sessionId, string $token): ?Session { $session = $this->getSession($documentId, $sessionId, $token); if ($session === null) {