Skip to content

fix(mcp): close the SSE connection when the legacy transport is dropped - #514

Merged
mrnasil merged 1 commit into
mainfrom
fix/mcp-sse-reader-leak
Nov 27, 2026
Merged

mrnasil merged 1 commit into
mainfrom
fix/mcp-sse-reader-leak

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 27, 2026

Copy link
Copy Markdown
Member

Problem

LegacySseTransport kept its reader task as a JoinHandle. Dropping a JoinHandle only detaches the task. The reader notices a dropped receiver only when it has an event to send, and a keep-alive comment is not an event. So a server that only sends keep-alives kept one task and one open HTTP connection per dropped transport, for the life of the process. A connect that timed out waiting for the endpoint event left the same task behind.

Fix

The handle lives in SseReader, whose Drop aborts the task. The response body and its connection go with the transport, on both the normal drop and the failed-connect path.

Tests

  • dropping_the_legacy_transport_closes_its_sse_connection and a_connect_that_never_gets_an_endpoint_closes_its_sse_connection: a raw TCP server sends keep-alives until a write fails. Both fail on main (connection still open after 5 s) and pass here.
  • sse_reader_stops_by_itself_when_nobody_receives: aborting early removed the only indirect coverage of the reader's own exit line, so it gets a direct test.

Verification

  • cargo test -p whycodes-mcp: 79 + 10 passed. Clippy, fmt, budget scripts: green.
  • Coverage: measured the mcp crate locally with the CI flags (-skip-expansions, after cargo llvm-cov clean). http.rs misses 6 lines here on both main and this branch, all warn! field lines that this Windows host reports and CI does not. The full scripts/coverage.sh workspace run was not done locally.
  • docs/knowhow.md has the entry.

🤖 Generated with Claude Code

LegacySseTransport kept its reader task as a JoinHandle. Dropping a
JoinHandle only detaches the task, and the reader notices a dropped
receiver only when it has an event to send. A server that just sends
keep-alive comments therefore kept one task and one open connection per
dropped transport for the life of the process. A connect that timed out
waiting for the endpoint event left the same task behind.

The handle now lives in SseReader, which aborts the task on drop.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@mrnasil
mrnasil merged commit 1c5944a into main Nov 27, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/mcp-sse-reader-leak branch November 27, 2026 19:19
@mrnasil mrnasil mentioned this pull request Nov 28, 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.

1 participant