I've found a bug where there is a race condition w...
# ask-ai
e
I've found a bug where there is a race condition when the agent is speaking and am sending data using publish to the frontend. So that the data never completely send and the voice stops here is what happens 1. User responds to lesson prompt 2. LLM generates evaluation text 3. LLM calls
send_lesson_data()
tool 4. SIMULTANEOUSLY: TTS is generating audio and the audio capture system is actively reading frames 5.
send_lesson_data
publishes to data channel 6. CRASH POINT: FFI tries to capture audio frame but the handle has become invalid during the data channel operation 7. Error:
livekit_ffi::cabi - failed to handle request CaptureAudioFrame: InvalidRequest("handle not found")
8. Process becomes unresponsive and is killed So what I have done is add a delay so the daa is sent once the voice stops but there's not guarantee the voice will stop before the delay and prevent the crahs from happening. There has to be a solution right
👍 1
t
Thanks for reaching out, Brian. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It looks like the issue you're facing might be due to a race condition between publishing data and capturing audio frames. Don't worry, I’m here to help! Here are a few suggestions that could make things smoother: • Use Explicit Locking: Try implementing locks so that only one operation—like publishing data or capturing audio—happens at a time. This can really help prevent conflicts when accessing shared resources. • Separate Tasks: Consider running audio and data frame processing in separate asynchronous tasks. This way, one won’t block the other, making everything run more smoothly. • Buffer Management: Increasing the buffer size (for example,
queue_size_ms
) can give the audio frames more room to be handled properly, reducing the chance of capture issues. • Consistent Timestamps: Make sure that audio frames have consistent capture timestamps to keep everything in sync. Trying out these steps should help reduce the race condition and make your system more stable. If you’d like, I’m happy to walk you through any of these or answer any questions you have! Sources: Resolving "InvalidState - failed to capture frame" errors in AudioSource | Debugging Audio/Video Sync Issues in LiveKit Publishing | livekit-plugins/livekit-plugins-bithuman/livekit/plugins/bithuman/avatar.py | Camera & microphone | LiveKit docs