We noticed instances where LiveKit disconnected fo...
# ask-ai
c
We noticed instances where LiveKit disconnected for a few different reasons:
Copy code
{"message": "livekit::rtc_engine:450:livekit::rtc_engine - received session close: \"server request to leave\" ConnectionTimeout Resume", "level": "WARNING", "name": "livekit", "call_id": "2cd793e2-a389-4c9b-8c75-32f7548e8fe0", "tenant_id": "93a14ff5-258d-4d77-a9c0-aa498a5d094a", "pid": 34746, "job_id": "AJ_T8mPUuhZDqDq", "timestamp": "2025-06-24T21:44:14.164188+00:00"}
develop-voice-agent
2025-06-24T21:44:14.165Z
{"message": "livekit::rtc_engine:450:livekit::rtc_engine - received session close: \"signal client closed: \\\"stream closed\\\"\" UnknownReason Resume", "level": "WARNING", "name": "livekit", "call_id": "2cd793e2-a389-4c9b-8c75-32f7548e8fe0", "tenant_id": "93a14ff5-258d-4d77-a9c0-aa498a5d094a", "pid": 34746, "job_id": "AJ_T8mPUuhZDqDq", "timestamp": "2025-06-24T21:44:14.164315+00:00"}
develop-voice-agent
2025-06-24T21:44:14.084Z
{"message": "livekit::rtc_engine:450:livekit::rtc_engine - received session close: \"signal client closed: \\\"stream closed\\\"\" UnknownReason Resume", "level": "WARNING", "name": "livekit", "call_id": "2cd793e2-a389-4c9b-8c75-32f7548e8fe0", "tenant_id": "93a14ff5-258d-4d77-a9c0-aa498a5d094a", "pid": 34746, "job_id": "AJ_T8mPUuhZDqDq", "timestamp": "2025-06-24T21:44:14.084484+00:00"}
develop-voice-agent
2025-06-24T21:44:14.083Z
{"message": "livekit::rtc_engine::rtc_session:743:livekit::rtc_engine::rtc_session - Publisher pc state failed", "level": "ERROR", "name": "livekit", "call_id": "2cd793e2-a389-4c9b-8c75-32f7548e8fe0", "tenant_id": "93a14ff5-258d-4d77-a9c0-aa498a5d094a", "pid": 34746, "job_id": "AJ_T8mPUuhZDqDq", "timestamp": "2025-06-24T21:44:14.082083+00:00"}
develop-voice-agent
2025-06-24T21:44:14.083Z
{"message": "livekit::rtc_engine:450:livekit::rtc_engine - received session close: \"pc_state failed\" UnknownReason Resume", "level": "WARNING", "name": "livekit", "call_id": "2cd793e2-a389-4c9b-8c75-32f7548e8fe0", "tenant_id": "93a14ff5-258d-4d77-a9c0-aa498a5d094a", "pid": 34746, "job_id": "AJ_T8mPUuhZDqDq", "timestamp": "2025-06-24T21:44:14.083068+00:00"}
1. What causes these to happen? 2. We created a
room.on("disconnected")
listener and we are not sure how to reproduce/test this listener. Any thoughts?
t
Thanks for reaching out, roni. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The disconnection events you are observing can occur for various reasons, such as duplicate identities, participant removal, room deletion, join failures, or server shutdowns. Each disconnection event includes a reason that can help identify the cause. For example, reasons like "server request to leave" or "signal client closed" indicate specific server-side or client-side issues. To test the
room.on("disconnected")
listener, you can simulate disconnection scenarios. For instance, you can manually call
room.disconnect()
in your code to trigger the listener. Additionally, you can test by intentionally causing conditions like duplicate identities or removing a participant via the API. Here's an example of setting up the listener:
Copy code
// First connect to the room
const room = new Room();
await room.connect(wsUrl, token);

// Set up the disconnected event listener
room.on('disconnected', (reason) => {
  console.log('Disconnected from room with reason:', reason);
  // Handle the disconnection based on the reason
});
This listener will capture and log the disconnection reason, allowing you to handle it appropriately. Sources: Connecting to LiveKit | LiveKit Docs | livekit.rtc API documentation | RoomEvent | LiveKit JS Client SDK - v2.8.1
c
using python, this is what i have:
Copy code
@self._ctx.room.on("disconnected")
        def on_room_disconnected(reason: rtc.DisconnectReason):
            logger.error(
                f"DISCONNECT HANDLER TRIGGERED - Room disconnected with reason: {reason} (type: {type(reason)})"
            )
            logger.error(
                f"Call {self._call_id} - Current status: {self._current_status.value}, Is terminal: {self._is_terminal}"
            )
            asyncio.create_task(self._transition_to_terminal_state(CallStatus.FAILED))
