Skip to content

fix(http): wait for active requests during shutdown - #3882

Open
jonxsong wants to merge 1 commit into
github:mainfrom
jonxsong:codex/fix-http-graceful-shutdown
Open

jonxsong wants to merge 1 commit into
github:mainfrom
jonxsong:codex/fix-http-graceful-shutdown

Conversation

@jonxsong

Copy link
Copy Markdown

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 SIGTERM or SIGINT, RunHTTPServer started http.Server.Shutdown in a goroutine. Go closes the listener before waiting for active handlers, so ListenAndServe returned http.ErrServerClosed while the drain was in progress. The CLI then logged server stopped gracefully and 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. Go Server.Shutdown documentation

The reproduction used the Linux CLI from 4e6e6c053e48, a normal MCP get_me call, and a local GitHub API stub. The stub held /api/v3/user until after SIGTERM closed 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 exceeded and exited with status 1 after approximately five seconds. The MCP client connection closed and the GitHub stub observed request cancellation.

What changed

  • Run the HTTP serve loop in a goroutine and wait for both the serve loop and graceful shutdown before returning to the CLI.
  • Keep the five-second deadline independent of the signal context so in-flight HTTP requests can finish. After a shutdown error, force-close any remaining connections and surface the error.
  • Exercise four paths using real net/http handlers, net.Pipe, and testing/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

  • No tool or API changes. The patch is limited to the lifecycle of the self-hosted HTTP CLI. The Go SDK continues to own Streamable HTTP request and session handling, and tool calls still use their request contexts.

Prompts tested (tool changes only)

The transport reproduction uses the existing get_me tool against a loopback API stub.

Security / limits

  • Auth / permissions considered. Requests continue to pass through the existing bearer-token middleware and tool authorization. Shutdown retains its five-second grace period.

Tool renaming

  • I am not renaming tools as part of this PR.

Lint & tests

  • Linted locally with ./script/lint on Linux using the pinned golangci-lint v2.14.0.
  • Tested locally with ./script/test (go test -race ./...) on Linux.
  • Windows, Go 1.27.4 — go test -run '^TestServeHTTP' -count=100 -timeout 60s ./pkg/http, go test -v ./..., and UPDATE_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.
  • Linux, Go 1.27.4 — 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

  • Not needed. script/generate-docs reproduced all five checked-in documentation files it generates.

Skills

  • Confirmed no bundled skill applies to the HTTP CLI shutdown lifecycle.

Streamable HTTP CLI mode was introduced in #1849.

AI assistance was used to investigate and reproduce the defect, author the fix and tests, review the diff, and draft this description.

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.
@jonxsong
jonxsong requested a review from a team as a code owner November 18, 2026 08:25
Copilot AI adaptive review requested due to automatic review settings November 18, 2026 08:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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)

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.

2 participants