agents icon indicating copy to clipboard operation
agents copied to clipboard

Segfault when two threads create AudioResampler at once (recorder + Google realtime)

Open advaitpaliwal opened this issue 2 months ago • 1 comments

A realtime agent sometimes crashes with a segfault right as the session starts, when two threads create an rtc.AudioResampler at the same moment. It happened in 3 of 10 local runs for us.

The two threads are Google realtime's input path on the main thread and the session recorder's encode thread:

Current thread (most recent call first):
  livekit/rtc/_ffi_client.py, line 295 in request
  livekit/rtc/audio_resampler.py, line 71 in __init__
  livekit/plugins/google/realtime/realtime_api.py, line 1668 in _resample_audio
  livekit/plugins/google/realtime/realtime_api.py, line 792 in push_audio
  livekit/agents/voice/agent_activity.py, line 1680 in push_audio

Thread (recorder_io encode thread):
  livekit/rtc/_ffi_client.py, line 295 in request
  livekit/rtc/audio_resampler.py, line 71 in __init__
  livekit/agents/voice/recorder_io/recorder_io.py, line 88 in _resample
  livekit/agents/voice/recorder_io/recorder_io.py, line 105 in push
  livekit/agents/voice/recorder_io/recorder_io.py, line 299 in _encode_thread

Fatal Python error: Segmentation fault

Versions: livekit 1.1.18, livekit-agents 1.8.2, livekit-plugins-google 1.8.2, Python 3.12.14, macOS arm64.

Starting the session with record={"audio": False} avoids it, since no recorder thread is created. Is livekit_ffi_request meant to be safe when resamplers are created from two threads at once?

advaitpaliwal avatar Sep 23 '26 20:09 advaitpaliwal

I can investigate this AudioResampler segfault.

Two threads creating rtc.AudioResampler at once (Google realtime input + recorder encode) crashing via livekit_ffi_request matches a non-thread-safe FFI init. I’ll try to reproduce with record on + Google realtime, check whether resampler creation needs a process-wide lock (or lazy shared resampler), and propose the smallest agents-side guard if the real fix is in livekit rtc.

record={"audio": False} avoiding it is a useful clue — will use that as the control.

HarshXAI avatar Sep 30 '26 04:09 HarshXAI