Skip to content

perf: clean up SSE session writer - #518

Closed
LeeEirc wants to merge 2 commits into
modelcontextprotocol:mainfrom
LeeEirc:perf_sse
Closed

perf: clean up SSE session writer#518
LeeEirc wants to merge 2 commits into
modelcontextprotocol:mainfrom
LeeEirc:perf_sse

Conversation

@LeeEirc

Copy link
Copy Markdown

Prevents memory leaks from stream writer

@nadeemcite

Copy link
Copy Markdown

Can you check if this can solve #514

@KludexKludex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the way to go. BackgroundTasks only works on 2xx responses, but also, this kind of logic is more suited as a middleware. Where do we create the session_id? We should probably drop it in the same place.

@LeeEirc

Copy link
Copy Markdown
Author

The SseServerTransport class contains a memory leak due to improper cleanup of the _read_stream_writers collection. This issue needs to be addressed to prevent resource exhaustion during extended runtime.

 async def connect_sse(self, scope: Scope, receive: Receive, send: Send):
......
session_id = uuid4()
session_uri = f"{quote(self._endpoint)}?session_id={session_id.hex}"
self._read_stream_writers[session_id] = read_stream_writer
logger.debug(f"Created new session with ID: {session_id}")
.....

@LeeEirc
LeeEirc requested a review from KludexApril 15, 2025 15:20
Comment threadsrc/mcp/server/sse.py Outdated
@lcamhoa

Copy link
Copy Markdown

@Kludex I make a PR for this one also #582 to clean up the memory writer when sse disconnect . SSE Disconnect event handler is new feature I request on sysid/sse-starlette#127

@LeeEircLeeEirc closed this May 4, 2025
Sign up for freeto 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.

5 participants

@LeeEirc@nadeemcite@lcamhoa@cclauss@Kludex