From a15e032024d0962beae5d94b8e9dcb9136efaa99 Mon Sep 17 00:00:00 2001 From: Alessandro Astone Date: Fri, 11 Oct 2024 15:40:10 +0200 Subject: [PATCH] fix(src): Close connections to API clients when shutting down This effectively re-implements asyncio.Server::close_clients which is only available in python3.13. In python3.12 asyncio.Server::wait_closed will hang unless these sockets are closed: https://github.com/python/cpython/issues/104344 --- src/wsdd.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wsdd.py b/src/wsdd.py index e102422..b087862 100755 --- a/src/wsdd.py +++ b/src/wsdd.py @@ -1127,6 +1127,7 @@ class ApiServer: def __init__(self, aio_loop: asyncio.AbstractEventLoop, listen_address: bytes, address_monitor: 'NetworkAddressMonitor') -> None: self.server = None + self.clients = set() self.address_monitor = address_monitor # defer server creation @@ -1146,6 +1147,7 @@ async def create_server(self, aio_loop: asyncio.AbstractEventLoop, listen_addres self.on_connect, path=listen_address)) async def on_connect(self, read_stream: asyncio.StreamReader, write_stream: asyncio.StreamWriter) -> None: + self.clients.add(write_stream.transport) while True: try: line = await read_stream.readline() @@ -1219,6 +1221,8 @@ async def cleanup(self) -> None: await self.create_task if self.server: self.server.close() + for transport in self.clients: + transport.close() await self.server.wait_closed()