@@ -353,6 +353,7 @@ async def _enqueue_message(
353353 # session is closing / closed, raise
354354 raise SessionClosedRiverServiceException (
355355 "river session is closed, dropping message" ,
356+ stream_id ,
356357 )
357358
358359 # Begin critical section: Avoid any await between here and _send_buffer.append
@@ -448,14 +449,15 @@ async def do_close() -> None:
448449
449450 await self ._task_manager .cancel_all_tasks ()
450451
451- for stream_meta in self ._streams .values ():
452+ for stream_id , stream_meta in self ._streams .items ():
452453 stream_meta ["output" ].close ()
453454 # Wake up backpressured writers
454455 try :
455456 stream_meta ["error_channel" ].put_nowait (
456457 reason
457458 or SessionClosedRiverServiceException (
458459 "river session is closed" ,
460+ stream_id ,
459461 )
460462 )
461463 except ChannelFull :
@@ -751,6 +753,13 @@ async def send_rpc[R, A](
751753 # Block for backpressure and emission errors from the ws
752754 await backpressured_waiter ()
753755 result = await anext (output )
756+ except asyncio .CancelledError :
757+ await self ._send_cancel_stream (
758+ stream_id = stream_id ,
759+ message = "RPC cancelled" ,
760+ span = span ,
761+ )
762+ raise
754763 except asyncio .TimeoutError as e :
755764 await self ._send_cancel_stream (
756765 stream_id = stream_id ,
@@ -835,6 +844,13 @@ async def send_upload[I, R, A](
835844 payload = payload ,
836845 span = span ,
837846 )
847+ except asyncio .CancelledError :
848+ await self ._send_cancel_stream (
849+ stream_id = stream_id ,
850+ message = "Upload cancelled" ,
851+ span = span ,
852+ )
853+ raise
838854 except Exception as e :
839855 # If we get any exception other than WebsocketClosedException,
840856 # cancel the stream.
@@ -916,6 +932,13 @@ async def send_subscription[I, E, A](
916932 continue
917933 yield response_deserializer (item ["payload" ])
918934 await self ._send_close_stream (stream_id , span )
935+ except asyncio .CancelledError :
936+ await self ._send_cancel_stream (
937+ stream_id = stream_id ,
938+ message = "Subscription cancelled" ,
939+ span = span ,
940+ )
941+ raise
919942 except Exception as e :
920943 await self ._send_cancel_stream (
921944 stream_id = stream_id ,
@@ -1002,6 +1025,15 @@ async def _encode_stream() -> None:
10021025 # ... block the outer function until the emitter is finished emitting,
10031026 # possibly raising a terminal exception.
10041027 await emitter_task
1028+ except asyncio .CancelledError as e :
1029+ await self ._send_cancel_stream (
1030+ stream_id = stream_id ,
1031+ message = "Stream cancelled" ,
1032+ span = span ,
1033+ )
1034+ if emitter_task .done () and (err := emitter_task .exception ()):
1035+ raise e from err
1036+ raise
10051037 except Exception as e :
10061038 await self ._send_cancel_stream (
10071039 stream_id = stream_id ,
@@ -1288,6 +1320,7 @@ async def _recv_from_ws(
12881320 # the outer loop.
12891321 await transition_no_connection ()
12901322 break
1323+ msg : TransportMessage | str | None = None
12911324 try :
12921325 msg = parse_transport_msg (message )
12931326 logger .debug (
@@ -1367,19 +1400,27 @@ async def _recv_from_ws(
13671400 stream_meta ["output" ].close ()
13681401 except OutOfOrderMessageException :
13691402 logger .exception ("Out of order message, closing connection" )
1403+ stream_id = "unknown"
1404+ if isinstance (msg , TransportMessage ):
1405+ stream_id = msg .streamId
13701406 close_session (
13711407 SessionClosedRiverServiceException (
1372- "Out of order message, closing connection"
1408+ "Out of order message, closing connection" ,
1409+ stream_id ,
13731410 )
13741411 )
13751412 continue
13761413 except InvalidMessageException :
13771414 logger .exception (
13781415 "Got invalid transport message, closing session" ,
13791416 )
1417+ stream_id = "unknown"
1418+ if isinstance (msg , TransportMessage ):
1419+ stream_id = msg .streamId
13801420 close_session (
13811421 SessionClosedRiverServiceException (
1382- "Out of order message, closing connection"
1422+ "Out of order message, closing connection" ,
1423+ stream_id ,
13831424 )
13841425 )
13851426 continue
0 commit comments