Skip to content

Skip variants trait-restore when session was explicitly stopped#116

Merged
obj-p merged 1 commit intocli-mcp-parityfrom
race-condition-audit
Apr 16, 2026
Merged

Skip variants trait-restore when session was explicitly stopped#116
obj-p merged 1 commit intocli-mcp-parityfrom
race-condition-audit

Conversation

@obj-p
Copy link
Copy Markdown
Owner

@obj-p obj-p commented Apr 15, 2026

Summary

Wave 1-C from the deferred-items list on #113: race-condition audit across every MCP handler.

Audit findings

No new bugs found. PreviewSession is an actor so within-session state transitions are serialized, and the preview_snapshot hole for missing sessions is already fixed in #112.

One defensive improvement landed

When a concurrent preview_stop fires mid-variants-loop, the trait-restore-at-end block would fire against a now-stopped session, producing a misleading "Warning: failed to restore original traits" message for a user who explicitly asked for the stop. Skip the restore when the session is no longer in the registry (iosState for iOS, App.host.allSessions for macOS). No user-visible change for the happy path; the spurious warning goes away for the concurrent-stop case.

Documented caveat

Added a doc comment on handlePreviewVariants spelling out the remaining concurrent-modification limitation: a second client mutating the same session via preview_configure / preview_switch while variants is mid-loop will interleave its trait change into our capture stream. The daemon intentionally does not hold a per-session lock across tool calls (more invasive architectural change); callers that want deterministic variants should own the session for the duration.

Test plan

  • swift build
  • swift test --filter VariantsCommandTests — 14 integration tests pass unchanged (~60s)

🤖 Generated with Claude Code

Follow-up audit from PR #113's deferred items. Walked every MCP
handler looking for concurrent-modification races. Summary:

No new bugs found — PreviewSession is an actor so within-session
state transitions are serialized, and the `preview_snapshot` hole
for missing sessions is already fixed in #112.

One defensive improvement worth landing: when a concurrent
`preview_stop` fires mid-variants-loop, the trait-restore-at-end
block would fire against a now-stopped session, producing a
misleading "Warning: failed to restore original traits" message
for a user who explicitly asked for the stop. Skip the restore
when the session is no longer in the registry (iosState for iOS,
App.host.allSessions for macOS). No user-visible change for the
happy path; the spurious warning goes away for the concurrent-stop
case.

Documented the remaining concurrent-modification caveat on
`handlePreviewVariants`: a second client mutating the same session
via preview_configure / preview_switch while variants is mid-loop
will interleave its trait change into our capture stream. The
daemon intentionally does not hold a per-session lock across tool
calls (that's a more invasive architectural change); callers that
want deterministic variants should own the session for the
duration.

All 14 variants integration tests pass unchanged.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@obj-p obj-p merged commit ca8c543 into cli-mcp-parity Apr 16, 2026
@obj-p obj-p deleted the race-condition-audit branch April 16, 2026 00:01
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.

1 participant