@@ -262,8 +262,8 @@ def close_session(reason: Exception | None) -> None:
262262 # during the cleanup procedure.
263263
264264 self ._terminating_task = asyncio .create_task (
265- self .close (reason , current_state = current_state ),
266- )
265+ self .close (reason , current_state = current_state ),
266+ )
267267
268268 def transition_connecting () -> None :
269269 if self ._state in TerminalStates :
@@ -396,7 +396,9 @@ async def _enqueue_message(
396396 # Wake up buffered_message_sender
397397 self ._process_messages .set ()
398398
399- async def close (self , reason : Exception | None = None , current_state : SessionState | None = None ) -> None :
399+ async def close (
400+ self , reason : Exception | None = None , current_state : SessionState | None = None
401+ ) -> None :
400402 """Close the session and all associated streams."""
401403 logger .info (
402404 f"{ self .session_id } closing session to { self ._server_id } , ws: { self ._ws } "
@@ -435,9 +437,28 @@ async def close(self, reason: Exception | None = None, current_state: SessionSta
435437 )
436438 stream_meta ["release_backpressured_waiter" ]()
437439 # Before we GC the streams, let's wait for all tasks to be closed gracefully.
438- await asyncio .gather (
439- * [stream_meta ["output" ].join () for stream_meta in self ._streams .values ()]
440- )
440+ try :
441+ async with asyncio .timeout (
442+ self ._transport_options .shutdown_all_streams_timeout_ms
443+ ):
444+ # Block for backpressure and emission errors from the ws
445+ await asyncio .gather (
446+ * [
447+ stream_meta ["output" ].join ()
448+ for stream_meta in self ._streams .values ()
449+ ]
450+ )
451+ except asyncio .TimeoutError :
452+ spans : list [Span ] = [
453+ stream_meta ["span" ]
454+ for stream_meta in self ._streams .values ()
455+ if not stream_meta ["output" ].closed ()
456+ ]
457+ span_ids = [span .get_span_context ().span_id for span in spans ]
458+ logger .exception (
459+ "Timeout waiting for output streams to finallize" ,
460+ extra = {"span_ids" : span_ids },
461+ )
441462 self ._streams .clear ()
442463
443464 if self ._ws :
0 commit comments