When does shutdown_callback execute exactly? After...
# ask-ai
l
When does shutdown_callback execute exactly? After the AgentSession is closed with aclose(), or after the room is deleted?
t
Thanks for reaching out, Ibrahim. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — great question. You're asking whether
shutdown_callback
runs after
AgentSession.aclose()
or after the room is deleted. Here's the concise answer and what that means for cleanup logic. Answer •
shutdown_callback
runs after the
AgentSession
is closed with
await session.aclose()
. • Room deletion (for example via the LiveKit API
delete_room
) is a separate step and happens after the session has been closed and the shutdown callbacks have run. What this means • Call
aclose()
to end the agent session and disconnect I/O. • Any functions you register with
ctx.add_shutdown_callback()
will execute during the session’s post-shutdown processing immediately after
aclose()
completes. • If you delete the room separately, do that only after your shutdown callbacks finish (for example, in a post-callback step). Shutdown callbacks are the right place for final cleanup such as saving state or logging. Timeout note • Shutdown callbacks should finish promptly — they run during the session shutdown window (there is a default timeout for shutdown processing). If you want, tell me how you’re currently using
shutdown_callback
(what cleanup it performs) and whether you delete rooms automatically or manually — I can suggest where to place room-deletion calls safely and an example ordering. Sources: Job lifecycle | LiveKit Documentation