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

mcp: drain SSE messages when closing

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 and checks the closed state around queue operations, so close races return io.EOF or 400 session closed. It adds regression tests for stale post-close reads and POST responses after close.

Tests:

  • go test -race ./mcp -run '^(TestSSEServerConnCloseDrainsIncoming|TestSSEServerTransportServeHTTPAfterClose)$' -count=50 — passed.
  • go test ./... — passed on the rebased head.
  • go vet ./... — passed.
  • gofmt -d mcp/sse.go mcp/sse_test.go — no changes reported.

Hosted checks for rebased head fd3a248297cd62389764587e2949a3cb988c6611 passed, including Test on Go 1.25 and 1.26, race-test, lint, client and server conformance, Analyze, and CodeQL.

Fixes #1191

@jstar0
jstar0 force-pushed the fix/1191-sse-close-drain branch from 44aeebb to fd3a248 Compare September 12, 2026 12:33
@jstar0

jstar0 commented Sep 12, 2026

Copy link
Copy Markdown
Contributor Author

Rebased the single commit onto current main at 5bc078a. The fix remains limited to mcp/sse.go and mcp/sse_test.go. Focused race tests (50 runs), go test ./..., go vet ./..., and gofmt all pass; hosted checks for fd3a248 are green across Go 1.25/1.26, race, lint, client/server conformance, Analyze, and CodeQL. I updated the PR description to reflect these results. Would appreciate review when convenient.

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