Repository navigation
Description
Summary
When a file in a watched plugin directory (specifically the plugin entrypoint dist/index.js) is rewritten while an MCP stdio tool call is in flight, opencode tears down the MCP stdio transport, dropping the in-flight call with Connection closed. The server (Serena) then exits with anyio.BrokenResourceError on its stdin reader, and opencode auto-reconnects ~2s later. The underlying tool subprocess is not killed — only the MCP round-trip is lost, so the work is silently discarded from the caller's perspective.
Environment
- opencode
v2.4.3 - Serena MCP server (uvx, stdio transport), 30 tools
- Linux
- Plugins loaded from a local watched directory containing
dist/index.js
Symptom
An in-flight execute_shell_command MCP call fails with:
MCP tool "execute_shell_command" on server "serena" failed: Connection closed
opencode logs WARN failed to call MCP tool ... error="Connection closed", followed by a watcher/plugin-reload burst, then mcp connected server=serena tools=30 ~2.1–2.2s later. The shell subprocess itself completes; only the MCP round-trip is lost.
Serena-side, the MCP server exits 1 with a traceback ending in anyio.BrokenResourceError from mcp/server/stdio.py stdin_reader — i.e. the host closed the stdio pipe, the server's in-process stream was closed underneath it.
Minimal reproduction
All calls made through the same MCP stdio connection. Baseline controls first, then the trigger:
| # | Command | Result |
|---|---|---|
| 1 | echo BASELINE_OK |
OK 66 ms |
| 2 | sleep 15; echo done |
OK 15027 ms |
| 3 | sleep 25; echo done |
OK 25034 ms |
| 4 | seq 1 20000 (~50k chars output) |
OK 52 ms |
| 5 | 22 s shell call + background touch AGENTS.md at 3 s |
OK 22207 ms |
| 6 | 22 s shell call + background touch dist/index.js at 3 s |
DISCONNECT at 5250 ms |
| 7 | (confirm) 10 s shell call + touch dist/index.js at 2 s |
DISCONNECT at 4162 ms |
Duration and output size are not sufficient triggers (rows 1–5 succeed). The trigger is overlap with a write to the watched plugin directory (dist/index.js), which is repeatable (rows 6, 7).
Minimal trigger:
# while a long MCP tool call is in flight, in another shell:
touch dist/index.js # plugin entrypoint inside a watched plugin dirExpected vs actual
- Expected: rewriting a plugin file may reload the plugin, but an in-flight MCP tool call should either be allowed to drain, or the caller should get a clear "server restarting, retry" signal — not a silent
Connection closedwith the request abandoned. - Actual: the stdio transport is closed mid-call; the in-flight request fails with
Connection closed; the server exits non-zero; opencode reconnects unconditionally ~2s later. The tool's subprocess keeps running, so the result is lost and the caller cannot tell whether the work completed.
Log signature
opencode:
WARN failed to call MCP tool server=serena error="Connection closed"
... watcher/plugin-reload burst (command.updated / provider.updated / model.updated) ...
INFO mcp connected server=serena tools=30 # ~2.1-2.2 s later
Serena:
ERROR MCP server process exited with code 1
anyio.BrokenResourceError (mcp/server/stdio.py: stdin_reader -> ...)
Relation to existing issues
- config: watcher.ignore is not applied to plugin entrypoints and the config-directory watcher #55934 —
watcher.ignoredoes not apply to plugin reloads; the same root path (a watched write triggers a reload we cannot suppress). - fix(core): ignore node_modules and .git in configured plugin directory watches #66471 (merged) — only ignores
node_modules/.git; explicitly leavesdist/buildreloads in place. - Location shutdown does not close its MCP servers, so reloads leave old stdio processes running #51731 — related MCP lifecycle leak.
Distinct part not covered by the above
There is no plugin-side hook to veto or pause the reload, nor to drain in-flight MCP calls before reconnect. The MCP lifecycle is host-owned; reconnect is unconditional. A way to (a) suppress reload for a known-watched dir, or (b) drain/park in-flight MCP tool calls across a reload, would resolve this.
Impact
- In-flight MCP tool calls fail with an opaque
Connection closedwhile their subprocess keeps running — silent work loss / duplicate execution risk. - Recovers only by client auto-reconnect (~2s), during which all MCP calls fail.
Evidence gathered via bounded, controlled experiments; not claiming a specific internal cause beyond "host closes the stdio pipe during a plugin-reload overlap".
Activity
opencodejr commentedon Dec 5, 2026
I couldn't reproduce this on 2.4.3 (Linux). My setup was a minimal stdio MCP server with a 12 s tool and a local plugin with entrypoint
dist/index.js. I tried the plugin in.opencode/plugins/, listed inpluginsin the project'sopencode.json, and at the project root. In each case, touchingdist/index.jsduring the call reloaded the plugin, but the MCP server kept running (same pid, no stdin EOF) and the call returned normally. That fits the code: a reload only restarts MCP servers whose config changed.Your log shows
provider.updated/model.updatedduring the reload, so what your plugin does when it loads probably matters. These would help:ctx.mcp.transform, or values that differ on each load).--log-level debug) around one touch, filtered formcpandplugin.opencodejr commentedon Dec 5, 2026
Closing this as not planned: I could not reproduce the problem with what the report gives. Replying here reopens the issue.
dcerisano commentedon Dec 6, 2026
Reopening with a reproduction that includes the missing ingredient.
The earlier attempt did not reproduce because its minimal plugin never disposed its own registrations. This plugin's
setup()returns a cleanup that explicitly callsregistration.dispose(). That explicit disposal is what tears down the MCP pipe; the transform's values are constant, so this is not a config-diff restart.Mechanism (short): during a plugin reload the host closes the old activation and loads the replacement inside a
State.batch. But a registration disposed from the plugin's own cleanup runs through the Promise adapter's captured setup-time runtime (packages/plugin/src/promise/adapter.ts), whoseState.batchis already inactive. Soregistration.remove()->changed->notifyruns immediately instead of joining the reload batch. The rebuilt MCP map has noserenaentry because the new setup has not registered its transform yet (it awaitssession.list()first). Reconcile then seesservers.get("serena") === undefined, fails the guard atpackages/core/src/mcp/index.ts(isDeepStrictEqual(entry.config, updated)), and stops the stdio connection.Minimal plugin (reproduces)
Control (does not reproduce)
Same transform values, but leave disposal to the host scope (do not call
registration.dispose()in the cleanup):Steps
opencode.json:{ "plugins": ["/absolute/path/to/this/plugin"], "experimental": { "todo": true } }sleep 22; echo LONG_DONE.touch /absolute/path/to/this/plugin/index.js(ordist/index.js).Observed (explicit-dispose plugin):
MCP tool "execute_shell_command" on server "serena" failed: Connection closed, thenmcp connected server=serena tools=30about 2 s later; thesleepkeeps running and its result is lost.Observed (control): the call returns normally; the reload does not drop the in-flight request.
Why this matches #55934/#66471: build output under a watched plugin directory still triggers the reload;
watcher.ignoreis the user-side workaround, but the reload path itself still drops calls.opencodejr commentedon Dec 6, 2026
Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.
This still happens on the latest
dev(045e9e3), and also on 2.4.3. Thanks for the plugin that triggers it.What I saw: a Promise plugin listed in
pluginswaits 500 ms, registers a stdio server throughctx.mcp.transformwith fixed values, and callsregistration.dispose()in its cleanup. I started a 12 s MCP call and toucheddist/index.js3 s in. About 1 ms after the plugin's cleanup ran, OpenCode closed the server's stdin, and a new server process started about 0.5 s later. The call failed withMCP tool "sleep" on server "sleepy" failed: Connection closed. With a cleanup that doesn't calldispose(), the same server process stayed up and the call returned normally.A small Node MCP server stood in for Serena, and the simulated model made the tool call. The script and recording below show both runs.
Likely cause (not confirmed): in
packages/plugin/src/promise/adapter.ts(register, around line 250),disposeruns with the runtime captured at setup. That runtime still carries the originalState.batch, which is no longer active (packages/core/src/state.tsbatch()). The removal therefore takes effect immediately instead of joining the reload, so the MCP reconcile inpackages/core/src/mcp/index.tssees the server as removed and stops it.Workaround until it's fixed: leave disposal to the plugin scope (return a cleanup that doesn't call
dispose()).https://anoma.ly/team/github-bot/media/f3df63924c5ae77fed9381c8ee76d841de7f12e20e149d7787eede4c5d1f2961.mp4
Reproduction script (drive.ts)
Run it with opencode-drive:
opencode-drive run ./drive.ts. It drivesopencodefrom yourPATH; setOC_DEVto the path of an OpenCode checkout to run that from source instead.opencodejr commentedon Dec 6, 2026
I opened a pull request with a fix: #68589. A maintainer will review it.
fix(plugin): honor watcher.ignore for plugin directory and file watches
fix(mcp): drain in-flight tool calls before plugin reload