rotel-langchain | {"message": "Task exception was...
# ask-ai
m
rotel-langchain | {"message": "Task exception was never retrieved\nfuture: <Task finished name='Task-21475' coro=<BackgroundAudioPlayer._play_task() done, defined at /usr/local/lib/python3.11/site-packages/livekit/agents/utils/log.py:13> exception=RuntimeError('aclose(): asynchronous generator is already running')>\nTraceback (most recent call last):\n File \"/usr/local/lib/python3.11/site-packages/livekit/agents/utils/log.py\", line 16, in async_fn_logs\n return await fn(*args, **kwargs)\n ^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.11/site-packages/livekit/agents/voice/background_audio.py\", line 367, in _play_task\n await gen.aclose()\nRuntimeError: aclose(): asynchronous generator is already running", "level": "ERROR", "name": "asyncio", "pid": 147, "job_id": "AJ_WdvHYU4itv96", "timestamp": "2025
t
Thanks for reaching out, Ayse. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! This error pops up because of a race condition in the
BackgroundAudioPlayer._play_task
method when it tries to close an asynchronous generator that’s still running. It usually happens when a task is cancelled while audio is playing. Here’s how you can fix it: 1. Make sure to handle task cancellation properly by updating the
finally
block in
_play_task
to check if the task was cancelled before calling
aclose()
. 2. Change the cleanup order in the
BackgroundAudioPlayer.aclose()
method so it stops play handles before cancelling tasks. 3. If you can’t change the LiveKit code, you can catch and ignore the specific
RuntimeError
in your app when calling
aclose()
. If you want, I’m here to help with more details or code examples—just let me know! Sources: livekit-agents/livekit/agents/voice/agent_session.py