Repository navigation
Description
Problem
The memory server orders writers from separate processes with a lock file, <memory file>.lock (withFileLock in src/memory/index.ts). #5560 (PR #5563) stopped a stale-lock breaker from deleting a live lock that replaced the one it judged: the breaker now renames the lock aside, checks by device and inode that it took the file it judged, and renames anything else straight back.
Three narrow hand-off windows remain. Each needs a second, independent event inside a gap of a few file-system calls, on top of a holder or breaker that has already stalled past the stale timeout, and the processes involved run as the same user, so these are correctness gaps, not a security boundary.
- A writer can acquire while a live lock is aside. When the breaker takes a successor's live lock by mistake, the lock path is free until the breaker renames it back. A third writer that creates the lock in that moment is overwritten by the restored lock and runs beside the successor (raised in review of fix(memory): never delete a successor's live lock when breaking a stale one #5563).
- A lock can be restored after its holder released it. If the successor finishes while its lock is aside, its release finds no file at the lock path and skips the removal. The breaker then restores a lock nobody holds. Its owner process is still alive, so on the same host it is not broken until it has gone unrefreshed for the stale timeout (30 seconds by default): later writes are delayed, none is lost (raised in review of fix(memory): never delete a successor's live lock when breaking a stale one #5563).
- Release compares, then deletes by path.
withFileLock's release, andreleaseHeldLocksat exit, read the lock's token and thenunlinkthe lock path. A holder that stalled past the stale timeout can pass the comparison, have its lock broken and re-taken, and then delete the new holder's lock. This predates memory: breakStaleLock can delete a successor's live lock, letting two writers overlap #5560, which cites the token comparison as the model.
Expected behavior
No interleaving of acquire, release and stale-lock breaking between processes running this version lets two writers hold the lock at once, or leaves behind a lock nobody holds.
Approach
To be decided; the options seen so far:
- Make the breaking guard (
<memory file>.lock.break) order every change of the lock path's occupant, not only breakers: a release takes it before comparing and deleting, and a writer that has just created the lock checks that no break is in progress before it runs, re-checking its own token once the guard is gone. - Or replace the lock file with an OS-level advisory lock where the platform offers one. This needs a dependency or a native call, so it is a larger decision.
Whatever is chosen must keep working next to an older server version sharing the same memory file, or say that it does not.
Acceptance criteria
- Deterministic tests (no timing dependence) for: a create between the breaker's two renames; a release between the breaker's two renames; a break and re-take between a release's comparison and its removal.
- The existing lock tests still pass.
Activity