Fix process_task not ending correctly (#271)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Jesse Hills 2022-10-07 09:37:47 +13:00 committed by GitHub
parent 4363960be3
commit 712f23d2b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -107,6 +107,8 @@ class APIConnection:
self._to_process: asyncio.Queue[message.Message] = asyncio.Queue() self._to_process: asyncio.Queue[message.Message] = asyncio.Queue()
self._process_task: Optional[asyncio.Task[None]] = None
async def _cleanup(self) -> None: async def _cleanup(self) -> None:
"""Clean up all resources that have been allocated. """Clean up all resources that have been allocated.
@ -116,6 +118,12 @@ class APIConnection:
await self._frame_helper.close() await self._frame_helper.close()
self._frame_helper = None self._frame_helper = None
if self._process_task is not None:
self._process_task.cancel()
with suppress(asyncio.CancelledError):
await self._process_task
self._process_task = None
if self._socket is not None: if self._socket is not None:
self._socket.close() self._socket.close()
self._socket = None self._socket = None
@ -202,7 +210,7 @@ class APIConnection:
# Create read loop # Create read loop
asyncio.create_task(self._read_loop()) asyncio.create_task(self._read_loop())
# Create process loop # Create process loop
asyncio.create_task(self._process_loop()) self._process_task = asyncio.create_task(self._process_loop())
async def _connect_hello(self) -> None: async def _connect_hello(self) -> None:
"""Step 4 in connect process: send hello and get api version.""" """Step 4 in connect process: send hello and get api version."""