Skip to content

Commit 60381d2

Browse files
committed
Fix exception chaining for ConnectionClosed.
1 parent 7b19e79 commit 60381d2

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/websockets/asyncio/connection.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,13 +906,17 @@ def connection_made(self, transport: asyncio.BaseTransport) -> None:
906906

907907
def connection_lost(self, exc: Exception | None) -> None:
908908
self.protocol.receive_eof() # receive_eof is idempotent
909-
self.recv_messages.close()
909+
910+
# Abort recv() and pending pings with a ConnectionClosed exception.
911+
# Set recv_exc first to get proper exception reporting.
910912
self.set_recv_exc(exc)
913+
self.recv_messages.close()
911914
self.abort_pings()
912915
# If keepalive() was waiting for a pong, abort_pings() terminated it.
913916
# If it was sleeping until the next ping, we need to cancel it now
914917
if self.keepalive_task is not None:
915918
self.keepalive_task.cancel()
919+
916920
# If self.connection_lost_waiter isn't pending, that's a bug, because:
917921
# - it's set only here in connection_lost() which is called only once;
918922
# - it must never be canceled.

0 commit comments

Comments
 (0)