Channel closed error understanding

So I’ve been seeing this error log for sometime from sentry for a production agent. Its popping up for a long time, but I’m curious on what the error is about. Looks like the error is not directly impacting the experience but i am not too sure.

Error:

Traceback (most recent call last):
  File "/app/.venv/lib/python3.10/site-packages/livekit/agents/utils/log.py", line 17, in async_fn_logs
    return await fn(*args, **kwargs)
  File "/app/.venv/lib/python3.10/site-packages/livekit/agents/voice/generation.py", line 410, in _audio_forwarding_task
    audio_output.flush()
  File "/app/.venv/lib/python3.10/site-packages/livekit/agents/voice/recorder_io/recorder_io.py", line 512, in flush
    self.next_in_chain.flush()
  File "/app/.venv/lib/python3.10/site-packages/livekit/agents/voice/transcription/synchronizer.py", line 568, in flush
    self._synchronizer._impl.end_audio_input()
  File "/app/.venv/lib/python3.10/site-packages/livekit/agents/voice/transcription/synchronizer.py", line 194, in end_audio_input
    self._audio_data.sr_stream.end_input()
  File "/app/.venv/lib/python3.10/site-packages/livekit/agents/voice/transcription/_speaking_rate.py", line 266, in end_input
    self.flush()
  File "/app/.venv/lib/python3.10/site-packages/livekit/agents/voice/transcription/_speaking_rate.py", line 262, in flush
    self._input_ch.send_nowait(self._FlushSentinel())
  File "/app/.venv/lib/python3.10/site-packages/livekit/agents/utils/aio/channel.py", line 92, in send_nowait
    raise ChanClosed
livekit.agents.utils.aio.channel.ChanClosed

JSON log :

{'args': (), 'level': 'ERROR', 'record': {'created': 1776723606.940273, 'funcName': 'async_fn_logs', 'lineno': 22, 'module': 'log', 'name': 'livekit.agents', 'pathname': '/app/.venv/lib/python3.10/site-packages/livekit/agents/utils/log.py', 'process': 16614, 'processName': 'job_proc', 'relativeCreated': 43005033.103227615, 'thread': 140017757242112, 'threadName': 'MainThread'}, 'extra': {}, 'context': {}, 'msg': 'Error in _audio_forwarding_task'}

I am curious on why this error is popping up at random times, so that i could handle that gracefully. Is it because, agent tries to send an audio but session is closed or something on those lines of non existent session.

cc @CWilson @darryncampbell would love your inputs

This looks related to shutting down.

The expected way to end cleanly is via session.shutdown(drain=True) so pending speech is flushed before close, as described in Job lifecycle – Ending the session.