Skip to content

updateTaskNode()'s unconditional rollback-on-failure can clobber a later queued write's result #2088

Closed

Description

@mstomar125

Found during

Implementing #2072 (updateTaskNode's OCC direct-hydration skip-while-editing guard). Adding a test mirroring #2069's "does not discard a genuinely queued second write" coverage (which locks in updateNode()'s equivalent guarantee) for updateTaskNode() failed, and the failure is unrelated to the direct-hydration branch #2072 fixed.

The bug

updateTaskNode()'s catch handler (packages/desktop-app/src/lib/services/shared-node-store.svelte.ts, in the PersistenceCoordinator.getInstance().persist(...) closure) does this on any write failure, OCC or not:

} catch (dbError) {
  ...
  // Rollback the optimistic update
  this.nodesSet(nodeId, existingNode);
  this.notifySubscribers(nodeId, existingNode, source);

  // If this is an OCC error, hydrate from authoritative current_node and notify
  if (occError) {
    ...

existingNode is the node snapshot captured at the start of this specific updateTaskNode() call, before its own optimistic write. Unconditionally writing it back into the store on failure means: if a second updateTaskNode() call (write B) landed and applied its own optimistic update while write A was still in flight, and write A then fails, this line overwrites the store with write A's own stale pre-edit snapshot — discarding write B's optimistic value (and, if B's real persist attempt resolves before A's catch runs, potentially discarding B's confirmed server state too).

updateNode()'s sibling catch handler avoids this entirely: it calls this.rollbackUpdate(nodeId, update), which only does bookkeeping (removes the failed update from pendingUpdates, rolls back the versions map entry, notifies subscribers with whatever is currently in the store) — it never calls nodesSet() and never touches node content. That is precisely why updateNode()'s OCC-related regression tests (#2069, does not discard a genuinely queued second write...) pass: there is no unconditional content rollback for a later write to race against.

Repro (removed from the #2072 PR, see that PR's test file history for the original draft)

  1. Seed a persisted task node.
  2. Write A: updateTaskNode(id, { status: 'in-progress' }, ...) — optimistic apply, persist starts executing (mode: 'immediate'), mocked backendAdapter.updateTaskNode delayed then rejects with a version-conflict error.
  3. Write B: updateTaskNode(id, { status: 'done' }, ...) submitted while A is still executing — collapses into queuedOperations, applies its own optimistic value (status: 'done') immediately.
  4. Wait for both to settle.
  5. Expected: the node ends up reflecting write B (queued write must not be silently discarded — same guarantee updateNode() already has). Observed: the node reverts to 'todo' (write A's own pre-edit existingNode snapshot), discarding write B's outcome.

Suggested fix

Make updateTaskNode()'s failure-rollback mirror updateNode()'s: stop calling this.nodesSet(nodeId, existingNode) unconditionally in the catch handler. Either route it through rollbackUpdate() (if compatible with updateTaskNode()'s update-tracking shape) or make it a no-op on content the same way, notifying subscribers with whatever the store currently holds rather than force-reverting to this call's own captured pre-edit snapshot. Add a regression test mirroring updateNode()'s #2069 coverage once fixed.

Scope note

Out of scope for #2072, which is specifically about the OCC direct-hydration branch (if (currentNode) { this.nodesSet(nodeId, currentNode); ... }) gaining the same decideRemoteUpdate skip-while-editing guard updateNode()'s equivalent branch has (#2071). This finding is about the unconditional rollback line that runs before that branch is even reached, on every failure (OCC or not) — a different code path, same store method.

Activity

  1. mstomar125 commented on Aug 14, 2026

    @mstomar125
    ContributorAuthor

    Fixed in #2095.

    updateTaskNode()'s catch handler no longer unconditionally does this.nodesSet(nodeId, existingNode) on failure — it now notifies subscribers with whatever the store currently holds instead, mirroring updateNode()'s rollbackUpdate() (which never touches node content on failure).

    Added a regression test (verified to fail against the pre-fix code) proving a genuinely queued second write's still-in-flight optimistic value survives the first write's failure. Also fixed a pre-existing test in shared-node-store-skip-while-editing.test.ts whose assertions/comment had documented the old unconditional-revert behavior as expected, and filled in the it.todo() PR #2089 left as an explicit placeholder for this fix.

  2. added 2 commits that reference this issue on Aug 14, 2026
    32716bc
    df27df2
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions