Repository navigation
Conversation
Wait for the Streamable HTTP grace period after the listener closes. Close remaining connections on a shutdown error and preserve the cause for the CLI. Cover in-flight responses, deadline expiry, startup cancellation, and listener errors.
There was a problem hiding this comment.
🔵 Needs a closer look
Coordinating signal handling, request draining, forced cancellation, and timeout-driven exit status changes affects in-flight responses and process supervision, requiring human review.
No open issues 🎉
What changed in this PR
Fixes the self-hosted Streamable HTTP CLI shutdown lifecycle so active requests can finish before the process exits, without changing MCP tools or APIs.
Changes:
- Waits for both the HTTP serve loop and graceful shutdown to finish.
- Preserves the five-second grace period, force-closes remaining connections on failure, and returns shutdown errors.
- Adds deterministic tests for response draining, timeout cancellation, listener errors, and cancellation before serving.
| File | Description |
|---|---|
pkg/http/shutdown_test.go |
Tests graceful draining and shutdown failure paths using in-memory connections. |
pkg/http/server.go |
Coordinates serving and shutdown, enforces the grace deadline, and propagates errors. |
Review effort: Max (adaptive)
|
@omgitsads could you review this Streamable HTTP shutdown fix when you have a chance? On SIGTERM, the server waits for active requests to finish within the existing five-second grace period. Both the drain and timeout paths passed local testing. If the diff looks good, could you also approve the fork workflows so CI can run? Would appreciate your feedback on this, thanks! |
Summary
Wait for active Streamable HTTP requests to finish during CLI shutdown. If the five-second grace period expires, close the remaining connections and return the shutdown error.
Why
On
SIGTERMorSIGINT,RunHTTPServerstartedhttp.Server.Shutdownin a goroutine. Go closes the listener before waiting for active handlers, soListenAndServereturnedhttp.ErrServerClosedwhile the drain was in progress. The CLI then loggedserver stopped gracefullyand could exit before the SDK wrote the MCP response. A client could receive EOF even when its GitHub API request could have completed inside the grace period. GoServer.ShutdowndocumentationThe reproduction used the Linux CLI from
4e6e6c053e48, a normal MCPget_mecall, and a local GitHub API stub. The stub held/api/v3/useruntil afterSIGTERMclosed the listener. With the response still blocked, the original process logged graceful shutdown and exited with status 0. The caller received EOF. Under the same sequence, the fixed process waited for the stub, delivered JSON-RPC result ID 42, and then exited with status 0.With the stub held past the full grace period, the fixed CLI reported
context deadline exceededand exited with status 1 after approximately five seconds. The MCP client connection closed and the GitHub stub observed request cancellation.What changed
net/httphandlers,net.Pipe, andtesting/synctest. The tests cover response delivery during drain, connection closure and request cancellation at the grace deadline, listener errors, and cancellation before serving. Restoring the old shutdown ordering in a temporary overlay made the drain and deadline regressions fail on their expected assertions.MCP impact
Prompts tested (tool changes only)
The transport reproduction uses the existing
get_metool against a loopback API stub.Security / limits
Tool renaming
Lint & tests
./script/linton Linux using the pinned golangci-lint v2.14.0../script/test(go test -race ./...) on Linux.go test -run '^TestServeHTTP' -count=100 -timeout 60s ./pkg/http,go test -v ./..., andUPDATE_TOOLSNAPS=true go test ./.... The verbose run passed 32 packages, 1,207 top-level tests, and 7,825 subtests. The snapshot pass produced no tracked changes.go test -race -run '^TestServeHTTP' -count=100 -timeout 60s ./pkg/http,go mod tidy -diff, and a CLI build. The CLI also built on Windows. The signal-to-response reproduction ran on Linux.Docs
script/generate-docsreproduced all five checked-in documentation files it generates.Skills
Streamable HTTP CLI mode was introduced in #1849.
Implementation powered by Codex. Reviewed and report written by Jon Zhang.