Repository navigation
Description
Describe the bug
When a document is opened in Text, the app acquires a lock through files_lock. If the editing session ends abnormally — tab closed without a clean teardown, browser crash, session expiry — the lock is never released.
The failure is silent. A desktop client holding a newer local copy retries the upload and gets HTTP 423 Locked forever, with no actionable error surfaced in the web UI or in the client. Files whose local copy happens to be unchanged simply stop syncing in both directions, with no visible sign at all.
Orphan rows are also left behind in oc_text_sessions, with last_contact frozen at the moment the session died. Every stale lock we found had exactly one matching stale session, which suggests both are leaked by the same teardown path.
To Reproduce
- Open a Markdown file in Text.
- Kill the session abruptly (close the tab while editing, or let the session expire).
- Inspect
oc_files_lock: a row remains, withowner = "Text"and a negativettl. - On a desktop client syncing that folder, modify the same file locally.
- Watch the web server access log:
PUT /remote.php/dav/files/<user>/<path>→423, repeating indefinitely.
Expected behavior
The lock is released when the editing session ends, or at the latest when the stale session is garbage-collected.
Observed on a production instance
- 8 stale locks accumulated over 3 weeks, all
owner = "Text", the oldest 20 days old. - Each had a matching orphan row in
oc_text_sessions. - One file had a newer local copy: its desktop client issued a
PUTevery 2 hours and received423each time, for 19 consecutive days. - The 7 other files were silently out of sync — discovered only while investigating the first one.
Why nothing cleans this up
files_lock ships a cleanup job, OCA\FilesLock\Cron\Unlock, but with default settings it is a no-op:
ConfigServicedefaultslock_timeoutto'-1';FileLock::ETA_INFINITE === -1;LockService::getDeprecatedLocks()returns[]when the timeout equalsETA_INFINITE, so the job deletes nothing.
Setting a finite timeout re-arms the job and is an effective workaround:
occ config:app:set files_lock lock_timeout --value=1440
This bounds the damage but does not fix the leak: the lock is still orphaned, and the file stays unsyncable until the job runs.
Possible second issue, in files_lock
Even with a finite lock_timeout, expiry does not track activity. LocksRequest::update() — the "extending existing lock" path — writes token, ttl, user_id, owner and scope, but not creation, while LocksRequest::getLocksOlderThan() filters on creation. A lock actively extended by an ongoing session can therefore be purged based on its original creation time, so a long editing session may lose its lock mid-edit. This is why we chose a deliberately large timeout above.
Happy to split this into a separate issue against files_lock if you prefer.
Server details:
- Nextcloud version: 34.0.3 (also observed on 32.x and 33.x before upgrading)
- PHP Version: 8.4
- Database: PostgreSQL 17.10 and MariaDB 11.8.6 — two separate instances, both affected
- files_lock: 34.0.1
Client details:
- OS: Debian (server), Linux desktop
- Desktop sync client: 33.0.7
- Browser: Firefox
Activity
fuckoffwiththisshitistaken commentedon Sep 8, 2026
thanks for reporting this issue, my workaround was to disable the text app.
Fix Memories indexing and stale Text file locks
sallyhaj commentedon Oct 1, 2026
It seems this issue affects Notes to sync between devices, make locks.
When I edit/create a note via Nextcloud website, after that I opened my Android device/Notes app, I get this error:
Nextcloud server 35.0.0.10,files_lock 35.0.0,Notes Android 35.0.0.unsent edit to the same note.
UT /apps/notes/api/v1/notes/<id>returns 423 withOCP\Lock\ManuallyLockedException, and theapp only shows
{"errorType":"Exception"}.occ text:reset <fileId>clears it at once. Settingfiles_lock lock_timeoutto 30 makesthe lock expire by itself, but the device still fails for up to 30 minutes.
sallyhaj commentedon Oct 15, 2026
That #9355 fixes only the Files-list icon, not the leaked lock, so the issue should stay open. This matters because maintainers might close #9042 thinking #9355 fixed it.
LucaBoschetto commentedon Oct 21, 2026
Confirming this on Nextcloud 34.0.4 (AIO), Text 8.0.0, files_lock 34.0.1, Notes 6.1.0, and adding one finding: it is not limited to crashed tabs. A normal, clean tab close never releases the lock either.
Reproduction (watching
oc_text_sessionsandoc_files_lockwhile doing it):Textlock appear.423 Locked(OCP\Lock\ManuallyLockedExceptionin the log).Why, as far as I can read the shipped bundles:
beforeunloadhandler issaveBeforeUnload, registered only while the document is dirty. It sends anavigator.sendBeaconto/apps/text/session/{id}/save(withrequesttokenin the JSON body).POST /apps/text/session/{id}/closeis only sent through a normal request when the editor component is torn down in the page. Nothing sends it onbeforeunload/pagehide, so a closed tab behaves exactly like a crashed one.SESSION_VALID_TIME(5 min):ApiService::close()unlocks only ifgetActiveSessions()is empty, and the orphan still counts as active. We hit this during testing: a clean in-page close 4 min 54 s after a closed tab did not unlock.Cleanupjob never unlocks, and files_lock'sUnlockjob is a no-op with the defaultlock_timeoutof -1. Orphaned sessions that hold steps survive up to 90 days (removeOldSessions). We found Text locks dating back a month.Possible fixes: send the close as a beacon on
pagehide(the save beacon shows the CSRF token can travel in the body), and/or have the cleanup job release Text's lock for documents with no active session.MK796 commentedon Nov 11, 2026
I can confirm that this issue is still occurring on a current Nextcloud AIO installation, despite the iOS fix from nextcloud/ios#4342 already being included.
I have been experiencing this intermittently since April 2026. Before then, the same workflow released the file locks correctly.
Observed behavior
After opening a Markdown or plain-text file in the Nextcloud iOS app and subsequently leaving or closing the editor, the file can remain “Locked by Text” indefinitely. The exact client-side exit path of the latest occurrence was not captured.
Environment
35.0.2.2(AIO)9.0.135.0.18.4.2718.635.2.2Read-only database findings
There are currently four stale locks owned by the
textapp:text/markdowntext/plainttl = -60ttl = 0Both non-positive TTL values are treated as infinite by
files_lock.Each lock has exactly one corresponding stale row in
oc_text_sessions. The session'slast_contactand the creation time of its lock differ by no more than 102 seconds in all four cases. This indicates that the Text sessions and their locks were abandoned together.files_lock.lock_timeoutis not explicitly configured on this instance, so the default value of-1applies. Consequently, the cleanup job does not expire these locks.Correlation with iOS 35.2.2
The newest lock was created at
2026-11-10 16:58:40 UTC. At16:59:18 UTC, the Nextcloud log contains aPOSTto the Text API with this user agent:This is a timestamp correlation rather than a complete client-side request trace. However, it confirms that the latest occurrence happened during an iOS
35.2.2Text session. This release already contains nextcloud/ios#4342, so that client-side fix does not prevent every path that can leave a Text session and its lock behind.No file paths, file IDs, user IDs, session tokens, or lock tokens are included in this report.
Please let me know if any additional privacy-safe diagnostics from this instance would be useful.
PeterPablo commentedon Nov 14, 2026
The caller does close the session — and the server answers 403, so the lock survives
Adding a datapoint that points at a different teardown failure than "the tab vanished", because it changes where the fix belongs.
Environment: Nextcloud 35.0.2 (Docker, Apache),
text+files_lockshipped versions, Nextcloud Notes for Android (F-Droid,it.niedermann.owncloud.notes35.0.0) as the only client. The Notes app opens a note through Direct Editing, which starts a Text session in its WebView.Server access log for one note, one afternoon (client is the Notes app in every line):
oc_files_lockafterwards (unchanged by the failed closes):type = 1isILock::TYPE_APP, the rowapps/text/lib/Service/LockService::lock()creates with ownerApplication::APP_NAME. Five such rows had accumulated over six days on.md/.txt/.htmlnotes; none was ever released.Why the close returns 403 — the same client gets 200 on
sync,pushandsave(95/59/16 calls that day), so this is not CSRF and not a client that fails to authenticate. Onlyclosefails, and the reason is in the controller:with
SessionMiddleware::afterException()mappingInvalidSessionException→Http::STATUS_FORBIDDEN. Thecloseendpoint is the only session endpoint that requires anIUser:sync/push/saveare satisfied by the document session token plusloginSessionUser(). A client that drives a document session by token — which is exactly what a mobile app does — therefore cannot close it, and the lock is never released.Two consequences worth separating, because they need different fixes:
closeshould not require a logged-in user. The session row already carries the owner (oc_text_sessions.user_id), so the user can be derived from(documentId, sessionId, sessionToken)— the token is the authorization. If no session matches,closeSession()already swallowsDoesNotExistExceptionand could simply return 204/200 instead of 403; a teardown call is idempotent by nature.FileContext::cleanup()is only reached viaApiService::close()→getActiveSessions() === 0. A stale session row (there were 11 of 15 older than an hour here, withlast_contactfrozen — matching your note about orphaned sessions) keeps that count non-zero, so even a successful close cannot unlock. Releasing the lock from theSession::closepath, plus an expiry that also covers app locks, would make the outcome independent of the client's manners.Reproduced 3/3 with the Notes app; not reproduced from the browser, which sends the request token and has a user session.
Workaround running here meanwhile: a systemd timer that unlocks
type = 1locks owned byTextolder than 45 minutes viaocc files:lock --unlock <fileid> <storage-owner>— deliberately narrow, because it deletes state. Not offered as a fix.The second point in that list (a successful close still not releasing the lock) turned out to be a third defect; it is in my later comment below and now has a draft PR: #9603.
#AI-ASSISTED
PeterPablo commentedon Nov 14, 2026
After the fixes: the same note, an hour later
Following up on my datapoint above. Three separate defects were involved, and the third one only became visible once the first two were patched — which is why the loop looked identical from the client each time.
1.
close()returning 403 — fixed onmainby599abe6(already noted). Backported here to the shipped 9.0.1, then:2. That 500 was
files_lock, not text:LockWrapper::rename()checksPERMISSION_UPDATE & PERMISSION_READ=2 & 1= 0, andcheckPermissions(…, 0)returnsfalsefor a locked path, so a rename never reached the branches that raiseManuallyLockedException. Reported separately: nextcloud/files_lock#1437. With that fixed the failure became an honest 423 Locked.3. With the close succeeding and the rename answering 423, the lock was still there — and this is the text-side bug this issue is about:
ApiService::close()only cleans up the context whengetActiveSessions()is empty, and that counts every session row younger thanSESSION_VALID_TIME(5 min). One note had six rows, one per reload of the editor — all the same user, the newest 13 s old when its own close ran. So the close succeeded, deleted its own row, and still saw "an active session", skippingcontext->cleanup()→ the lock survived. Note that a close whose row cannot be matched leaves its row behind too, so the same gate fails for a second reason.The rule that fixed it, and that I would suggest for upstream: the lock protects other people, so ask whether another user is still active — not whether any row is left:
60 s because a live editor pushes every few seconds — that window separates "somebody is typing" from "these rows are leftovers". A maintainer may prefer another mechanism (release on close plus an expiry that also covers app locks); I am not attached to this shape, only to the outcome.
Result on the same device, same app, unpatched client:
Rename after an edit now works repeatedly, and no lock is left behind.
Opened as a draft PR: #9603 — two helpers and one condition. It is a draft on purpose: the rule ("nobody else has been active in the last 60 s") is a judgement call, and I would rather hear whether that shape fits before polishing tests around a mechanism you may want to replace — the alternative I can see is releasing on close plus an expiry that also covers app locks.
#AI-ASSISTED