abundant-father-33863
10/31/2025, 6:10 AMtall-airline-16756
10/31/2025, 6:10 AMtall-airline-16756
10/31/2025, 6:11 AMsession.shutdown() to gently stop all tasks. By default, it lets ongoing operations finish nicely (drain=True), but if you need to stop immediately, you can set drain=False.
2. Interrupt Ongoing LLM Generation: If you want to stop real-time LLM responses right away, just call session.interrupt(). This sends a cancellation event to the server-side LLM to halt the process.
3. Custom Cleanup: To tidy up external resources like databases or logs, you can register shutdown callbacks with add_shutdown_callback(). This helps keep everything neat and organized.
4. Stop TTS Playout: To cancel any ongoing speech playback, use session.interrupt(), or if you prefer to wait for the speech to finish, SpeechHandle.wait_for_playout() is your friend.
If you'd like, I’m happy to provide more details or walk you through a specific example—just let me know!
Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Job lifecycle | LiveKit Docs | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | Distributed Setup | LiveKit docsabundant-father-33863
10/31/2025, 6:16 AMtall-airline-16756
10/31/2025, 6:16 AM_ParticipantInputStream class handles lifecycle events like track_subscribed or track_unpublished. Sometimes, if a new track gets subscribed or re-published while everything is shutting down, it might briefly reattach before the final shutdown completes.
• The on_detached() and on_attached() methods adjust internal flags, but occasionally, pending events can cause the state to flip back and forth, which is why you see that log sequence.
• Importantly, this behavior is usually harmless and doesn’t signal any real problem since the underlying data channels are still being properly closed.
To help avoid this, I recommend making sure the shutdown process happens in the right order—like removing event listeners before closing streams or canceling tasks that forward frames. If you'd like, I’m here to guide you through the implementation details or answer any other questions you might have!
Sources: livekit-agents/livekit/agents/voice/transcription/synchronizer.py | agents/src/voice/room_io/room_io.tsabundant-father-33863
10/31/2025, 6:19 AMtall-airline-16756
10/31/2025, 6:19 AMshutdown, it’s a good idea to unregister any event listeners so no further state changes happen. For example:
room.remove_event_listener("track_subscribed", handler)
3. Cancel Pending Tasks: Make sure to explicitly cancel tasks that handle forwarding frames or processing input/output streams. For instance:
await asyncio.gather(task1.cancel(), task2.cancel(), return_exceptions=True)
4. Use Shutdown Callbacks: You can register a callback to tidy up resources, ensuring it runs during shutdown:
def on_shutdown(reason: str):
print(f"Shutting down: {reason}")
# Add cleanup logic here
ctx.add_shutdown_callback(on_shutdown)
5. Interrupt Ongoing LLM/TTS: If there’s any active LLM response generation or TTS playback, use session.interrupt() to stop it immediately.
If you’d like, I’m happy to provide a full code example or explain any of these steps in more detail. Just let me know!