late-businessperson-58140
09/01/2025, 6:47 AMtall-airline-16756
09/01/2025, 6:47 AMtall-airline-16756
09/01/2025, 6:47 AMlate-businessperson-58140
09/01/2025, 6:48 AMtall-airline-16756
09/01/2025, 6:48 AMlate-businessperson-58140
09/01/2025, 6:50 AMtall-airline-16756
09/01/2025, 6:50 AMparticipant_disconnected event and then call await session.aclose() to close the session immediately.
• Handle Inactivity: Set up user_away_timeout in AgentSession and watch for user_state_changed events to close sessions when users are inactive.
• Clean Up Resources: Don’t forget to call ctx.delete_room() after closing the session to tidy everything up properly.
If you want, I’d be happy to walk you through any of these methods in more detail!
Sources: Worker lifecycle | LiveKit Docs | Anatomy of an Agent | LiveKit Docs | examples/warm-transfer/warm_transfer.py | basics/exit_message.py | Agents Core Architecture | livekit/livekit_composite | DeepWikilate-businessperson-58140
09/01/2025, 7:04 AMtall-airline-16756
09/01/2025, 7:04 AMimport asyncio
from livekit import rtc
async def monitor_and_close_session(ctx, session):
room_empty_future = asyncio.get_running_loop().create_future()
def on_participant_disconnected(participant):
if len(ctx.room.remote_participants) == 0 and not room_empty_future.done():
room_empty_future.set_result(None)
ctx.room.on("participant_disconnected", on_participant_disconnected)
# Wait for the room to become empty
await room_empty_future
# Close the session immediately
await session.aclose()
# Clean up the room
try:
await ctx.api.room.delete_room(ctx.room.name)
print(f"Room {ctx.room.name} deleted successfully.")
except Exception as e:
print(f"Oops, something went wrong deleting room {ctx.room.name}: {e}")
# Example usage
async def main(ctx):
session = rtc.AgentSession()
await monitor_and_close_session(ctx, session)
# Just swap 'ctx' with your actual JobContext instance when you call main()
This snippet listens for the participant_disconnected event, waits until the room is empty, then closes the session and deletes the room. If you want to chat more about this or need a hand with anything else, just let me know!late-businessperson-58140
09/01/2025, 7:13 AMtall-airline-16756
09/01/2025, 7:13 AMsignal client closed: "stream closed". Just wanted to check in— is this the issue you're trying to sort out, or are there other problems popping up? For instance, is the session not closing like you expected, or are you seeing delays when things are cleaning up? Let me know so I can help you better!late-businessperson-58140
09/01/2025, 7:14 AMtall-airline-16756
09/01/2025, 7:14 AMsignal client closed: "stream closed" usually means the session is closing, but it might not be happening as quickly as you'd like. To help with that, you can make sure the session closes right away by explicitly handling the session close event and tidying up resources promptly. Here's a little updated code snippet that does just that:
import asyncio
from livekit import rtc
async def monitor_and_close_session(ctx, session):
room_empty_future = asyncio.get_running_loop().create_future()
def on_participant_disconnected(participant):
if len(ctx.room.remote_participants) == 0 and not room_empty_future.done():
room_empty_future.set_result(None)
ctx.room.on("participant_disconnected", on_participant_disconnected)
# Wait for the room to become empty
await room_empty_future
# Close the session immediately
await session.aclose()
# Clean up the room
try:
await ctx.api.room.delete_room(ctx.room.name)
print(f"Room {ctx.room.name} deleted successfully.")
except Exception as e:
print(f"Error deleting room {ctx.room.name}: {e}")
# Example usage
async def main(ctx):
session = rtc.AgentSession()
await monitor_and_close_session(ctx, session)
# Replace 'ctx' with your actual JobContext instance when calling main()
This code listens for the participant_disconnected event, waits until the room is empty, then closes the session and deletes the room. If you want me to help tweak this or if anything’s unclear, just let me know—I’m here to help!