Skip to content

fix: stop completed MCP calls from killing the stdio server - #3

Merged
Balanov merged 1 commit into
Balanov:mainfrom
Zenty-Me:fix/mcp-stdio-cancellation
Nov 13, 2026
Merged

Balanov merged 1 commit into
Balanov:mainfrom
Zenty-Me:fix/mcp-stdio-cancellation

Conversation

@Zenty-Me

@Zenty-Me Zenty-Me commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

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.

Fixes #2.

Validation:

  • 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.
  • Full Linux tests also passed with the agent changes from PR fix: preserve tool output when execution fails #1 applied using a Go build overlay.
  • git diff --check passed.

@Balanov

Balanov commented Nov 13, 2026

Copy link
Copy Markdown
Owner

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.

@Balanov
Balanov merged commit 3b5acfd into Balanov:main Nov 13, 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.

CI: investigate Ubuntu failure in TestStdioMCPTool

2 participants