when i run await ctx.agent.disconnect() somewhere in the execution, i don't see any of the logs from the listener printed
t
Hey there! 👋 Dealing with tricky event listeners can be super frustrating, right? Let me help you troubleshoot why your
room.on("disconnected")
listener might not be working as expected. There are a few friendly things we can check together: 1. Listener Registration: Double-check that you've set up your listener before the disconnection happens. It's like making sure you've got your mic ready before a video call! 2. Logging Configuration: Make sure your logging settings are catching the right level of messages. Think of it like adjusting the volume – you want to hear what's important. 3. Event Emission: Let's confirm the
disconnect()
method is actually sending out that disconnected event. A quick print statement can be your best friend here. 4. Asynchronous Context: When you're using
asyncio.create_task
, just make sure your event loop isn't getting stuck somewhere else in your code. Here's a handy debugging approach you can try:
Copy code
python @self._ctx.room.on("disconnected") def on_room_disconnected(reason: rtc.DisconnectReason): print("Listener triggered") # Debugging print statement logger.error( f"DISCONNECT HANDLER TRIGGERED - Room disconnected with reason: {reason} (type: {type(reason)})" ) asyncio.create_task(self._transition_to_terminal_state(CallStatus.FAILED))
If the print statement doesn't show up, we might have an issue with how the listener is registered or how the event is being emitted. If it does appear but the logs are playing hide and seek, let's take a peek at your logging configuration. Hope this helps you track down the issue! Debugging can be a bit like detective work – we'll solve this together. 🕵️‍♀️🔍 Sources: GitHub::livekit::agents | livekit-agents/livekit/agents/voice/room_io/room_...
c
maybe we need to use _on_room_event?
t
Using
_on_room_event
could totally be a smart way to handle disconnection events! I've got some insights that might help you navigate this. Think of
_on_room_event
like a behind-the-scenes detective tracking what's happening in your room. When a participant disconnects, this method does some really helpful things: 1. It finds out who just left the room 2. Removes that participant from your participants list 3. Notes down why they disconnected 4. Sends out a friendly notification about the disconnection If you're wondering whether to use this method or the standard
room.on("disconnected")
listener, here's the scoop:
_on_room_event
gives you more detailed control. It's like having a backstage pass to see exactly what's happening. Here's a peek at how you might use it:
Copy code
def _on_room_event(self, event: proto_room.RoomEvent):
    which = event.WhichOneof("message")
    if which == "participant_disconnected":
        identity = event.participant_disconnected.participant_identity
        rparticipant = self._remote_participants.pop(identity)
        rparticipant._info.disconnect_reason = event.participant_disconnected.disconnect_reason
        self.emit("participant_disconnected", rparticipant)
And here's a cool way to handle those disconnections:
Copy code
room.on(
    "participant_disconnected",
    lambda p: asyncio.create_task(handle_disconnect(p)),
)
Just make sure this approach fits nicely with how your app is set up. Every project is a bit different, right? 😊 Let me know if you want to chat more about handling these disconnections! Sources: livekit.rtc API documentation | GitHub::livekit::agents | livekit-agents/livekit/agents/voice/room_io/room_...
c
i only care about disconnected events not participant disconnected
t
If you're struggling with room-level disconnections, don't worry! I totally get how tricky these can be. Let's walk through this together. When you're specifically focused on handling room disconnects, the
room.on("disconnected")
listener is your best friend. Think of it like a safety net for your connection. The
_on_room_event
method is more for catching individual participant disconnects, so let's keep our focus where it counts. To make sure everything's working smoothly, here are a few friendly checkpoints: 1. Listener Registration: Just like making sure you've got your keys before leaving home, double-check that your listener is set up before any potential disconnects. 2. Event Emission: Add a quick print statement or debug log. It's like leaving a breadcrumb to see exactly what's happening. 3. Logging Configuration: Take a peek at your logging setup and make sure you're capturing the right log levels. Here's a handy example that might help you out:
Copy code
@self._ctx.room.on("disconnected")
def on_room_disconnected(reason: rtc.DisconnectReason):
    print("Room disconnected with reason:", reason)  # Debugging print statement
    logger.error(
        f"DISCONNECT HANDLER TRIGGERED - Room disconnected with reason: {reason} (type: {type(reason)})"
    )
    asyncio.create_task(self._transition_to_terminal_state(CallStatus.FAILED))
If things still aren't clicking, don't stress! It might just be a small SDK hiccup or something unique to your setup. I'm here to help you troubleshoot and get things running smoothly. Shoot me any questions you've got, and we'll figure this out together! 😊