chore: filter upstream MCP-SDK race-condition log noise on session teardown#6
Merged
romainvaltier-work merged 2 commits intoMay 4, 2026
Merged
Conversation
…ardown The mcp.server.streamable_http logger emits an ERROR with a full ClosedResourceError traceback on every session teardown — when a client opens an SSE stream via GET /mcp and immediately calls DELETE /mcp, the standalone_sse_writer task is mid-checkpoint while the GET stream gets closed by cleanup. The DELETE itself returns 200, the session ends cleanly, the response reaches the client; the trace is purely log noise. Upstream PR #1384 (merged 2025-12-04) added explicit ClosedResourceError handling to the sibling message_router but did not port the same pattern to standalone_sse_writer. Until upstream catches up, drop just those records via a logging.Filter scoped to the upstream logger. The filter matches the exact upstream message string + ClosedResourceError exc_info — every other ERROR from that logger, including legitimate streamable-HTTP failures, passes through. Filter is installed only when the wrapper runs under streamable-http or sse transport; stdio is unaffected.
…tests Round-1 review fixes for PR #6: - Use module-level `import logging` instead of locally aliasing inside `_run` (`logging` is already imported at the top of server.py). - Make race-filter attachment idempotent — re-entry into `_run` no longer stacks duplicate filters on the upstream logger. - Drop the `# noqa: A003` annotation; rule A is not in this project's ruff selection so the comment was inert. - New `TestRaceFilterInstallation` class in test_run_wiring.py with a cleanup fixture and three cases: filter attached on streamable-http, not attached on stdio, attachment is idempotent across two `_run` invocations. Closes the wiring loop the same way the bearer middleware attachment is verified.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The `mcp.server.streamable_http` logger emits an ERROR with a full `anyio.ClosedResourceError` traceback on every session teardown (open SSE via `GET /mcp` → terminate via `DELETE /mcp` while the standalone SSE writer is mid-`checkpoint`). The `DELETE` itself returns 200, the session ends cleanly, the response reaches the client — the trace is purely log noise, but it bloats logs and dashboards.
Upstream PR modelcontextprotocol/python-sdk#1384 (merged 2025-12-04) added explicit `ClosedResourceError` handling to the sibling `message_router`, but the same pattern hasn't been ported to `standalone_sse_writer` yet. Until it is, drop just those records via a tightly-scoped `logging.Filter`.
Approach
`StandaloneSseWriterRaceFilter` matches exactly:
Every other ERROR from that logger — including legitimate streamable-HTTP failures and even `Error in standalone SSE writer` records with non-`ClosedResourceError` exceptions — passes through unchanged.
The filter is installed only when the wrapper runs under `streamable-http` or `sse` transport; stdio is unaffected.
Test plan
Notes