Skip to content

fix(mcp): preserve transport ownership through cancellation and reload - #2062

Closed
KooshaPari wants to merge 6 commits into
1jehuang:masterfrom
KooshaPari:codex/mcp-owned-lifecycle-20261103
Closed

KooshaPari wants to merge 6 commits into
1jehuang:masterfrom
KooshaPari:codex/mcp-owned-lifecycle-20261103

Conversation

@KooshaPari

@KooshaPari KooshaPari commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Shared MCP startup could hold a manager-wide handle lock during a slow handshake, preventing independent peers from publishing tools. Coordinate handshakes per server, retain admission through handle publication, roll back cancelled claims, and keep unconfirmed cleanup visible until a successful drain retry. Failed reloads reconcile the visible registry and hide closed or unconfirmed handles.

Split client, manager, pool, remote transport, and Windows ownership code into focused modules. Windows child environments retain ordinal case-insensitive ordering, preserve UTF-16 values, and return comparison errors before spawn. Inherited credentials require explicit per-server opt-in. The upstream merge preserves Windows dependency features; small test extraction and unreachable SSH panic removal satisfy existing quality ratchets without raising budgets.

Validation:

  • Targeted MCP gates covered 79 distinct passing tests on a8f3bd8; the MCP implementation is unchanged in 47725bf. The pool suite initially passed 21/22 with one initialization timeout; its focused retry passed 1/1. Both outcomes are retained. The root CLI check passed without default features.
  • On 47725bf, both relocated cloud handoff tests compiled, were discovered, and passed (2/2). Full formatting and seven source/quality ratchets passed. Independent review verified the moved test scope and unchanged reachable SSH retry behavior.
  • Hosted CI run 41962409063 passed for head 47725bf using GitHub's synthetic merge 0118e66 into upstream f8a950c. All 15 reported checks passed, including Linux/macOS/Windows build-and-test jobs, Quality Guardrails, cloud acceptance, and both Windows companion targets.

Windows natural-process fixtures require C:\Python314\python.exe. Production termination, installation/release, coverage acceptance, and the STOS-specific Linux reproduction remain separate acceptance gates. Reconnect/session persistence work is separate. This PR remains a draft.

Related: #1036 (registered tools versus live connection state and child cleanup).

tx-agent: codex
tx-validated: test,cargo-check
tx-scope: mcp
Merge 378f449 and preserve both parents' Windows process features.
Validated 72 targeted MCP tests and the root CLI cargo check on the exact
resolved tree. The 57 MCP Rust files retain their previously checked format.

tx-agent: codex
tx-validated: test
tx-scope: mcp
tx-parent: b44b6ad
Propagate Windows environment comparison failures before spawn and preserve
case-insensitive Unicode ordering. Report cleanup failures, recover poisoned
test observer locks, and keep remote test modules classified as tests.

Validate Unicode child environment, inherited synthetic-secret filtering,
explicit opt-in, and invalid keys with an isolated one-test subprocess.
Use UTF-8 explicitly for the Python JSON protocol fixture.

Validation: 79 distinct targeted tests passed across gates and focused retry;
CLI check passed. Pool suite first ran21/22 with an initialization timeout;
its focused retry passed1/1. Preserve both outcomes in local gate receipts.

tx-agent: codex
tx-validated: test
tx-task: mcp-owned-lifecycle-20261103
Integrate upstream28ac5e6 without rewriting the published MCP history.
Move two existing cloud handoff tests into a small nested module and qualify
their commands helper path. Replace the impossible empty retry result panic
with an error while retaining SSH deadlines, attempt counts and last-error
host context on reachable paths. Keep ratchet budgets unchanged.

Validation: both relocated TUI tests compiled, were discovered and passed
(2/2;3362 filtered). Full formatting and all seven source/quality ratchets
passed. Independent source review confirmed scope and SSH behavior.

tx-agent: codex
tx-validated: test
tx-task: mcp-owned-lifecycle-20261103
* fix(server): retain sanitized lightweight pipe error context

