average-architect-28889
07/29/2025, 12:08 PMtall-airline-16756
07/29/2025, 12:08 PMtall-airline-16756
07/29/2025, 12:09 PMhttp_context.http_session() to automatically manage and clean up sessions. For example:
from livekit.agents.utils import http_context
# Get a managed session
session = http_context.http_session()
• Pass Sessions as Parameters: Instead of creating sessions internally, pass them as constructor parameters. This ensures better control and cleanup:
def __init__(self, http_session: aiohttp.ClientSession | None = None):
self._session = http_session
• Use Session Properties: Implement session properties with fallback to managed sessions:
@property
def session(self) -> aiohttp.ClientSession:
if not self._session:
self._session = utils.http_context.http_session()
return self._session
• Ensure Proper Cleanup: Always close WebSocket connections and sessions in finally blocks:
finally:
if ws is not None:
await ws.close()
These practices should help address the errors you're encountering. If the issue persists, you may need to review your code for any instances where sessions or connectors are being created but not properly closed.
Sources: livekit-plugins/livekit-plugins-lmnt/livekit/plugins/lmnt/tts.py