Skip to content

storage: no cross-process writer exclusion — a second writer corrupts the session file (live repro) #475

Closed

Description

@ajianaz

Summary

JsonlStorage guards against stale writes only with an in-memory CAS (expected_seq vs self.state.seq, storage.rs:881). Nothing stops a second process from appending to the same session file. docs/architecture.md:171 says "CAS on state.seq protects against double-drive (e.g. resume + host race) — only one wins" — that holds inside one process only.

Live repro (develop @ 438ad0d, i.e. post-v0.9.0 with an [Unreleased] section; workspace version 0.9.0; Linux aarch64; mock OpenAI-compatible server, no real provider)

  1. tole run "first turn" --sessions-dir S -> session <id>, 5 records, last seq 5.
  2. Process A: tole chat --resume <id> --sessions-dir S, send "from A, message 1" -> last seq 10.
  3. Process B (while A idles): tole resume <id> "from B" --sessions-dir S -> rc 0, last seq 15.
  4. Process A sends "from A, message 2" -> rc 0, no error; it appends with its stale in-memory seq, so seq 11-15 are written a second time.
  5. tole status <id> and tole resume <id> ... -> rc 1:
    tole: replaying session log: corrupt session file: line 11: seq 11 not strictly increasing (last 15)

Result: 20 records, duplicated seq 11-15, session can no longer be opened. open_with repair only truncates a torn EOF tail, so this is not self-healing.

Impact

Any two writers: a tole serve / ACP / MCP daemon holding a live session plus a CLI tole resume (the global session index, #439, lists sessions across projects), or two terminals. Only chat + resume was reproduced; the other combinations share the same commit path.

Inferred from source, not reproduced: compact() renames a fresh file over the live path, so a stale second handle would append to the unlinked inode (silent loss).

Proposal

  • Advisory exclusive lock for the lifetime of every writable handle (File::try_lock, stable since Rust 1.89; the workspace has no rust-version, CI uses stable; check the Android/mobile profile). open_read_only stays lock-free.
  • The lock must survive compact(): it renames jsonl.tmp over the live path, and an advisory lock on the old inode is gone after that. Reproduced with two real processes and File::try_lock: after the rename a second process gets the lock; locking the temp file before the rename, or holding a sidecar <id>.jsonl.lock that is never renamed, keeps it refused.
  • Take the lock before the torn-tail repair in open_with (a writable open truncates, and another process may be mid-append).
  • Belt and braces: before each append compare the file length with the last known good length (one fstat, negligible next to the ~5 ms fdatasync per commit) and fail with a stale-handle error instead of appending.
  • Error names the holder ("session is open for writing by pid N") instead of a generic failure.
  • Separate follow-up (own issue): tole session recover <id> that copies the longest valid prefix into a new session. fx ships fx session recover and fx doctor reports session damage.

Acceptance

  • E2E with two REAL processes (the repro above): the second writer gets a clear error; no duplicate seq in the file; status and resume work afterwards.
  • Read-only commands (status, sessions) succeed while a writer holds the lock.
  • Lock is released on crash/SIGKILL (OS lock); the existing E5 crash-resume tests still pass.
  • serve / ACP / MCP faces: a CLI tole resume against a session held by the daemon fails with the holder message instead of corrupting.
  • docs/architecture.md section 9 corrected.
  • A lock held across compact(): a second process is still refused after the rename.
  • The torn-tail repair runs only while the lock is held.
  • Both feature profiles (--no-default-features) build and test.

Found by

Competitor research 2026-11-02 (fx durable-session TLA+ model Fencing.tla: "a process replaced while it ran is refused at its next write"), then a live repro against develop @ 438ad0d. The repro above is complete and needs only a mock OpenAI-compatible server.

Activity

  1. added
    bug
    reliability
    on Nov 2, 2026
  2. changed the title [-]storage: no cross-process writer exclusion \u2014 a second writer corrupts the session file (live repro)[/-] [+]storage: no cross-process writer exclusion — a second writer corrupts the session file (live repro)[/+] on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugreliability

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions