Skip to content

fix(gateway): recover MCP loopback after unexpected close - #183352

Merged
steipete merged 12 commits into
openclaw:mainfrom
SC-10749:fix/139340-mcp-loopback-liveness
Oct 27, 2026
Merged

steipete merged 12 commits into
openclaw:mainfrom
SC-10749:fix/139340-mcp-loopback-liveness

Conversation

@SC-10749

@SC-10749 SC-10749 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #139340

What Problem This Solves

Fixes: CLI-backed runs keep receiving a dead MCP endpoint after its listener closes while the Gateway remains alive.

User Impact

The next CLI run starts a replacement listener and regains tool access. Grants from the retired listener stop authorizing calls. The Gateway log records unexpected closure.

Why This Change Was Made

The listener owner shares idempotent cleanup between managed shutdown and the actual close event. Cleanup retires only its own runtime, grants, and tool cache, preserves a successor listener, and drains accepted work.

Thanks to @SC-10749 for the fix and process-level recovery proof. Contributor commits are preserved.

Co-authored-by: songc [email protected]

Evidence

  • The real HTTP/filesystem regression fails on main 0cbd02a455a08bb329fb6f695dc5ce30708338c9: closing the actual listener leaves the cached runtime present. The repaired owner passes; authorized writes succeed before closure and through the replacement, while a captured old grant is revoked and cannot write.
  • Recovery and async-scope tests: 12/12 passed before final main integration (95.91 seconds outer wall; recovery test 6.547 seconds). Final integrated-head results are recorded below.
  • Targeted core oxlint, oxfmt, and patch whitespace checks pass. Local Gateway typecheck refuses declaration inputs from the linked shared installation; exact-head CI supplies broad validation.
  • Independent Codex autoreview: scoped-clean, no P0–P2 findings. Simplification removed 54 lines from the contributed test and its redundant preparation wrapper; existing tests already cover revocation during preparation.
  • Net change against integrated main: +21 production, +147 test, +2 documentation lines. Production growth provides the missing listener lifecycle cleanup and warning; no unrelated deletion was added to meet a quota.

Contributor process-level proof on da197ede4f8f6f67e43c88448529dd647a0419a8: a real Windows Gateway launched Claude Code CLI 2.1.98 against an isolated local model mock. An MCP write succeeded, Node Inspector closed the Gateway's actual MCP HTTP listener, and the next CLI run bound a replacement listener and wrote successfully. The Gateway and WebSocket listener remained alive throughout. This proof was retained rather than repeated with an additional Inspector/model harness; the spontaneous real-world cause of listener closure remains unverified. No external model credentials or operator state were used.

Current integration includes main commit a8af441b0efa, which repairs the unrelated reply-preparation test type error seen in the earlier CI run. Final integrated-head focused checks and CI are recorded below.

Final head 1061587b199ccf1066dffe4d1f3e131b0358de98 passes all 12 focused tests after integration (215.00 seconds outer wall, 189.36 seconds Vitest; real HTTP recovery 22.479 seconds under shared-host load). Targeted core oxlint, oxfmt, and whitespace checks pass. Exact-head CI run 40385723704 and the aggregate CI gate passed. Final-head ClawSweeper completed with zero findings.

@clawsweeper

clawsweeper Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S labels Oct 25, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 25, 2026
@clawsweeper

clawsweeper Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR retires a closed MCP listener’s cached endpoint and grants so the next CLI run starts a replacement.

Example: The MCP listener closes while the Gateway remains alive, then Claude CLI requests an OpenClaw write.

  • Before: The new run receives the cached dead endpoint and its tool call fails.
  • After: The new run starts a replacement listener and writes successor.txt; the retired grant receives HTTP 401 and creates no file.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A useful, focused lifecycle repair with supplied process recovery evidence, meaningful authority controls, and no actionable findings.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): The supplied Windows Gateway and actual Claude Code CLI recovery record exercises successful MCP writes before and after Inspector closes the real listener, with model I/O mocked; the production-path HTTP/filesystem regression separately verifies retired authority, denied writes, and authorized successor writes.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: New CLI-backed runs can lose OpenClaw tool access indefinitely after the shared MCP listener closes while the Gateway remains alive.
Reason: The patch restores existing behavior through the current lifecycle owner without new settings, public APIs, or persistent state.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR repairs a current-main listener lifecycle defect with a focused change and no actionable findings; exact-head CI remains pending.

Priority: P2
Reviewed head: 1061587b199ccf1066dffe4d1f3e131b0358de98

Before merge

None.

Findings

None.

Agent review details

How this fits together

The Gateway-owned loopback HTTP listener exposes scoped OpenClaw tools to CLI subprocesses; run grants authorize incoming MCP requests, and CLI preparation consumes the published listener endpoint.

flowchart TD
  A[CLI run preparation] --> B[Active runtime]
  B --> C[Loopback HTTP listener]
  C --> D[Run grant validation]
  D --> E[Scoped tool execution]
  C --> F[Listener close cleanup]
  F --> B
  F --> G[Grant retirement and work drain]
Loading

Technical review

Best possible solution:

Retain this owner-level cleanup repair and land it once the required checks pass.

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

Source establishes stale endpoint reuse after actual listener closure, and the supplied process run exercises that condition; this review ran no target code, and the original spontaneous closure cause remains unknown.

Is this the best way to solve the issue?