tx-agent: codex
tx-validated: test

* fix(server): preserve safe I/O details across reply paths

tx-agent: codex
tx-validated: test
Package a dedicated Job-contained fixture and separate observational probes.
Keep accounting and process-handle completion claims distinct.

tx-agent: codex
tx-validated: test,build
tx-task: mcp-pool-overlap-regression
tx-parent: 47725bf
@KooshaPari

Copy link
Copy Markdown
Contributor Author

Closing this draft, with the measurement so it can be reopened

Closing rather than porting, by operator decision. Nothing here is deleted: the branch stays, and this comment is the map for reopening.

Measured

Item Value
Diff vs master 79 files, +11,302 / -4,685
Base 28ac5e603 (2026-11-04 06:45Z, "docs: update downloads badge")
Head 2830cf0d3 (2026-11-11 13:16Z, "test(mcp): add contained Windows pool overlap regression (#252)")
Drift since base 911 commits on master (now 70c69c717, 2026-11-21)
Drift on the same files 6 commits touching crates/jcode-base/src/mcp, 63 touching crates/jcode-app-core/src/tool, 90 touching crates/jcode-app-core/src/server
Merge-base distance 10 days of branch age, 10 days of upstream movement, never merged or rebased after 2026-11-11

Why closed rather than ported

The change is architectural, not additive: it splits mcp/{client,manager,pool}.rs into ~35 new modules (client_connect, client_shutdown, manager_lifecycle, pool_handle_lease, pool_attempt, windows_native_*, windows_job, etc.) and introduces an admission/lease protocol into the shared manager. master's crates/jcode-base/src/mcp/mod.rs still declares only budget, client, manager, oauth, pool, protocol, remote, schema_cache, tool — none of those modules exist upstream.

Meanwhile master moved in the same area, in ways that conflict semantically rather than textually:

  • 2f8957ca6 "Cut idle battery drain: reap exec-inherited children, MCP process groups and exponential reconnect backoff" — rewrote child lifecycle in mcp/client.rs and mcp/pool.rs, the two files this PR restructures most.
  • 84d437017 / ae298ebe1 / 2f81d41d0 — schema-cache snapshots, borrowed OAuth registration metadata, and token-form release before response waits: new budget.rs / schema_cache.rs / oauth.rs seams that this PR predates and would have to be threaded through 35 new modules.
  • 9d08c53f9, 2c05ddd73 — reconnect and provider-fallback behaviour in the same connect paths.

Rebasing 911 commits across a 79-file module split whose whole point is to own those same identifiers is a rewrite, not a merge. The prior CI evidence in the body (run 41962409063, head at 47725bf5e) is now two heads and 911 commits stale and no longer describes anything on master.

Real capability gap that remains (follow-up work, not lost)

These are still absent from master and are worth a fresh branch on a clean base:

  1. Windows Job object ownership for MCP children. windows_job.rs and the windows_native_* modules have no upstream equivalent — master's tree contains no Job-object module at all; 2f8957ca6's process-group reaping is the lighter POSIX-flavoured answer and does not give kill-on-close semantics for grandchildren. This is the part most likely to still matter.
  2. cleanup_pending lifecycle. Keeping unconfirmed-child cleanup visible until a successful drain retry, and rolling back cancelled admission claims, has no counterpart upstream.
  3. Per-server handshake coordination with admission retained through handle publication — the core anti-deadlock claim of this PR. Upstream still has the manager-wide path the PR was written to fix.
  4. Per-server opt-in for inherited credentials. Still not gated upstream.
  5. Small extras: ordinal case-insensitive env ordering, UTF-16 preservation, comparison errors before spawn.

Suggested shape if reopened: land the Windows Job ownership and cleanup_pending drain-retry as one focused PR against current master, and treat the module split as a separate mechanical PR only if it is still wanted. The module split itself is the most expensive and least separable part of this branch and is what makes it unportable.

Related: #1036.

@KooshaPari KooshaPari closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant