frankie567 / httpx-ws

WebSocket support for HTTPX
https://frankie567.github.io/httpx-ws/
MIT License
110 stars 17 forks source link

tasks not awaited when closing the connection #49

Closed zsorizs closed 9 months ago

zsorizs commented 1 year ago

Describe the bug

In our tests we were getting warnings that tasks _background_receive_task and _background_keepalive_ping_task were terminated while pending.

Expected behavior

All tasks should be awaited to avoid warnings.

Configuration

Possible fix

The next patch fixes the issue for us:

diff --git a/httpx_ws/_api.py b/httpx_ws/_api.py
index 5abfebe..cb51a34 100644
--- a/httpx_ws/_api.py
+++ b/httpx_ws/_api.py
@@ -924,6 +924,7 @@ class AsyncWebSocketSession:
             except httpcore.WriteError:
                 pass
         await self.stream.aclose()
+        await asyncio.gather(self._background_receive_task, self._background_keepalive_ping_task)

     async def _background_receive(self, max_bytes: int) -> None:
         """
frankie567 commented 1 year ago

So, can you confirm it happens when using the ASGIWebSocketTransport?

zsorizs commented 1 year ago

So, can you confirm it happens when using the ASGIWebSocketTransport?

Yes, I'm using that transport.

frankie567 commented 9 months ago

I think this is fixed indirectly by #58