jolly-eve-3916
07/29/2025, 11:55 PMtall-airline-16756
07/29/2025, 11:55 PMtall-airline-16756
07/29/2025, 11:56 PMlivekit-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:
room_input_options=RoomInputOptions(close_on_disconnect=False)
By default, the session should close automatically with logging:
<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:
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