@@ -312,7 +312,10 @@ async def send_notification(
312
312
message = JSONRPCMessage (jsonrpc_notification ),
313
313
metadata = ServerMessageMetadata (related_request_id = related_request_id ) if related_request_id else None ,
314
314
)
315
- await self ._write_stream .send (session_message )
315
+ try :
316
+ await self ._write_stream .send (session_message )
317
+ except (anyio .BrokenResourceError , anyio .ClosedResourceError ):
318
+ logging .debug ("Discarding notification due to closed stream" )
316
319
317
320
async def _send_response (self , request_id : RequestId , response : SendResultT | ErrorData ) -> None :
318
321
if isinstance (response , ErrorData ):
@@ -400,16 +403,14 @@ async def _receive_loop(self) -> None:
400
403
await self ._handle_incoming (notification )
401
404
except Exception as e :
402
405
# For other validation errors, log and continue
403
- logging .warning (
404
- f"Failed to validate notification: { e } . " f"Message was: { message .message .root } "
405
- )
406
+ logging .warning (f"Failed to validate notification: { e } . Message was: { message .message .root } " )
406
407
else : # Response or error
407
408
stream = self ._response_streams .pop (message .message .root .id , None )
408
409
if stream :
409
410
await stream .send (message .message .root )
410
411
else :
411
412
await self ._handle_incoming (
412
- RuntimeError ("Received response with an unknown " f" request ID: { message } " )
413
+ RuntimeError (f "Received response with an unknown request ID: { message } " )
413
414
)
414
415
415
416
# after the read stream is closed, we need to send errors
0 commit comments