Skip to content

P1: sync-over-async in Dispose() is deadlock-prone across McpHttpServer, NodeService, and GatewayConnectionManager #553

Closed

Description

@QQSHI13

Problem

Several critical services perform sync-over-async inside Dispose(), which is deadlock-prone and can stall shutdown or freeze the UI thread.

Locations:

  • src/OpenClaw.Shared/Mcp/McpHttpServer.cs:457 — StopCoreAsync(DrainTimeout).GetAwaiter().GetResult();
  • src/OpenClaw.Tray.WinUI/Services/NodeService.cs:1927 — _voiceService?.DisposeAsync().AsTask().GetAwaiter().GetResult();
  • src/OpenClaw.Connection/GatewayConnectionManager.cs — Task.Run(() => _nodeConnector.DisconnectAsync()).Wait(TimeSpan.FromSeconds(2));

In WinUI, the DispatcherQueue and SynchronizationContext make this especially dangerous. A future change to ConfigureAwait or a WinUI threading update can turn "works most of the time" into a reproducible deadlock.

Impact

  • App hangs on shutdown.
  • McpHttpServer drain may block a finalizer thread.
  • Unit tests may deadlock if a SynchronizationContext is ever introduced.

Suggested fix

  1. Expose ValueTask DisposeAsync() or Task ShutdownAsync() on hot services (McpHttpServer, NodeService, GatewayConnectionManager).
  2. Make callers use await service.DisposeAsync() or IAsyncDisposable.
  3. For fire-and-forget disposal where blocking is truly impossible, use _ = Task.Run(async () => await DrainAsync()) with a timeout, but do not block the caller.

Files

  • src/OpenClaw.Shared/Mcp/McpHttpServer.cs
  • src/OpenClaw.Tray.WinUI/Services/NodeService.cs
  • src/OpenClaw.Connection/GatewayConnectionManager.cs

Activity

  1. clawsweeper commented on May 27, 2026

    @clawsweeper

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed May 27, 2026, 9:01 AM ET / 13:01 UTC.

    Summary
    Current main still contains the reported blocking disposal paths, so this should stay open as an actionable shutdown-lifecycle bug rather than be closed as implemented.

    Reproducibility: no. high-confidence runtime reproduction is present; source inspection confirms the reported blocking calls exist on current main and are reachable from app shutdown.

    Ways to help us reproduce this

    • Add a screenshot or short recording showing the behavior.
    • Add expected vs actual behavior.
    • Include redacted logs or terminal output.

    Next step
    This is a bounded lifecycle repair candidate, but the worker must add focused coverage because the current report is source-reproducible rather than runtime-reproduced.

    Review details

    Best possible solution:

    Add an awaitable shutdown/disposal path for these services and have app shutdown use it, while keeping bounded cleanup and regression coverage for MCP handler drain, gateway disconnect, tunnel stop, and voice-service disposal.

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

    No high-confidence runtime reproduction is present; source inspection confirms the reported blocking calls exist on current main and are reachable from app shutdown.

    Is this the best way to solve the issue?

    Yes, directionally: moving these hot services to an awaitable shutdown path is the maintainable fix, but the patch needs tests that prove cleanup still drains handlers and disconnects node/tunnel resources safely.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • No high-confidence runtime deadlock reproduction is attached, so the exact user-visible hang path and regression test still need to be established.
    • A repair must preserve the existing MCP handler-drain guarantee and GatewayConnectionManager's connection lifecycle ownership while avoiding UI-thread blocking.

    Codex review notes: model gpt-5.5, reasoning high; reviewed against bf21e7b216aa.

    Label changes

    Label changes:

    • add P2: The report identifies real blocking shutdown paths in core services, but no live P1 regression or reproduced freeze is provided.
    • add impact:crash-loop: The reported failure mode is a shutdown hang or UI freeze in service disposal paths.
    • add issue-rating: 🐚 platinum hermit: Current issue advisory state selects this label.
    • add clawsweeper:needs-live-repro: Current issue advisory state selects this label.

    Label justifications:

    • P2: The report identifies real blocking shutdown paths in core services, but no live P1 regression or reproduced freeze is provided.
    • impact:crash-loop: The reported failure mode is a shutdown hang or UI freeze in service disposal paths.
    Evidence reviewed

    Acceptance criteria:

    • ./build.ps1
    • dotnet test ./tests/OpenClaw.Shared.Tests/OpenClaw.Shared.Tests.csproj --no-restore
    • dotnet test ./tests/OpenClaw.Connection.Tests/OpenClaw.Connection.Tests.csproj --no-restore
    • dotnet test ./tests/OpenClaw.Tray.Tests/OpenClaw.Tray.Tests.csproj --no-restore

    What I checked:

    Likely related people:

    • Ranjesh: git blame attributes the reported disposal paths in McpHttpServer, NodeService, and GatewayConnectionManager to 6d8541e, and git log shows several recent touches in these lifecycle files. (role: introduced behavior and recent area contributor; confidence: high; commits: 6d8541e57008, 7747e6075052, d7d7661ac28c; files: src/OpenClaw.Shared/Mcp/McpHttpServer.cs, src/OpenClaw.Tray.WinUI/Services/NodeService.cs, src/OpenClaw.Connection/GatewayConnectionManager.cs)
    • Scott Hanselman: git log and shortlog show recent changes touching GatewayConnectionManager and NodeService around node pairing and capability gating. (role: adjacent connection/node contributor; confidence: medium; commits: 45dccec60c51, 6711591b5c14; files: src/OpenClaw.Connection/GatewayConnectionManager.cs, src/OpenClaw.Tray.WinUI/Services/NodeService.cs)
    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.
    • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
    • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
  2. added
    P2
    impact:crash-loop
    issue-rating: 🐚 platinum hermit
    clawsweeper:needs-live-repro
    on May 27, 2026
  3. shanselman commented on May 28, 2026

    @shanselman
    Collaborator

    Addressed by merged PR #564. The reported sync-over-async disposal paths now have awaitable shutdown/disposal paths and App exit awaits the shutdown sequence.

  4. locked as resolved and limited conversation to collaborators on May 30, 2026
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

    P2clawsweeper:needs-live-reproimpact:crash-loopissue-rating: 🐚 platinum hermit

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions