Skip to content

fix: lock the session registry across processes and stop stale overwrites (#55) - #140

Merged
TMHSDigital merged 1 commit into
mainfrom
fix/session-lock-55
Oct 24, 2026
Merged

TMHSDigital merged 1 commit into
mainfrom
fix/session-lock-55

Conversation

@TMHSDigital

Copy link
Copy Markdown
Owner

Closes #55.

Bugs fixed

  1. Stale overwrite (worse than the race in the issue). persist() merged with disk but let every in-memory record win, including another instance's records loaded at boot and never touched. If A loaded B's live recording and B then stopped it, A's next unrelated write flipped it back to recording. Now a dirty set means only records this instance created/changed override disk; the in-memory view is refreshed from what was written. A test reproduces it and failed before the fix (expected 'recording' to be 'stopped').
  2. No cross-process lock. Read-merge-write now runs under sessions.json.lock (O_EXCL, contents {pid, at}). A stale lock (owner dead, or >10 s old) is taken over. After 2 s of contention it writes anyway with a stderr warning rather than failing a recording.
  3. Windows EPERM crash. Found by the new multi-process test: renaming the temp file over sessions.json fails while another process has it open. load() now reads under the lock too, and the rename retries briefly on EPERM/EBUSY/EACCES.

Debugging notes (two more lock bugs, both caught by the stress test)

  • A just-created, still-empty lock parsed as "no pid", which isAlive() reported as dead, so it was stolen. A lock with no readable pid is now judged by age only.
  • On Windows a lock its holder just deleted, while another process had it open, lingers as delete pending, and openSync('wx') fails with EPERM, not EEXIST. The old code took any non-EEXIST error as "can't lock here" and wrote unlocked, silently. Before the fix, 6 of 10 runs lost a record mid-sequence; after it, 0 of 15.

Tests

  • Stale-copy regression; lockfile released after write and a dead-owner lock taken over; isLockStale rules (dead / old / unreadable-but-fresh).
  • Multi-process stress: 6 node --import tsx workers × 25 records into one registry; asserts all 150 present and no leftover lock. Passes 15/15 locally on Windows.
  • Full suite 232 pass; live capture harness 10/10.

Version 0.14.3.

🤖 Generated with Claude Code

…ites (#55)

persist() let every in-memory record win over disk, so a copy of another
instance's recording loaded at boot could undo that instance's later stop.
Only records this instance changed now override the disk copy.
Read-merge-write runs under an O_EXCL lockfile with stale-lock takeover,
reads take the lock too, and the temp-file rename retries on Windows
sharing errors. A delete-pending lock (EPERM on Windows) is treated as
busy instead of as permission to write unlocked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@TMHSDigital
TMHSDigital merged commit eac223f into main Oct 24, 2026
8 checks passed
@TMHSDigital
TMHSDigital deleted the fix/session-lock-55 branch October 24, 2026 20:46
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.

sessions.json has no cross-process locking — concurrent instances can still interleave writes

1 participant