Repository navigation
fix(plugin): let a Promise plugin's cleanup disposals join the reload batch - #68589
opencodejr[bot] wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
This looks right to me. I ran the #68514 drive script on base and with this change: base closed the MCP server's stdin about 3 ms after the plugin cleanup and the in-flight call failed with Connection closed. With the change, the same server process stayed up and the call returned slept 12.
The cause checks out: in effect 4.1.0, acquireRelease runs its release step with the context from setup time, so a dispose() made from the cleanup used a batch that had already ended. With Scope.addFinalizer, the cleanup runs with the context of whatever is unloading the plugin, so the disposal joins the reload batch the same way the host's own disposals do. The disposal also joins a shutdown batch now, which is an improvement. The new test fails on base and passes here. The other plugin and MCP test failures are the same 14 that fail on base. tsc --noEmit in packages/plugin is clean.
Public API: unchanged. The plugin API's signatures and behaviour stay the same, except that a disposal made from a cleanup now joins the host's batch.
No blocking concerns.
Related: #55934
Issue for this PR
Closes #68514
Type of change
What does this PR do?
See #68514. When a Promise plugin's cleanup disposed its own registration (e.g. an MCP server added with
ctx.mcp.transform), a hot reload tore down that server even though the replacement registers the same config. Any in-flight MCP tool call failed withConnection closed. The adapter ran the cleanup throughEffect.acquireRelease, which uses the context from when the plugin was set up.registration.dispose()also used that setup-time context, whoseState.batchhad already ended, so the removal notified straight away instead of joining the host's reload batch, and the MCP reconcile stopped the server before the new activation re-registered it. The adapter now registers the cleanup withScope.addFinalizer, so it runs in the unloading fiber's context. While the cleanup runs,registration.dispose()uses that context, so the disposal joins the reload batch like the host's own scope-owned disposals. A regression test covers a Promise plugin that disposes its registration in cleanup and re-registers after an await.How did you verify your code works?
Reproduction (opencode-drive with the simulated model, a minimal stdio MCP server with a 12 s
sleeptool, and a Promise plugin listed inpluginsthat registers the server with ctx.mcp.transform after a 500 ms wait and callsregistration.dispose()in its cleanup; touch the plugin entrypoint 3 s into the call): on dev 045e9e3 without the fix the server's stdin closed ~1 ms after the cleanup, a new server pid started, and the tool returnedMCP tool "sleep" on server "sleepy" failed: Connection closed(same on the 2.4.3 release). With the fix the same pid stayed up, the call completed and returnedslept 12. New testkeeps a Promise plugin's registrations through a reload when its cleanup disposes themin packages/core/test/model-plugins.test.ts fails without the fix and passes with it.bun test test/plugin test/model-plugins.test.ts test/mcpin packages/core: 894 pass, 14 fail; the same 14 (GitLab OAuth tests and one test/mcp/stdio.test.ts case) also fail with the fix stashed. The packages'typecheckscript (bun check) is not available in the sandbox's Bun, so I ran tsc directly:tsc --noEmitin packages/plugin andtsc -b tsconfig.tests.jsonin packages/core both report no errors.Screenshots / recordings
The reproduction on #68514 has the recording and script, where there is one.
Checklist
Opened by the bot from its reproduction of #68514 (
simulated), fixed in a sandbox and reviewed in a separate session. A maintainer should review it before merging.Added by the opencode bot (AI-generated).
Related: #55934