1010
1111 # Create Starlette routes for SSE and message handling
1212 routes = [
13- Route("/sse", endpoint=handle_sse),
13+ Route("/sse", endpoint=handle_sse, methods=["GET"] ),
1414 Mount("/messages/", app=sse.handle_post_message),
1515 ]
1616
@@ -22,12 +22,18 @@ async def handle_sse(request):
2222 await app.run(
2323 streams[0], streams[1], app.create_initialization_options()
2424 )
25+ # Return empty response to avoid NoneType error
26+ return Response()
2527
2628 # Create and run Starlette app
2729 starlette_app = Starlette(routes=routes)
2830 uvicorn.run(starlette_app, host="0.0.0.0", port=port)
2931```
3032
33+ Note: The handle_sse function must return a Response to avoid a "TypeError: 'NoneType'
34+ object is not callable" error when client disconnects. The example above returns
35+ an empty Response() after the SSE connection ends to fix this.
36+
3137See SseServerTransport class documentation for more details.
3238"""
3339
@@ -120,11 +126,22 @@ async def sse_writer():
120126 )
121127
122128 async with anyio .create_task_group () as tg :
123- response = EventSourceResponse (
124- content = sse_stream_reader , data_sender_callable = sse_writer
125- )
129+
130+ async def response_wrapper (scope : Scope , receive : Receive , send : Send ):
131+ """
132+ The EventSourceResponse returning signals a client close / disconnect.
133+ In this case we close our side of the streams to signal the client that
134+ the connection has been closed.
135+ """
136+ await EventSourceResponse (
137+ content = sse_stream_reader , data_sender_callable = sse_writer
138+ )(scope , receive , send )
139+ await read_stream_writer .aclose ()
140+ await write_stream_reader .aclose ()
141+ logging .debug (f"Client session disconnected { session_id } " )
142+
126143 logger .debug ("Starting SSE response task" )
127- tg .start_soon (response , scope , receive , send )
144+ tg .start_soon (response_wrapper , scope , receive , send )
128145
129146 logger .debug ("Yielding read and write streams" )
130147 yield (read_stream , write_stream )
0 commit comments