Repository navigation
fix(gateway): keep CLI tools working after caller scope closes - #150339
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 16, 2026, 6:48 PM ET / 22:48 UTC (Revision 2). ClawSweeper reviewWhat this changesThe PR gives the shared CLI tool listener its own lifetime, isolates each tool execution’s cancellation scope, and waits for accepted cleanup during shutdown. Merge readiness✅ Ready for maintainer review Keep open: this remains a useful, focused fix for a source-supported CLI tool failure that persists on the fetched main and latest release. No blocking correctness or security findings were identified. Priority: P1 Review scores
Verification
How this fits togetherOpenClaw’s local MCP HTTP listener exposes Gateway tools to CLI backends through authenticated requests. It resolves permitted tools, executes them, and returns results while tracking their cleanup. flowchart TD
A[CLI backend request] --> B[Bearer and grant checks]
B --> C[Listener lifetime and tool cache]
C --> D[Independent execution scope]
D --> E[Tool result to CLI]
D --> F[Accepted cleanup]
G[Gateway shutdown] --> H[Close listener and drain work]
F --> H
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +20, Tests +155. Total +175 across 2 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep lifetime ownership with the shared listener and its individual executions while preserving per-run authorization and the existing closed-scope fences. Do we have a high-confidence way to reproduce the issue? Yes: start the listener inside a caller scope, close that scope, then invoke a tracked tool over HTTP. Source inspection and the supplied before/after results establish this path; this read-only review did not execute it. Is this the best way to solve the issue? Yes: the patch repairs the serving owner and reuses existing cancellation primitives without weakening closed-scope rejection or adding configuration, persistent state, or a parallel API. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 75ba5e3fae5c. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
Give the shared MCP HTTP listener its own work lifetime and each tool call an independently tracked request scope. Stop HTTP admission before joining listener cleanup so late work cannot poison a successor listener. Fixes #146265. Related: #144788. Thanks to marieldejesus12 for the instrumented scope trace and josepeluso for the Claude/Gemini comparison with unchanged plugin configuration.
e037444 to
a8f3b9a
Compare
|
Merged via squash.
|
…law#150339) Give the shared MCP HTTP listener its own work lifetime and each tool call an independently tracked request scope. Stop HTTP admission before joining listener cleanup so late work cannot poison a successor listener. Fixes openclaw#146265. Related: openclaw#144788. Thanks to marieldejesus12 for the instrumented scope trace and josepeluso for the Claude/Gemini comparison with unchanged plugin configuration.
What Problem This Solves
Fixes #146265: Claude CLI tool calls can keep failing with
Async work scope is closedafter the turn that first started the shared MCP listener finishes. Related: #144788.The original restart-race/shared-DB-pool diagnosis was withdrawn by the reporter after an instrumented trace identified the MCP HTTP listener retaining a heartbeat turn's closed scope: #146265 (comment).
User Impact
User impact: later Claude CLI turns keep using bundled and plugin tools after an earlier heartbeat, cron, or user turn finishes. Listener shutdown also waits for accepted tool cleanup, including cleanup that outlives the HTTP response.
Why This Change Was Made
The lazy MCP listener now owns its async work scope until Gateway shutdown and does not inherit the first caller's root admission. Cached tools are constructed under that listener lifetime; hooks and each tool execution run in a fresh request scope through the existing cancellation helper. Existing grant checks after discovery and before execution remain in place.
The listener stops accepting HTTP requests before its work scope drains, and failed startup also drains its scope. An active listener therefore cannot remain bound to a closed work scope, so there is no new health branch or speculative failure projection. Old runtime grants retain their existing revocation behavior.
This is distinct from #148089 (deferred session lifecycle hooks) and #148025 (deferred followup-queue drains), and from #144788's post-ack webhook continuation. None of those changes owns the shared MCP HTTP listener.
Thanks to @marieldejesus12 for the instrumented scope trace and @josepeluso for the Claude/Gemini comparison with unchanged Home Assistant configuration.
Evidence
Real loopback HTTP listener, JSON-RPC dispatch, tool cache, and real async scopes; the synthetic tool performs tracked work. No live Claude/model service or operator Gateway is needed for the regression.
Before the production change:
After:
The new tests also prove cached tool construction retains the listener scope, separate executions receive distinct scopes, closing the creator's root admission does not poison later calls, and predecessor cleanup cannot close a successor listener. The earlier replacement control was already green; the committed regression extends it by closing the replacement's creator before invoking its tools.
node scripts/check-changed.mjs -- src/gateway/mcp-http.ts src/gateway/mcp-http.async-scope.test.tspassed in full, including core/test typechecks, line-cap guards, export scans, lint, and runtime import-cycle checks. Independent Codex review with--mode branch --base origin/main --max-priority P1is scoped-clean with no actionable P0/P1 findings.Production: +32/-12 lines (net +20). Tests: +155 lines. The added production lines assign and drain the serving owner, isolate request cancellation, and clean up failed startup. Lazy import boundaries remain unchanged; no build was run. No live Windows/Claude, full Gateway restart-race, or published-updater run is claimed; the repair changes the candidate runtime's MCP serving lifetime and needs no driver marker or data migration.
Sibling scope-capture inventory and dispositions
The DB/worker owners already capture each operation:
worker-task-pool.ts:111(dispatch restoration at:246, reply restoration at:272),sqlite-worker-client.ts:103, andsqlite-worker-operation-admission.ts:36. The adjacentsrc/mcp/tools-stdio-server.ts:22,31-32,47-49,75-77already gives its server/request work explicit lifetimes.The following background captures retain their existing contracts. A follow-up audit can check import-time context selection, but none was shown to bind later tool admission to a closed scope, so they are not changed here.
src/infra/sqlite-worker-broker.ts:58src/infra/worker-task-pool.ts:21src/infra/sqlite-coordinator.ts:122src/infra/sqlite-wal.ts:62src/infra/net/pinned-dispatcher-pool.ts:7extensions/memory-core/src/memory/background-context.ts:6src/skills/runtime/refresh.ts:63src/config/sessions/session-transcript-worker-runtime.ts:192src/agents/agent-bundle-mcp-manager-lifecycle.ts:22src/agents/tools/media-generate-background-shared.ts:49Per-run
captureAsyncWorkTrackerclosures remain exact-owner lifetime fences.