P1: sync-over-async in Dispose() is deadlock-prone across McpHttpServer, NodeService, and GatewayConnectionManager
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.
McpHttpServerdrain may block a finalizer thread.- Unit tests may deadlock if a
SynchronizationContextis ever introduced.
Suggested fix
- Expose
ValueTask DisposeAsync()orTask ShutdownAsync()on hot services (McpHttpServer,NodeService,GatewayConnectionManager). - Make callers use
await service.DisposeAsync()orIAsyncDisposable. - 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.cssrc/OpenClaw.Tray.WinUI/Services/NodeService.cssrc/OpenClaw.Connection/GatewayConnectionManager.cs
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:
- Current main: Repository HEAD for this review is bf21e7b216aa34e5f3c4895aa36b64ea8c6980f6. (bf21e7b216aa)
- McpHttpServer blocking Dispose: Dispose() blocks on StopCoreAsync(DrainTimeout).GetAwaiter().GetResult() and also blocks on Task.WhenAny(...).GetAwaiter().GetResult() when StopAsync is already in flight. (
src/OpenClaw.Shared/Mcp/McpHttpServer.cs:448, bf21e7b216aa) - NodeService blocking Dispose: Dispose() calls _voiceService?.DisposeAsync().AsTask().GetAwaiter().GetResult(). (
src/OpenClaw.Tray.WinUI/Services/NodeService.cs:1909, bf21e7b216aa) - GatewayConnectionManager blocking cleanup: DisposeActiveClient() blocks on Task.Run(() => _nodeConnector.DisconnectAsync()).Wait(TimeSpan.FromSeconds(2)); Dispose() also blocks on the transition semaphore and tunnel StopAsync through Task.Run().Wait(). (
src/OpenClaw.Connection/GatewayConnectionManager.cs:929, bf21e7b216aa) - App shutdown reachability: ExitApplication synchronously disposes the GatewayConnectionManager and NodeService, so these blocking disposal paths are reachable during tray shutdown. (
src/OpenClaw.Tray.WinUI/App.xaml.cs:3993, bf21e7b216aa) - Lifecycle docs: The connection architecture docs define GatewayConnectionManager as the owner of connection lifecycle and DisconnectAsync as the teardown API, supporting this as a core lifecycle concern. (
docs/CONNECTION_ARCHITECTURE.md:19, bf21e7b216aa)
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-reviewcomments, 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-reviewor@clawsweeper re-runon an open PR or issue to request a fresh review only. - Maintainers can also comment
@clawsweeper reviewto 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 explainto ask for more context, or@clawsweeper stopto stop active automation.
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.