Skip to content

fix(close): release the file lock when no other user is still editing - #9603

Draft
PeterPablo wants to merge 1 commit into
nextcloud:mainfrom
PeterPablo:fix/release-lock-when-last-editor-leaves
Draft

PeterPablo wants to merge 1 commit into
nextcloud:mainfrom
PeterPablo:fix/release-lock-when-last-editor-leaves

Conversation

@PeterPablo

@PeterPablo PeterPablo commented Nov 14, 2026 •

Copy link
Copy Markdown

Problem

ApiService::close() released the file lock only when getActiveSessions() came back empty — and that method counts every session row younger than SESSION_VALID_TIME (5 minutes). A reload of the editor leaves such rows behind, so closing the freshest session skipped context->cleanup() and the files_lock row survived. The document then cannot be written or renamed by anyone until somebody releases the row by hand — the symptom described in #9042.

Measured on 35.0.2 with the Notes app (Direct Editing → Text in a WebView), one note, six session rows, all the same user, the newest 13 s old when its own close ran:

POST /apps/text/session/<id>/close       200        ← the close succeeded
PUT  /apps/notes/api/v1/notes/958914     423        ← the rename did not
  "files/Notes/tasks 2026 v4.md" is locked, existing lock on file: files_lock/0f01f381-…
oc_files_lock                            1 row, unchanged

Two ways the gate fails, and both are ordinary:

  1. a reload (or a second tab) of the same user leaves a row inside the window;
  2. a close whose row cannot be matched leaves its own row behind, so the table never empties.

Change

The lock protects other people, so ask about them instead of about rows:

  • SessionService::hasRecentSessionsOfOtherUsers($documentId, $userId, $graceSeconds = 60) — any non-guest session of a different user with contact inside the window;
  • ApiService::close() cleans up when no session is left or when no other user has been active. Consequence: closing one of several tabs of the same user now releases the lock too — which is the intended behaviour, the user is done with that document.

60 s because a live editor pushes every few seconds; that window separates "somebody is typing" from "these rows are leftovers".

Verified

Same device, same unpatched Notes app, with this change applied to a live instance:

POST /apps/text/session/<id>/close       200
PUT  /apps/notes/api/v1/notes/958914     200      ← rename goes through
oc_files_lock                            0 rows

Repeated: edit → close → rename, several times in a row, no lock left behind. php -l clean on both files.

Honest limits, and where I would like an opinion

  • No unit test. SessionService can be unit-tested with the existing mocks, and I will add one if you tell me which shape you want (the repo has tests/Unit/Service/LockServiceTest.php-style mocks, but no test for ApiService). I did not want to ship an unrun test.
  • The rule is a judgement call. A maintainer may prefer releasing on close plus an expiry that also covers app locks, or a shorter validity window, or tracking "the user has another session open" explicitly. I am attached to the outcome (no leaked lock after a normal close), not to this mechanism.
  • close() still needs the getUser() ?? getSessionUser() fallback from 599abe6 for a token-driven client to reach this code at all — that part is already on main.

Refs #9042


#AI-ASSISTED (DeepSeek V4.1 Flash 1017)

Assisted by DeepSeek:deepseek-v4.1-flash-1017

`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/<id>/close       200
    PUT  /apps/notes/api/v1/notes/<id>       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>
@PeterPablo
PeterPablo force-pushed the fix/release-lock-when-last-editor-leaves branch from e8998c6 to 218747e Compare November 18, 2026 20:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant