enough-dentist-75383
12/10/2025, 7:42 AMtall-airline-16756
12/10/2025, 7:42 AMtall-airline-16756
12/10/2025, 7:43 AMclear 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 docsenough-dentist-75383
12/10/2025, 7:44 AMtall-airline-16756
12/10/2025, 7:44 AMperform_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.enough-dentist-75383
12/10/2025, 7:51 AMtall-airline-16756
12/10/2025, 7:52 AMregister_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 docsenough-dentist-75383
12/10/2025, 8:02 AMtall-airline-16756
12/10/2025, 8:02 AMawait 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.enough-dentist-75383
12/10/2025, 8:06 AMtall-airline-16756
12/10/2025, 8:06 AMenough-dentist-75383
12/10/2025, 8:33 AMenough-dentist-75383
12/10/2025, 8:36 AMenough-dentist-75383
12/10/2025, 9:54 AMenough-dentist-75383
12/10/2025, 11:01 AM