5-07-28 12:21:55,555 - ERROR asyncio - Unclosed cl...
# ask-ai
a
2025-07-28 122155,555 - ERROR asyncio - Unclosed client session client_session: <aiohttp.client.ClientSession object at 0x00000228D03D5810> 2025-07-28 122155,555 - ERROR asyncio - Unclosed connector connections: ['deque([(<aiohttp.client_proto.ResponseHandler object at 0x00000228D3AF3B30>, 1195137.8797273)])'] connector: <aiohttp.connector.TCPConnector object at 0x00000228D03D0640>
t
Thanks for reaching out, giri. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The error messages you are encountering indicate that there are unclosed client sessions and connectors in your application. This typically happens when resources like HTTP sessions or WebSocket connections are not properly closed. Below are some best practices and patterns to address this issue: • HTTP Session Cleanup: Ensure that HTTP sessions are properly closed using a centralized cleanup function. For example:
Copy code
async def _close_http_ctx() -> None:
    val = _ContextVar.get(None)
    if val is not None:
        logger.debug("http_session(): closing the httpclient ctx")
        await val().close()
        _ContextVar.set(None)
• WebSocket Connection Cleanup: Always close WebSocket connections in a
finally
block to ensure cleanup even if exceptions occur. For example:
Copy code
finally:
    if ws is not None:
        await ws.close()
• Graceful Task Cancellation: Use utility functions like
utils.aio.gracefully_cancel()
to properly cancel tasks and clean up resources. For example:
Copy code
finally:
    await utils.aio.gracefully_cancel(*tasks, wait_reconnect_task)
    tasks_group.cancel()
    tasks_group.exception()  # retrieve the exception
• Session Management: Centralize session management and ensure active sessions are closed properly. For example:
Copy code
async def _close_active_session(self) -> None:
    async with self._session_lock:
        if self._active_session:
            try:
                await self._active_session.close()
            except Exception as e:
                logger.warning(f"error closing session: {e}")
            finally:
                self._active_session = None
• Key Principles: ◦ Always use
finally
blocks for cleanup. ◦ Check for
None
before closing resources. ◦ Handle exceptions during cleanup to avoid masking original errors. Implementing these patterns should help prevent unclosed client session and connector errors. If you need further assistance or have specific implementation questions, feel free to provide additional details about your setup. Sources: livekit-plugins/livekit-plugins-azure/livekit/plugins/azure/stt.py