Yes: sharing cleanup between managed shutdown and actual listener closure repairs the existing owner while preserving grant isolation, successor ownership, and accepted-work draining.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Single-claim shutdown and successor isolation keeps the original intent (bb98a3a: Prevent concurrent shutdown callers from closing the same listener twice and prevent late old-listener cleanup from revoking a successor.)
  • Listener-owned accepted work keeps the original intent (3072557: Keep the shared listener independent of its starter turn and drain accepted work before releasing its lifetime.)
  • Per-run authority and retained Gateway resources keeps the original intent (fdcbcf5: Remove creator-turn authority while retaining the Gateway process broker and database-reader lifetime.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 3072557 adds src/gateway/mcp-http.ts:522 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 307255794c48; files: src/gateway/mcp-http.ts)
  • Oldrich333: Raw commit fdcbcf5 adds src/gateway/mcp-http.ts:576 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: fdcbcf5c2ebd; files: src/gateway/mcp-http.ts)

PR surface

Source +21, Tests +147, Docs +2. Total +170 across 3 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 1 24 3 +21
Tests 1 147 0 +147
Docs 1 2 0 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 173 3 +170

Labels

Label changes:

No label changes.

Label justifications:

  • P2: A bounded recovery repair restores CLI tool access after listener closure; the spontaneous trigger behind the reported production incident remains unverified.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (7 earlier review cycles)
  • reviewed 2026-10-25T07:00:54.021Z sha 6b211c7 :: needs real behavior proof before merge. :: [P2] Make the new closed mocks satisfy the export ratchet
  • reviewed 2026-10-25T08:17:43.372Z sha ceebf6e :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-25T14:24:50.023Z sha da197ed :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-25T14:32:15.596Z sha da197ed :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-26T04:38:59.045Z sha da197ed :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-26T05:18:33.916Z sha da197ed :: needs maintainer review before merge. :: none
  • reviewed 2026-10-26T18:41:43.841Z sha fcbc8c6 :: needs maintainer review before merge. :: none

Reviewed October 26, 2026, 3:23 PM ET / 19:23 UTC (Revision 8).

@SC-10749

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 25, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 26, 2026
steipete and others added 2 commits October 26, 2026 11:32
Integrate current main and retire the closed listener runtime and grants so the next CLI run starts a replacement.

Co-authored-by: songc <[email protected]>
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation size: S and removed size: M labels Oct 26, 2026
@steipete

Copy link
Copy Markdown
Contributor

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 26, 2026
@steipete

Copy link
Copy Markdown
Contributor

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 26, 2026
@steipete
steipete merged commit 7058d9e into openclaw:main Oct 27, 2026
139 of 141 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 27, 2026
…83352)

Retire the MCP loopback runtime, grants, and cached endpoint when its listener closes unexpectedly, so the next CLI run starts a working listener. Share idempotent cleanup with managed shutdown and preserve successor ownership.

Validated with the actual-listener recovery regression and async-scope tests (12 passed), retained isolated Windows Gateway/CLI proof, and exact-head CI 40385723704.

Closes openclaw#139340

Co-authored-by: Peter Steinberger <[email protected]>
Co-authored-by: Nice De Cc <[email protected]>
Co-authored-by: songc <[email protected]>
RomneyDa added a commit that referenced this pull request Oct 28, 2026
Retire the MCP loopback runtime, grants, and cached endpoint when its listener closes unexpectedly, so the next CLI run starts a working listener. Share idempotent cleanup with managed shutdown and preserve successor ownership.

Validated with the actual-listener recovery regression and async-scope tests (12 passed), retained isolated Windows Gateway/CLI proof, and exact-head CI 40385723704.

Closes #139340

Co-authored-by: Peter Steinberger <[email protected]>
Co-authored-by: Nice De Cc <[email protected]>
Co-authored-by: songc <[email protected]>

(cherry picked from commit 7058d9e)

Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit caea1bbd76548daf42b08686683e3e91dd44cfee)
RomneyDa added a commit that referenced this pull request Oct 28, 2026
Retire the MCP loopback runtime, grants, and cached endpoint when its listener closes unexpectedly, so the next CLI run starts a working listener. Share idempotent cleanup with managed shutdown and preserve successor ownership.

Validated with the actual-listener recovery regression and async-scope tests (12 passed), retained isolated Windows Gateway/CLI proof, and exact-head CI 40385723704.

Closes #139340

Co-authored-by: Peter Steinberger <[email protected]>
Co-authored-by: Nice De Cc <[email protected]>
Co-authored-by: songc <[email protected]>

Beta.5 baseline adaptation: Retain the native beta.5 owners, fixtures and public contracts; add focused regression controls and bounded fixture extraction; apply checkout formatting.

(cherry picked from commit 7058d9e)
(cherry picked from commit fe15736)

Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 441f64154d8d95980b882baef840e5b4a17faaff)
olyashok pushed a commit to cellect-ai/openclaw that referenced this pull request Nov 5, 2026
Preserve the deployed requester-bound mail recovery checkpoint repair while
integrating the reviewed backend recovery, question, model, and delivery fixes.

Upstream OpenClaw backports and adaptations:
- MCP listener recovery, openclaw#183352 / 7058d9e: Nice De Cc, songc,
  and Peter Steinberger.
- Process lineage sharing, 7dc3350: Dallin Romney.
- Model publication supersession, openclaw#179144 / 5a97c90: Peter
  Steinberger; adapted to this fork's successful skipped-candidate batches.
- Authoritative delivery settlement, openclaw#169650 / c2fe11f: Peter
  Steinberger.
- Blocking question routing, openclaw#170039 / e163d81: Peter Steinberger.
- fast-uri security backport, openclaw#187285 / 9f492b6: Dallin Romney;
  upstream parser fixes by Vincent Le Goff and Ulises Gascon.
- Nodemailer sender parsing, openclaw#187301 / 0692808: Dallin Romney;
  upstream parser fixes by Andris Reinman.

Keep existing local question-answer custody and synchronous native Codex
steering; preserve both published SDK module/ingress ownership repairs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP loopback server singleton cached with no liveness check — all MCP tool calls fail permanently after listener dies

2 participants