open
https://gitlab.synchro.net/main/sbbs/-/work_items/1342
## Summary
On Windows, with the Synchronet tree on an SMB share served by Samba, a shared byte-range lock on a `user.tab` record can remain on the server after it has been unlocked. It stays until the handle that took it is closed. While it is there, readers still get the record but every exclusive lock on it fails.
The JS `User` object keeps `user.tab` open for its whole lifetime, so when its handle is the one left holding the lock, the record stays locked against writers for as long as the script's context lives. In the web server that includes the session's own `http_logoff()`, which runs before the context is destroyed: it retries `lockuserdat()` 200 times (about 106 s) and fails with `USER_LOCK_ERROR`. Every other login or logout for the same user waits alongside it.
## Symptom
vert.synchro.net, web server built from 36867a9bbf (2026-11-11), 2026-11-11 22:23-22:25: three webv4 requests from one logged-in user (user #1) logged in or out within the same second. Then:
- a page request waited 109 s in login and logged `!Error -104 writing user data for user #1`
- an event-stream session waited 108 s in logout and logged `!ERROR -104 in logoutuserdat`
- a third session's logout waited 91 s, and three more queued requests logged in at the moment the first two gave up
In the browser, every page click for that user spun for one to two minutes.
This is the same symptom b643ac2bca (2026-11-09) set out to fix. That commit made `logoutuserdat()` unlock before closing; the stall recurred with it in place.
## Reproduction (no Synchronet code involved)
takes the locks with `LockFileEx(LOCKFILE_FAIL_IMMEDIATELY)`, as `rdlock()` and `xp_lockfile()` in `xpdev/filewrap.c` do. Run it with `-Dir` set to a local directory and then to a directory on the share. Client: Windows 11. Server: Samba 4.22 on Linux.
Two handles A and B on one file, each taking a shared lock on the same 1000 bytes, followed by an exclusive lock attempt from a third handle:
| Sequence | Local disk | SMB share |
|---|---|---|
| A lock, B lock, A unlock, B unlock, then exclusive probe | granted | **fails, error 33** |
| A lock, B lock, B unlock, A unlock, then probe | granted | granted |
| A lock, A unlock, B lock, B unlock, then probe | granted | granted |
| failing sequence, then close B, then probe | | still fails |
| failing sequence, then close A, then probe | | granted |
- Every lock and unlock call in the failing sequence returns success.
- A further `UnlockFileEx()` on either handle returns error 158 (not locked), so the client cannot release the leftover lock except by closing A.
- The result is the same with two read-only handles, two read-write handles, one of each, or two read-only handles with different share modes.
A threaded version (N writers taking exclusive locks and N readers taking shared locks on one record, one operation each per round, with the `lockuserdat()` retry timing):
| Readers | One reader keeps its handle open | Result on the share | |---|---|---|
| 2 or 3 | yes | every writer gave up after 106-107 s, in 4 of 4 runs |
| 1 | yes | no stall in 379 rounds |
| 2 or 3 | no | no stall in 300+ rounds, longest wait 0.2 s |
Whether the Windows SMB client or Samba mishandles the out-of-order unlock is not determined.
## Where it bites in Synchronet
- `readuserdat()` takes a shared lock (`rdlockuserdat()`) for every read that isn't followed by a write, so two requests reading the same user's record overlap routinely.
- With a handle that is closed straight after the read (`getuserdat()`), the leftover lock lasts milliseconds.
- `js_user.cpp` keeps a read-only handle in `user_private_t::file` until the object is finalized or `User.close()` is called (4176df9717, 2018-06-07). That is the handle that turns the leftover lock into a 106 s stall.
- The read loops that hold one handle across many records (`str.cpp` user list, `main.cpp`) can block a writer for the length of the loop.
`rdlock()` is only used for `user.tab`.
## Proposed fix
Have the JS `User` object open, read and close the user base on each uncached read, instead of keeping the handle. A change to that effect compiles under MSVC but has not been deployed or tested.
The Linux counterpart of this problem (a CIFS server lock left behind by a failed OFD lock) was fixed in 36867a9bbf (2026-11-11).
*Authored by Claude (Claude Code), on behalf of @rswindell*
--- SBBSecho 3.38-Linux
* Origin: Vertrauen - [vert/cvs/bbs].synchro.net (1:103/705)