Repository navigation
fix: bound MCP connect so mcp list cannot hang - #46800
RaviTharuma wants to merge 1 commit into
Conversation
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
|
Hey! Your PR title Please update it to start with one of:
Where See CONTRIBUTING.md for details. |
There was a problem hiding this comment.
🟡 Changes recommended
The current timeout helper doesn’t cancel underlying connects/spawns (risking lingering sockets/processes and continued hangs), and the CLI timeout fallback suppresses useful error context for users.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR aims to ensure opencode mcp list can’t hang indefinitely when plugins (and their MCP connections) are enabled by bounding MCP initialization work with timeouts.
Changes:
- Add a timeout wrapper around each
create()call during MCP status initialization and mark timed-out servers asfailed. - Add an overall timeout around
MCP.status()in themcp listCLI command to prevent the command from blocking forever.
File summaries
| File | Description |
|---|---|
| packages/opencode/src/mcp/index.ts | Wrap per-server create() during status init in a timeout and record failures/timeouts in status. |
| packages/opencode/src/cli/cmd/mcp.ts | Bound mcp list by timing out the MCP.status() call. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
# Conflicts: # packages/opencode/src/cli/cmd/mcp.ts # packages/opencode/src/mcp/index.ts
5133253 to
b1a2c73
Compare
Hahaknight
left a comment
There was a problem hiding this comment.
Thanks for tackling this — opencode mcp list blocking indefinitely on one bad server is a real papercut, and bounding create in the layer is the right instinct. A few things I'd check before merge:
1. The listState timeout wipes all statuses, not just the slow server (packages/opencode/src/cli/cmd/mcp.ts)
mcp.status() returns the whole status record; the orElse replaces it with {}. When it fires, the renderer's if (!status) branch shows every server — including ones that connected fine — as ○ not initialized. One slow server (or N servers each inside their own budget but >20s in aggregate) degrades the entire table into a misleading state. Per-server timeouts (mirroring the layer-side bound) would keep healthy servers rendering; alternatively surface an explicit "timed out" state instead of "not initialized".
2. Interrupt-on-timeout can leak an already-spawned local MCP child (packages/opencode/src/mcp/index.ts, create)
When the new outer timeout fires, create is interrupted mid-flight. The mcpClient.close() cleanup only runs via the inner gen's catchCause, which external interruption bypasses — on interrupt only finalizers run. So a local server that already spawned+connected and then hangs (e.g. in tool listing) gets interrupted with its child process left alive — the descendants/pid machinery elsewhere in this file suggests that's a known hazard here. Wrapping the body in Effect.onInterrupt (close whatever client/transport exists) would make the timeout safe. Worth a manual repro: stdio server that completes the handshake, then stalls on tools/list.
3. Root-cause question: both connection (connectTransport(transport, connectTimeout) at the two mcp.timeout ?? DEFAULT_TIMEOUT sites) and tool listing (McpCatalog.defs → listTools(client, timeout ?? DEFAULT_TIMEOUT), catalog.ts:39) are already individually bounded at 30s. Where was the hang actually observed? If one of those inner bounds isn't effective on some path, this outer bound masks it while the leak in (2) remains. A repro snippet or trace link would help decide whether the inner bound also needs fixing.
Nits
listStatehardcodes20 secondswhile the layer path usesmcp.timeout ?? DEFAULT_TIMEOUT— a user who raisestimeoutstill gets cut off at 20s inlist, with the misleading status from (1).- Layer
orElsereports"Failed to connect"even if the timeout hit during tool listing after a successful connect; including the key and elapsed time (connect timed out after 30s) would save a confusing debug session.
Direction is right; (1) and (2) are what I'd want addressed before this ships.
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
|
The description now uses the PR template (Closes #43484). GitHub will not let me reopen this PR from here. Please reopen if the template looks ok, or say if you want a fresh PR from the same branch (fix/mcp-list-timeout). |
Issue for this PR
Closes #43484
Type of change
What does this PR do?
mcp list can hang forever if an MCP server never finishes connecting. This times out each MCP create during status init and caps the wait on MCP.status() so a stuck plugin cannot block the command.
How did you verify your code works?
MCP status/list path with a slow or stuck create should return instead of blocking. Unrelated MCP behavior is unchanged.
Screenshots / recordings
Not a UI change.
Checklist