[2025-12-09 22:51:13] [ERROR] livekit.agents: fail...
# ask-ai
e
[2025-12-09 225113] [ERROR] livekit.agents: failed to perform clear buffer rpc Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/livekit/agents/voice/avatar/_datastream_io.py", line 174, in _clear_buffer_task await self._room.local_participant.perform_rpc( File "/usr/local/lib/python3.11/site-packages/livekit/rtc/participant.py", line 328, in perform_rpc raise RpcError._from_proto(cb.perform_rpc.error) livekit.rtc.rpc.RpcError: Failed to send [2025-12-09 225113] [ERROR] livekit: livekit:roomparticipantlocal participant786livekitroomparticipant:local_participant - Failed to publish RPC request: engine: connection error: engine is closed
t
Thanks for reaching out, Guess. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m looking at the "failed to perform clear buffer rpc" + "engine is closed" errors you shared. That combo means the RPC was attempted after the room/engine connection had already closed or disconnected. What you can check and try next: • Confirm when the RPC is being called: ensure
clear buffer
RPCs run only while the local participant/room is connected (not during shutdown or after
Room.disconnect()
). • Add a guard before calling
perform_rpc
to check the connection state (only call if room/participant is still connected). • If disconnects are expected, move buffer-cleanup to your disconnect/teardown flow so you don’t call RPCs after the engine closes. • Add retry or error handling around
perform_rpc
to catch
RpcError
and skip or retry when the engine is closed. • Check for recent network drops or code paths that call `disconnect()`/close the engine prematurely around the timestamp in your logs. To help further, please share: • The code snippet around where you call
perform_rpc
(especially connection state checks and disconnect logic). • Whether disconnects were expected at that time or if you saw any network/transport errors earlier in logs. If you want, I can review the snippet and suggest exact guard logic or a safe teardown sequence. Sources: Remote method calls | LiveKit docs | Connecting to LiveKit | LiveKit docs
e
no i am facing this initiakllya s i join the room with bot
t
Thanks — you said this happens immediately when the bot joins the room. I’ll help diagnose that; I need a few quick details to pinpoint the root cause: 1. SDK and version the bot uses (language and package version). 2. The code that creates/initializes the Room/engine and the join call (the snippet that sets up the bot and connects). 3. The code that triggers
perform_rpc
/
_clear_buffer_task
(or the agent snippet around that task), including any connection-state checks. 4. Full log lines from bot startup through the join and the error (timestamps before and after the error). 5. How the bot gets its join token and the participant identity used. 6. Environment details (container/VM, headless, any proxies or network restrictions). 7. Is this reproducible every join or intermittent? Does it happen only on first join? Share those items and I’ll point to the specific race or token/connection issue and provide a concrete fix (guard, reorder, or token change) and exact code suggestions.
e
livekit-1.0.20 livekit-agents-1.3.6 livekit-api-1.1.0 livekit-blingfire-1.0.0 livekit-plugins-anam-1.3.6 livekit-plugins-bey-1.3.6 livekit-plugins-cartesia-1.3.6 livekit-plugins-deepgram-1.3.6 livekit-plugins-google-1.3.6 livekit-plugins-noise-cancellation-0.2.5 livekit-plugins-openai-1.3.6 livekit-plugins-silero-1.3.6 livekit-plugins-turn-detector-1.3.6 livekit-protocol-1.1.1
t
Thanks — you said this happens immediately when the bot joins. I’ll help debug that. Quick checks you can run now: • Confirm the bot registers RPC handlers (e.g.,
register_rpc_method
) before joining or before any
perform_rpc
is invoked. • Ensure you only call
perform_rpc
after the local participant/room is fully connected. Add a guard that checks the room/participant connected state or wait for the agent session to reach a Running/Listening state before calling
_clear_buffer_task
. • Log the agent lifecycle events (Initializing → Starting → Running) and the room connection state at join time to see if the RPC is fired during a transition. • Verify the join token and identity are valid and that no server-side action (RemoveParticipant/DeleteRoom) is immediately disconnecting the bot. Please share: 1. The snippet that initializes/joins the room (connection and join flow). 2. The code for
_clear_buffer_task
/ where
perform_rpc
is called. 3. Bot startup logs from join start to the error (timestamps around the error). With those I’ll point out the exact race or token/connection issue and provide the specific guard or reorder to fix it. Sources: Remote method calls | LiveKit docs | Agent session | LiveKit docs | Events and error handling | LiveKit docs
e
async def entrypoint(ctx: JobContext): # ... lots of setup code ... # Line ~486: Avatar initialization happened HERE (EARLY) avatar_session = None if avatar_enabled and avatar_id: try: if avatar_id in BEYOND_PRESENCE_AVATARS: # ... setup code ... avatar_session = bey.AvatarSession(...) await avatar_session.start(session, room=ctx.room) # Line ~518 elif avatar_id in ANAM_AVATARS: # ... setup code ... avatar_session = anam.AvatarSession(...) await avatar_session.start(session, room=ctx.room) # Line ~556 except Exception as e: logger.error(f"Failed to start avatar session: {e}") avatar_session = None # ... more setup code (activity tracker, recording, etc.) ... # Line ~1341: Room connection happens MUCH LATER await ctx.connect() # ← Room connected HERE # Line ~1351: Session starts await session.start(...) # ← Session started HERE
t
Thanks — you showed the issue: avatar session is started well before the room is connected (avatar start around line ~518, but
await ctx.connect()
happens at ~1341). That explains the RPC error: the avatar's
_clear_buffer_task
(which calls
perform_rpc
) runs before the engine/room is ready, so RPCs fail with "engine is closed." What you can do (pick one): • Reorder startup (recommended) a. Move avatar creation/`avatar_session.start(...)` to after
await ctx.connect()
and
await session.start(...)
so the room/engine is active before the avatar starts sending RPCs. • Add a connection guard (if reordering isn't possible) a. Before calling
perform_rpc
in
_clear_buffer_task
, check the room/participant connection state and skip or delay the RPC if not connected. b. Example logic: wait for a
connected
flag or an AgentSession state of Running/Listening, or poll
room.is_connected
(or equivalent in your SDK) with a timeout, then proceed. • Make
_clear_buffer_task
resilient a. Catch
RpcError
with "engine is closed" and either retry with backoff while connected, or abort cleanup safely if the engine is closed permanently. b. Ensure teardown cancels
_clear_buffer_task
when shutting down the agent to avoid RPCs after disconnect. What I recommend first • Move avatar initialization/start to after
await ctx.connect()
and
await session.start(...)
. That fixes the race and is the simplest, safest change. If you want, paste the
_clear_buffer_task
function (or the lines where it calls
perform_rpc
) and I’ll suggest the exact guard/retry code to add.
e
so tell me why was this working fromost and failing for some
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
e
nt: Starting Anam avatar session with avatar_id: d9ebe82e-2f34-4ff6-9632-16cb73e7de08 [2025-12-09 224717] [INFO] agent: 📋 Registering room event handlers (participant_connected, participant_disconnected) [2025-12-09 224717] [INFO] agent: 📋 Registering participant_disconnected event handler [2025-12-09 224717] [INFO] agent: Started intelligent pause detection task [2025-12-09 224717] [INFO] agent: 🎯 Initialized enhanced pause detection - user's turn to speak [2025-12-09 224717] [INFO] agent: 🔌 Connecting to room BEFORE starting session to ensure event handlers are active [2025-12-09 224717] [INFO] agent: 🔄 Starting enhanced pause detection loop (40-second threshold)... [2025-12-09 224717] [INFO] agent: 🔍 PAUSE CHECK: current=1765320437.37, last_interaction=1765320436.41, diff=1.0s [2025-12-09 224717] [INFO] agent: ⏱️ Time since last interaction: 1.0s, user_turn: True, pause_sent: False [2025-12-09 224717] [INFO] agent: ✅ Room connected - event handlers are now active and listening [2025-12-09 224717] [INFO] agent: 🤖 VoiceInterviewAssistant initialized with org_id: 24191 [2025-12-09 224717] [WARNING] livekit.agents: RoomInputOptions and RoomOutputOptions are deprecated, use RoomOptions instead [2025-12-09 224717] [INFO] agent: Participant connected: candidate-0ab9bd8d-ed36-4038-959a-aa40cb2d6168-23200363 [2025-12-09 224717] [INFO] agent: 📤 Calling generate_reply for greeting - expecting TTS to start within 5s [2025-12-09 224717] [INFO] agent: 🎤 User audio track subscribed [2025-12-09 224717] [INFO] agent: ✅ User activity detected - reset interaction time [2025-12-09 224718] [INFO] agent: 🤖 AGENT STATE: listening [2025-12-09 224718] [WARNING] livekit.agents: resume_false_interruption is enabled but audio output does not support pause, it will be ignored [2025-12-09 224718] [INFO] agent: 🎧 Setting up simplified event monitoring for turn detection [2025-12-09 224718] [INFO] agent: ✅ Session health monitor started - checking every 10 seconds [2025-12-09 224718] [INFO] agent: 🔍 BACKFILL: Found 1 participant(s) already in room - processing... [2025-12-09 224718] [INFO] agent: ✅ candidate-0ab9bd8d-ed36-4038-959a-aa40cb2d6168-23200363 already tracked - no action needed [2025-12-09 224718] [INFO] agent: 📊 BACKFILL COMPLETE: Total active_participants: 1, Human count: 1, Bot count: 0 [2025-12-09 224718] [INFO] agent: 🤖 AGENT STATE: thinking [2025-12-09 224718] [INFO] agent: Participant connected: anam-avatar-agent [2025-12-09 224718] [INFO] agent: Metrics collected: type='vad_metrics' label='livekit.plugins.silero.vad.VAD' timestamp=1765320438.8811805 idle_time=1.051004484997975 inference_duration_total=0.08066063001024304 inference_count=32 metadata=Metadata(model_name='silero', model_provider='ONNX') [2025-12-09 224719] [INFO] agent: 🎤 BOT STARTED SPEAKING - is_user_turn=False, last_interaction_time=1765320439.0109897 [2025-12-09 224719] [INFO] livekit.agents: LLM metrics [2025-12-09 224719] [INFO] agent: Metrics collected: type='llm_metrics' label='livekit.plugins.openai.llm.LLM' request_id='chatcmpl-Cl0mEguhNM4qbezfoZG8RhZZCwrNU' timestamp=1765320439.6477814 duration=1.6183060529983777 ttft=1.5940115570010676 cancelled=False completion_tokens=8 prompt_tokens=5043 prompt_cached_tokens=0 total_tokens=5051 tokens_per_second=4.9434406953973244 speech_id='speech_625b30a3f49c' metadata=Metadata(model_name='gpt-4.1', model_provider='api.openai.com') [2025-12-09 224719] [INFO] agent: Metrics collected: type='vad_metrics' label='livekit.plugins.silero.vad.VAD' timestamp=1765320439.8822327 idle_time=2.052056550997804 inference_duration_total=0.03498676899835118 inference_count=32 metadata=Metadata(model_name='silero', model_provider='ONNX') [2025-12-09 224720] [INFO] agent: 🤖 AGENT STATE: speaking [2025-12-09 224720] [INFO] livekit.agents: TTS metrics [2025-12-09 224720] [INFO] agent: Metrics collected: type='tts_metrics' label='livekit.plugins.cartesia.tts.TTS' request_id='061682e72c07' timestamp=1765320440.333219 ttfb=0.24664563400074258 duration=0.6838787189990398 audio_duration=2.1362083333333333 cancelled=False characters_count=27 streamed=True segment_id='fa44cb225875' speech_id='speech_625b30a3f49c' metadata=Metadata(model_name='sonic-3', model_provider='Cartesia') [2025-12-09 224720] [INFO] agent: 🔇 Avatar audio track unpublished - agent finished [2025-12-09 224720] [INFO] agent: 🔇 BOT FINISHED SPEAKING - is_user_turn=True, last_interaction_time=1765320440.8209388 earlier before this fix above logs i am geting in error logs we are getting this [2025-12-09 224923] [ERROR] livekit.agents: failed to perform clear buffer rpc Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/livekit/agents/voice/avatar/_datastream_io.py", line 174, in _clear_buffer_task await self._room.local_participant.perform_rpc( File "/usr/local/lib/python3.11/site-packages/livekit/rtc/participant.py", line 328, in perform_rpc raise RpcError._from_proto(cb.perform_rpc.error) livekit.rtc.rpc.RpcError: Failed to send [2025-12-09 224923] [ERROR] livekit: livekit:roomparticipantlocal participant786livekitroomparticipant:local_participant - Failed to publish RPC request: engine: connection error: engine is closed aove all are before fix ;logs analyze logs i want analysis based o that
any result
is it possible for avatr to start before room creation
Original order: 1. Line 283/316: avatar_session.start() — room not connected 2. Line 919: session.start() — room still not connected 3. Line 933: ctx.connect() — room connects last This caused: • Avatar trying to use ctx.room before it was connected • _clear_buffer_task RPC operations failing with "engine is closed" • Race condition where sometimes it worked (if connection happened fast enough) is this possible