Skip to content

Commit 03d62c9

Browse files
committed
Fix server shutdown on Python 3.12.
Ref python/cpython#79033. Fix #1356.
1 parent 73394df commit 03d62c9

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/websockets/legacy/server.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -761,18 +761,13 @@ async def _close(self, close_connections: bool) -> None:
761761
# Stop accepting new connections.
762762
self.server.close()
763763

764-
# Wait until self.server.close() completes.
765-
await self.server.wait_closed()
766-
767764
# Wait until all accepted connections reach connection_made() and call
768765
# register(). See https://bugs.python.org/issue34852 for details.
769766
await asyncio.sleep(0)
770767

771768
if close_connections:
772-
# Close OPEN connections with status code 1001. Since the server was
773-
# closed, handshake() closes OPENING connections with an HTTP 503
774-
# error. Wait until all connections are closed.
775-
769+
# Close OPEN connections with close code 1001. After server.close(),
770+
# handshake() closes OPENING connections with an HTTP 503 error.
776771
close_tasks = [
777772
asyncio.create_task(websocket.close(1001))
778773
for websocket in self.websockets
@@ -782,8 +777,10 @@ async def _close(self, close_connections: bool) -> None:
782777
if close_tasks:
783778
await asyncio.wait(close_tasks)
784779

785-
# Wait until all connection handlers are complete.
780+
# Wait until all TCP connections are closed.
781+
await self.server.wait_closed()
786782

783+
# Wait until all connection handlers terminate.
787784
# asyncio.wait doesn't accept an empty first argument.
788785
if self.websockets:
789786
await asyncio.wait(

0 commit comments

Comments
 (0)