Skip to content

Commit 11d0456

Browse files
committed
Fix leaked anyio streams
1 parent a558ab6 commit 11d0456

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/mcp/server/streamable_http.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ async def _validate_accept_header(self, request: Request, scope: Scope, send: Se
427427
return False
428428
return True
429429

430-
async def _handle_post_request(self, scope: Scope, request: Request, receive: Receive, send: Send) -> None:
430+
async def _handle_post_request(self, scope: Scope, request: Request, receive: Receive, send: Send) -> None: # noqa: PLR0915
431431
"""Handle POST requests containing JSON-RPC messages."""
432432
writer = self._read_stream_writer
433433
if writer is None: # pragma: no cover
@@ -620,8 +620,9 @@ async def sse_writer():
620620
except Exception:
621621
logger.exception("SSE response error")
622622
await sse_stream_writer.aclose()
623-
await sse_stream_reader.aclose()
624623
await self._clean_up_memory_streams(request_id)
624+
finally:
625+
await sse_stream_reader.aclose()
625626

626627
except Exception as err: # pragma: no cover
627628
logger.exception("Error handling POST request")
@@ -722,9 +723,10 @@ async def standalone_sse_writer():
722723
await response(request.scope, request.receive, send)
723724
except Exception:
724725
logger.exception("Error in standalone SSE response")
726+
await self._clean_up_memory_streams(GET_STREAM_KEY)
727+
finally:
725728
await sse_stream_writer.aclose()
726729
await sse_stream_reader.aclose()
727-
await self._clean_up_memory_streams(GET_STREAM_KEY)
728730

729731
async def _handle_delete_request(self, request: Request, send: Send) -> None: # pragma: no cover
730732
"""Handle DELETE requests for explicit session termination."""

0 commit comments

Comments
 (0)