Skip to content

Commit

Permalink
Fix server shutdown on Python 3.12.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed May 18, 2023
1 parent 73394df commit 03d62c9
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/websockets/legacy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,18 +761,13 @@ async def _close(self, close_connections: bool) -> None:
# Stop accepting new connections.
self.server.close()

# Wait until self.server.close() completes.
await self.server.wait_closed()

# Wait until all accepted connections reach connection_made() and call
# register(). See https://bugs.python.org/issue34852 for details.
await asyncio.sleep(0)

if close_connections:
# Close OPEN connections with status code 1001. Since the server was
# closed, handshake() closes OPENING connections with an HTTP 503
# error. Wait until all connections are closed.

# Close OPEN connections with close code 1001. After server.close(),
# handshake() closes OPENING connections with an HTTP 503 error.
close_tasks = [
asyncio.create_task(websocket.close(1001))
for websocket in self.websockets
Expand All @@ -782,8 +777,10 @@ async def _close(self, close_connections: bool) -> None:
if close_tasks:
await asyncio.wait(close_tasks)

# Wait until all connection handlers are complete.
# Wait until all TCP connections are closed.
await self.server.wait_closed()

# Wait until all connection handlers terminate.
# asyncio.wait doesn't accept an empty first argument.
if self.websockets:
await asyncio.wait(
Expand Down

0 comments on commit 03d62c9

Please sign in to comment.