Skip to content

Make NodeService MCP server start and stop atomic #2072

Closed

Description

@shanselman

Lane 2 of 5 for #2071. Filed by a Copilot agent.

Problem:

  • NodeService.StartMcpServer (~773-839) checks and then sets _enableMcpServer/_mcpServer with no lock, and its catch sets _mcpServer = null unconditionally.
  • StopMcpServerAsync swaps the fields with no lock.
  • SetMcpEnabled holds _clientLock, and its comment wrongly says the stop is lock-protected.
  • AttachClient calls RegisterCapabilities() (which ends in StartMcpServer) after releasing _clientLock, on a threadpool thread from the NodeConnector ClientCreated event.

Failure: saving settings with node mode and MCP both on reconnects the node and calls SetMcpEnabled at the same time. That can start two servers, where the losing attempt nulls the winner, or it can lose a disable. Either way an untracked MCP listener, including system.run, keeps running and turning MCP off cannot stop it until restart.

Fix:

  • Add a dedicated MCP lifecycle lock around start and stop.
  • Re-check the enabled flag inside the lock.
  • In the catch, clear the field only if it still holds this attempt.
  • Correct the comment.

Done when:

  • a regression test covers concurrent start/stop
  • the required validation passes
  • there is winnode MCP proof that enable/disable still works
  • a rubber-duck review is done

Activity

  1. self-assigned this
    on Nov 8, 2026
  2. added
    lever
    on Nov 8, 2026
  3. added
    clawsweeper:intake:bug
    clawsweeper:intake:needs-environment
    clawsweeper:intake:needs-repro
    P2
    clawsweeper:needs-security-review
    clawsweeper:no-new-fix-pr
    clawsweeper:source-repro
    impact:security
    issue-rating: 🦞 diamond lobster
    and removed
    clawsweeper:intake:bug
    clawsweeper:intake:needs-environment
    clawsweeper:intake:needs-repro
    on Nov 8, 2026
  4. clawsweeper commented on Nov 8, 2026

    @clawsweeper

    Codex review: this still needs some work.

    Summary
    Current main still permits competing MCP starts and disable-during-startup races; this collaborator-authored issue remains necessary.

    Reproducibility: Source provides a concrete interleaving: startup passes its enabled check, disable observes no published server and returns, then startup publishes the listener; a separate competing-start failure can clear another attempt's server field. No live reproduction, build, or test was run.

    Next step
    Keep this security-sensitive repair with the audit owner: establish failing concurrent-start and disable-during-startup regressions, serialize lifecycle ownership, and verify MCP discovery, invocation, and disable through the shipped app.

    Security
    Needs attention: The race can leave authenticated MCP capabilities reachable after MCP is disabled.

    Review details

    Best possible solution:

    Give enabled intent, listener publication, failed-attempt cleanup, and shutdown one synchronized lifecycle owner.

    Do we have a high-confidence way to reproduce the issue?

    Source provides a concrete interleaving: startup passes its enabled check, disable observes no published server and returns, then startup publishes the listener; a separate competing-start failure can clear another attempt's server field. No live reproduction, build, or test was run.

    Is this the best way to solve the issue?

    A dedicated lifecycle gate fits the defect if every startup and shutdown entry point participates, failed attempts cannot clear another listener, and asynchronous draining occurs without blocking capability or client locks.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • The Windows listener-conflict manifestation has not been exercised in this read-only review.
    • Lifecycle synchronization must preserve asynchronous request draining and avoid lock-order inversions with client and capability registration.

    Codex review notes: model internal, reasoning medium; reviewed against e70292487958.

    Label changes

    Label changes:

    • add P2: Source establishes a limited local lifecycle race, without evidence of an active exploit or widespread broken workflow.
    • add impact:security: Disabling MCP can fail to revoke its listener, leaving authenticated access to sensitive capabilities available.
    • add issue-rating: 🦞 diamond lobster: Current issue advisory state selects this label.
    • add clawsweeper:source-repro: Current issue advisory state selects this label.
    • add clawsweeper:no-new-fix-pr: Current issue advisory state selects this label.
    • add clawsweeper:needs-security-review: Current issue advisory state selects this label.

    Label justifications:

    • P2: Source establishes a limited local lifecycle race, without evidence of an active exploit or widespread broken workflow.
    • impact:security: Disabling MCP can fail to revoke its listener, leaving authenticated access to sensitive capabilities available.
    Evidence reviewed

    Security concerns:

    • [medium] MCP disable does not reliably revoke the listener — src/OpenClaw.Tray.WinUI/Services/NodeService.cs:775
      Startup and disable have no shared lifecycle synchronization, and failed startup can erase another attempt's listener reference. This threatens the user's transport-disable boundary; it does not establish bypass of bearer authentication, execution approvals, or sandbox controls.
      Confidence: 0.97

    What I checked:

    • Verified current main: The checkout matches the GitHub main branch at e702924; the repository identifies main as its default branch. (e70292487958)
    • Unsynchronized startup and failed-attempt cleanup: StartMcpServer checks enabled intent and the server field without a lifecycle lock, starts an attempt, then publishes it; its catch unconditionally clears the server field. A losing attempt can therefore erase another attempt's published listener. (src/OpenClaw.Tray.WinUI/Services/NodeService.cs:773, e70292487958)
    • Disable can miss an in-progress startup: StopMcpServerAsync reads and clears the server and bridge without synchronizing with startup. SetMcpEnabled writes enabled intent under _clientLock, which StartMcpServer does not acquire. Startup can pass its enabled check, disable can observe no server, and startup can subsequently publish a live listener. (src/OpenClaw.Tray.WinUI/Services/NodeService.cs:870, e70292487958)
    • Independent production entry points: AttachClient releases _clientLock before RegisterCapabilities, which calls StartMcpServer after releasing _capabilitiesLock. SettingsChangeCoordinator launches reconnect before applying MCP runtime state, and App does not await that reconnect before continuing. (src/OpenClaw.Tray.WinUI/Services/NodeService.cs:604, e70292487958)
    • Transport locking does not protect host ownership: McpHttpServer serializes its own Start and shutdown operations, but those locks belong to individual server instances and cannot serialize competing instances created by NodeService. (src/OpenClaw.Shared/Mcp/McpHttpServer.cs:169, e70292487958)
    • Existing coverage leaves the ownership race unproved: The MCP restart contract test asserts source ordering and capability preservation; McpRuntimeStatePolicyTests exercise pure decisions, while HTTP tests exercise individual-server disposal. These do not establish atomic NodeService startup versus disable. (tests/OpenClaw.Tray.Tests/AppRefactorContractTests.cs:719, e70292487958)
    How this review workflow works
    • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
    • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
    • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
    • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
    • Maintainers can also comment @clawsweeper review to request a fresh review only.
    • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge; those execution lanes are retired.
    • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

    Reviewed November 8, 2026, 1:29 AM ET / 06:29 UTC.

  5. locked as resolved and limited conversation to collaborators on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2clawsweeper:needs-security-reviewclawsweeper:no-new-fix-prclawsweeper:source-reproimpact:securityissue-rating: 🦞 diamond lobsterlever

Type

No type

Fields

Priority

None yet

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions