@@ -312,7 +312,10 @@ async def send_notification(
312312 message = JSONRPCMessage (jsonrpc_notification ),
313313 metadata = ServerMessageMetadata (related_request_id = related_request_id ) if related_request_id else None ,
314314 )
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" )
316319
317320 async def _send_response (self , request_id : RequestId , response : SendResultT | ErrorData ) -> None :
318321 if isinstance (response , ErrorData ):
@@ -400,16 +403,14 @@ async def _receive_loop(self) -> None:
400403 await self ._handle_incoming (notification )
401404 except Exception as e :
402405 # 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 } " )
406407 else : # Response or error
407408 stream = self ._response_streams .pop (message .message .root .id , None )
408409 if stream :
409410 await stream .send (message .message .root )
410411 else :
411412 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 } " )
413414 )
414415
415416 # after the read stream is closed, we need to send errors
0 commit comments