Repository navigation
Description
Problem
In the collab WebSocket handler, the Steps version check and the step append are not atomic, so two clients connected to the same document can both pass the version check with the same base version and both have their steps appended — silently diverging the shared document instead of one of them getting the version-mismatch error the protocol relies on.
Evidence (main ed59bc6d, pkgs/id/src/web/collab.rs)
:1002-1003— the handler reads the current version:let current_version = doc.version();(Document::versionis a plainAtomicU64load,collab.rs:268-270) and compares it to the version the client's Steps frame claims.:1019-1028— only afterwards does it takedoc.steps.write().awaitand push the steps. The.awaiton the steps lock sits between the check and the append, so the task can yield there.:1030-1033— the version is bumped only after the push:doc.version.fetch_add(steps.len() as u64, SeqCst). The version and the steps vector are two separate synchronization objects; nothing holds a lock across check → append → bump.- Each socket is handled by its own task (
handle_collab_socket), and the server performs no transform/rebase of incoming steps: steps are stored verbatim (:1023-1026) and broadcast verbatim (:1036-1040). The version equality check is the only guard against a client submitting steps computed against a stale document state — ProseMirror's collab protocol depends on exactly that rejection.
Interleaving that breaks it:
- Clients A and B are both at version V (the normal state after a broadcast).
- A sends Steps(version=V, steps=[a]); B concurrently sends Steps(version=V, steps=[b]), where
bwas computed against the document at V (not V+[a]). - A's handler checks V == V, then yields awaiting the steps lock (or B's handler runs first — either order works).
- B's handler also checks V == V and passes.
- Both append; the version advances by len(a)+len(b). B's steps are now recorded as if they followed A's, and every other client replays them in that order via the broadcast Update and the join-time catch-up (
:791-806). No client ever sees a version mismatch, so no client rebases or reconnects; the replicas have applied non-commuting steps in an order one author never intended.
Verification this run: code-read verification of the handler on current main (the check/append/bump sequence and the per-socket task structure are as cited above; no lock or atomic spans them). The race needs no exotic timing — any two editors typing at the same moment on the same document can hit the await window, and the window widens under load (steps lock contention with the catch-up read at :792).
Why it matters
Silent divergence is the worst collab failure mode: the server reports success (Ack with the new version, :1046-1053) to both authors, so the corruption is only discovered later as mangled text on save. The codebase already treats the neighbouring failure (broadcast lag) as a desync worth an explicit error + reconnect (:896-906); this path produces the same desync with no signal at all.
Acceptance criteria
- Steps acceptance is atomic per document: the version check, the steps append, and the version bump happen under one per-document lock (or an equivalent compare-and-swap discipline), so a second Steps frame with the same base version is rejected with the existing
Version mismatcherror once the first has been applied. - A regression test drives two concurrent Steps submissions with the same base version against one document and asserts exactly one is Acked and the other gets the version-mismatch error, and that the stored steps/version reflect only the winner.
- Single-client behaviour is unchanged: sequential Steps at the correct version are Acked, broadcast, and replayed by the join-time catch-up exactly as before.
- The fix does not hold the steps lock across network sends (broadcast/Ack stay outside the critical section, or the ordering is argued in the PR).
Dedup / related
- id(web): image uploads can dispatch against a destroyed editor view #2799 (open) is the Init-frame version (
Initalways carries version 0 while the document replays to a later version) — a different frame and a different defect; this issue is about two live Steps submissions racing. - id(web): collab handleMessage trusts MessagePack frame shape — a null frame throws out of onmessage, and a field-less INIT silently initializes an empty editor at an undefined version #2824 (open) is the TypeScript client's trust of MessagePack frame shape — client-side, distinct.
- id(web): collab WebSocket URL interpolates the raw filename unencoded — names with / ? # never connect, and a %20 name silently opens a different file #2830 (open) is the collab WS URL encoding — distinct.
- Search of open + closed issues for "collab steps version race / concurrent steps / version mismatch" found no existing issue covering this interleaving.
Activity
dezren39 commentedon Oct 31, 2026
🤖 Agent note · state: filed
dezren39 commentedon Oct 31, 2026
Cross-link: sibling filed in the same run — #2969 (collab step history grows without bound; no cap or compaction). Same handler, distinct defect: #2968 is the Steps acceptance race, #2969 is retention.
18 remaining items
dezren39 commentedon Nov 1, 2026
🤖 Agent note · picked up
queue/5489-2968-steps-atomicat32fe0bf6b; revalidate the regression test and create the PR for the existing fix. No competing PR exists.queue/5489-2968-steps-atomic· worktree/home/user/git/fleet/5489-2968dezren39 commentedon Nov 1, 2026
🤖 Agent note · handed off for PR review
pr_opened)queue/5489-2968-steps-atomicat 5d21029. It merges current main and implements the per-document atomic Steps check/append/version bump plus the deterministic same-base regression. Issue id(web/collab): Steps version check and append are not atomic — two clients at the same base version both pass, both append, and the document silently diverges #2968 remains open pending PR review and merge.git diff --checkpassed;cargo test --features web collab::was still running at handoff. Fresh PR check view showed only CodeRabbit neutral; no green CI result yet.queue/5489-2968-steps-atomicdezren39 commentedon Nov 3, 2026
🤖 Agent note · handed off
Execution-lane assignment for task 7439 is
opencode: this shift hands the task to the laptop opencode claimant, which launches a session bound to task 7439. This note is the handoff record, not a claim of in-flight implementation work.Merge pull request #3683 from developing-today/queue/5489-2968-steps-…
dezren39 commentedon Nov 3, 2026
🤖 Agent note · done (merged)
queue/5489-2968-steps-atomic) merged current main,apply_stepsresolved to delegate to the atomicDocument::try_apply_stepswhile keepingMAX_STEPS_PER_MESSAGE, and the PR was merged asddd7dd6ee0.cargo test --features web --lib collab::— 33 passed, 0 failed (includestest_try_apply_steps_concurrent_same_base_version,test_try_apply_steps_sequential_versions,test_apply_steps_rejects_oversized_batch_without_mutation).git diff --checkpass. Integration test crates (documents,crash,bridges, …) fail to compile on the merged head withE0063 missing field checkpoint in ChainExtin files this branch does not touch — pre-existing main drift, unrelated to this change.id,nix citrigger only on push tomain), so verification is the targeted local suite above.queue/5489-2968-steps-atomic