Skip to content

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

Closed

Description

@dezren39

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::version is a plain AtomicU64 load, collab.rs:268-270) and compares it to the version the client's Steps frame claims.
  • :1019-1028 — only afterwards does it take doc.steps.write().await and push the steps. The .await on 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:

  1. Clients A and B are both at version V (the normal state after a broadcast).
  2. A sends Steps(version=V, steps=[a]); B concurrently sends Steps(version=V, steps=[b]), where b was computed against the document at V (not V+[a]).
  3. A's handler checks V == V, then yields awaiting the steps lock (or B's handler runs first — either order works).
  4. B's handler also checks V == V and passes.
  5. 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 mismatch error 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

Activity

  1. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    priority/normalOrdinary priority (the default)
    source/creatorFiled by the new-issue-creator job
    task/triageQueue packet kind: verify, reproduce, and route this item
    app/idPackage/app: id (pkgs/ or apps/ in this repo)
    effort/sSmall: expected under 2 hours of work
    on Oct 31, 2026
  2. dezren39 commented on Oct 31, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · state: filed

    • Agent: new-issue-creator job (new-issue-creator-code-b)
    • Session / ID: new-issue-creator-code-b + run 2026-10-31T06:20Z (scheduled_for 01:20 CDT)
    • Found via: filed this run
    • Task: Filed this issue — collab Steps version check (collab.rs:1003) and step append/version bump (:1021-1033) are not atomic; two clients at the same base version can both pass and both append, silently diverging the document.
    • Expectation: effort/s — triage pins the atomicity shape (per-document lock spanning check+append+bump, or equivalent), then a fix with a two-concurrent-Steps regression test.
    • Links: issue body evidence in hidden_files/issue-code-collab-steps-race-body.md (goal workspace); queue packet packet-2968-triage.json
  3. dezren39 commented on Oct 31, 2026

    @dezren39
    MemberAuthor

    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.

  4. added
    work/assignedQueue worker lifecycle state
    queue/claimedQueue worker lifecycle state
    and removed on Nov 1, 2026
  5. 18 remaining items

  6. added and removed
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    on Nov 1, 2026
  7. dezren39 commented on Nov 1, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · picked up

    • Agent: repo-worker-code, OpenChamber scheduled lane
    • Session / ID: queue task 5489 (attempt 2/2)
    • Found via: queue claim; resuming the parked implementation
    • Task: Resume branch queue/5489-2968-steps-atomic at 32fe0bf6b; revalidate the regression test and create the PR for the existing fix. No competing PR exists.
    • Expectation: effort s — verified PR opened or parked with a precise resume condition before this shift ends.
    • Links: branch queue/5489-2968-steps-atomic · worktree /home/user/git/fleet/5489-2968
  8. added
    queue/needs-reviewQueue task finished and is awaiting review
    and removed
    queue/claimedQueue worker lifecycle state
    work/assignedQueue worker lifecycle state
    on Nov 1, 2026
  9. dezren39 commented on Nov 1, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · handed off for PR review

  10. dezren39 commented on Nov 3, 2026

    @dezren39
    MemberAuthor

    🤖 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.

  11. added a commit that references this issue on Nov 3, 2026
  12. dezren39 commented on Nov 3, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done (merged)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

app/idPackage/app: id (pkgs/ or apps/ in this repo)effort/sSmall: expected under 2 hours of workkind/bugSomething isn't working (prefixed form of the classic bug label)priority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobtask/triageQueue packet kind: verify, reproduce, and route this item

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions