You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: stop completed MCP calls from killing the stdio server - #3
fix: stop completed MCP calls from killing the stdio server
A successful MCP stdio request can be followed by MCP server closed stdio on the next request. Call closes its completion channel before its caller cancels the request context. If the watcher runs after both channels are closed, its select can choose cancellation and kill the shared server even though that request already completed. This also affects the deferred cancellations in Manager.Connect and Manager.Register.
Use context.AfterFunc and unregister the cancellation callback before Call returns. Add regression coverage that repeatedly cancels completed calls and checks that the same server remains usable. A separate test verifies that a deadline still interrupts a request whose server never responds.
Reproduced the original TestStdioMCPTool failure on Linux with Go 1.25.14, matching CI. In a run with -count=100 -cpu=1,2,4, the original test failed 3 times and the new completed-call regression failed 244 times against the original client.
After the fix, go test ./internal/mcp -run '^TestStdio' -count=100 -cpu=1,2,4 passed (900 test executions, including 60,000 completed-call cancellation cycles).
Linux Go 1.25.14: full tests, vet, and go build -trimpath passed. go test -race ./internal/mcp -count=5 also passed.
Windows Go 1.27.1: full tests, vet, and go build -trimpath passed; the original stdio test also passed 100 consecutive runs.
Thanks — and thank you for linking Fixes #2. I diagnosed the same thing independently while reviewing #2: Call closed its done channel before the caller cancelled the request context, so the watcher's select could pick the cancellation branch and kill the shared stdio server for a request that had already completed. context.AfterFunc plus unregistering the callback before Call returns removes that race, and the deferred cancellations in Manager.Connect and Manager.Register are exactly where it bit.
Your reproduction numbers are the part I want to keep: a failure that only shows up in 3 of 100 runs on Linux and never on macOS or Windows is the kind of bug that gets dismissed as a flaky test.
One suggestion for extra safety: stopCancel returns false when the callback has already started, so a cancellation that races the final response can still kill the server. An atomic "call finished" flag checked inside the callback would close that window completely.
With this merged, issue #2 closes — the failure was a product bug, not a test race, and the CI noise goes away with it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: stop completed MCP calls from killing the stdio server
A successful MCP stdio request can be followed by
MCP server closed stdioon the next request.Callcloses its completion channel before its caller cancels the request context. If the watcher runs after both channels are closed, itsselectcan choose cancellation and kill the shared server even though that request already completed. This also affects the deferred cancellations inManager.ConnectandManager.Register.Use
context.AfterFuncand unregister the cancellation callback beforeCallreturns. Add regression coverage that repeatedly cancels completed calls and checks that the same server remains usable. A separate test verifies that a deadline still interrupts a request whose server never responds.Fixes #2.
Validation:
TestStdioMCPToolfailure on Linux with Go 1.25.14, matching CI. In a run with-count=100 -cpu=1,2,4, the original test failed 3 times and the new completed-call regression failed 244 times against the original client.go test ./internal/mcp -run '^TestStdio' -count=100 -cpu=1,2,4passed (900 test executions, including 60,000 completed-call cancellation cycles).go build -trimpathpassed.go test -race ./internal/mcp -count=5also passed.go build -trimpathpassed; the original stdio test also passed 100 consecutive runs.git diff --checkpassed.