Skip to content

mcp: drain SSE messages when closing - #1192

Open
jstar0 wants to merge 1 commit into
modelcontextprotocol:mainfrom
jstar0:fix/1191-sse-close-drain
Open

mcp: drain SSE messages when closing#1192
jstar0 wants to merge 1 commit into
modelcontextprotocol:mainfrom
jstar0:fix/1191-sse-close-drain

Conversation

@jstar0

@jstar0 jstar0 commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #1191.

SSEServerTransport.Close previously closed the session signal without draining buffered client messages. Because Read and ServeHTTP select between the message queue and the closed signal, a closed session could deliver stale messages or acknowledge a POST with 202 Accepted even though the message would never be processed.

This change:

  • Drains buffered incoming messages when the server connection closes.
  • Checks the closed state before and after queue operations so close races return io.EOF or 400 session closed.
  • Adds regression tests for stale post-close reads and POST responses after close.

Tests:

  • go test ./mcp -run '^TestSSE(ServerConnCloseDrainsIncoming|ServerTransportServeHTTPAfterClose|Server|Handler_DiscoverFallsBackToInitialize|ClientTransport_HTTPErrors|405AllowHeader)$' -count=20
  • go test -race ./mcp -run '^TestSSE(ServerConnCloseDrainsIncoming|ServerTransportServeHTTPAfterClose|Server)$' -count=20
  • go vet ./...
  • go test ./... (the MCP package still has the pre-existing localhost protection failure where the 0.0.0.0 via localhost rejects evil.com case receives HTTP 200 instead of 403; all other packages pass)

Co-authored-by: openhands <openhands@all-hands.dev>
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.

sse: Close does not drain incoming; Read can deliver stale messages after Close and POST can return false 202

1 participant