green-match-33925
06/25/2025, 3:27 AMtall-airline-16756
06/25/2025, 3:27 AMtall-airline-16756
06/25/2025, 3:27 AMroom_finished
event? For example:
• Are you using any specific SDK or API version?
• Does this delay occur consistently or only under certain conditions?
• Are there any specific configurations or customizations in your setup?
This information will help us investigate the issue further.green-match-33925
06/25/2025, 3:34 AMtall-airline-16756
06/25/2025, 3:34 AMgreen-match-33925
06/25/2025, 3:39 AMpython
# We track each participant leaving and wait for all to disconnect
if len(connected_participants) == 0:
session_active = False
asyncio.create_task(delayed_shutdown(ctx))
2. Multi-layer Agent Cleanup:
- Agent session cleanup
- BitHuman avatar session cleanup (bithuman.AvatarSession
)
- Participant tracking cleanup
- Room event listener cleanup
3. Built-in 2-second Delay:
python
async def delayed_shutdown(ctx: JobContext):
await asyncio.sleep(2) # We intentionally wait 2 seconds
Modal Serverless Performance:
- Cold starts: Possible but our functions are pre-warmed
- Function cancellation: We use function_call.cancel()
which might not be immediate
- Modal Dict operations: We're using persistent storage for session tracking
Custom Webhook Handler Processing:
Our webhook handler has some custom logic that might add time:
python
# Session tracking with Modal Dict
if room_sid in room_sessions:
call_id = room_sessions.pop(room_sid) # Dict operation
function_call = FunctionCall.from_id(call_id) # Function lookup
function_call.cancel() # Cancellation request
Key Observation: The delay might be coming from LiveKit waiting for our agent to fully terminate before sending room_finished
. Our agent has extensive cleanup that includes:
- Waiting for all participants to disconnect
- 2-second intentional delay
- Multiple async cleanup operationstall-airline-16756
06/25/2025, 3:39 AMgreen-match-33925
06/25/2025, 4:14 AMtall-airline-16756
06/25/2025, 4:15 AMdelayed_shutdown
function and some other cleanup tasks are definitely playing a role in slowing things down.
• Webhook Event Handling: I noticed quite a few webhook events getting processed - things like track_unpublished
and participant_left
- which could be adding some extra processing time before the room_finished
event shows up.
• Errors in Transcription Publishing: Those transcription publishing errors (like "failed to send transcription, room closed") suggest some operations are trying to happen after the room is already closed, which can definitely mess with the timing.
Want to speed things up? Here are some friendly suggestions:
1. See if you can streamline those cleanup processes - maybe trim down the time for tracking participant disconnections and agent session cleanup.
2. Take a look at that 2-second delay in the delayed_shutdown
function. If it's not super crucial, you might want to reduce or remove it.
3. Try to wrap up things like transcription publishing before closing the room to avoid those pesky errors.
4. Do a little performance check on your webhook handler to spot any potential slowdowns.
If things are still feeling sluggish after trying these out, we might need to dig a bit deeper with LiveKit's event handling. No worries though - I'm here to help! Got any questions? Just ask, and we'll figure this out together. 😊