We're experiencing an issue with livekit-agents 1....
# ask-ai
j
We're experiencing an issue with livekit-agents 1.2.2 where our Python AgentSession agent doesn't detect participant disconnections. When a user leaves the room (client calls room.disconnect()), no participant_disconnected event fires in our @ctx.room.on('participant_disconnected') handler, and the agent only shuts down after a 5-6 minute timeout instead of immediately. The docs say AgentSession automatically closes when participants disconnect, but this isn't happening. Additionally, when users reconnect quickly to the same room name (a UUID), the agent doesn't process their audio anymore - they appear connected but speech isn't detected. What's the correct way to ensure the agent detects disconnections and shuts down properly? Should we manually call ctx.shutdown() or session.aclose() in the disconnect handler, or is there a configuration we're missing for automatic cleanup? We're using React Native client SDK with tokens generated via our backend.
t
Thanks for reaching out, Albert. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The issue you're experiencing with
livekit-agents
1.2.2 where the
participant_disconnected
event does not fire and the agent does not shut down immediately could be related to how disconnections are handled in the agent session. Here are some key points to consider: How Participant Disconnection Should Work: • The agent session automatically handles participant disconnection through the
_on_participant_disconnected
method. The session will close when: ◦ The linked participant disconnects (only the disconnection of the participant the agent is linked to triggers session closure). ◦ The disconnect reason is one of the default reasons (
CLIENT_INITIATED
,
ROOM_DELETED
,
USER_REJECTED
). ◦ The
close_on_disconnect
option is enabled (this is
True
by default in
RoomInputOptions
). Potential Causes for the Issue: • The disconnect reason might not be one of the default reasons (e.g.,
UNKNOWN_REASON
,
SIGNAL_CLOSE
,
CONNECTION_TIMEOUT
). • The
close_on_disconnect
option might have been explicitly set to
False
. • The disconnecting participant might not be the linked participant. • There might already be a close session task running. Configuration Options: You can control this behavior through
RoomInputOptions
. For example:
Copy code
room_input_options=RoomInputOptions(close_on_disconnect=False)
By default, the session should close automatically with logging:
Copy code
<http://logger.info|logger.info>(
    "closing agent session due to participant disconnect "
    "(disable via `RoomInputOptions.close_on_disconnect=False`)",
    extra={
        "participant": participant.identity,
        "reason": rtc.DisconnectReason.Name(
            participant.disconnect_reason or rtc.DisconnectReason.UNKNOWN_REASON
        ),
    },
)
Alternative Approaches: You can manually handle participant disconnection events as shown below:
Copy code
def _on_participant_disconnected(_: rtc.Participant, fut=room_empty_future) -> None:
    if len(ctx.room.remote_participants) == 0 and not fut.done():
        fut.set_result(None)

ctx.room.on("participant_disconnected", _on_participant_disconnected)
To address your specific issue: • Check the disconnect reason being received to ensure it matches one of the default reasons. • Verify that your agent is properly linked to the participant you expect to trigger the session closure. • If needed, you can manually call
ctx.shutdown()
or
session.aclose()
in the disconnect handler as a workaround. If the issue persists despite these checks, it may require further investigation or a potential update to the agent version. Sources: livekit-agents/livekit/agents/voice/room_io/room_io.py