what is the order of execution for multiple shutdo...
# ask-ai
f
what is the order of execution for multiple shutdown callbacks in livekit agents?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Regarding the order of execution for multiple shutdown callbacks in LiveKit agents: The order is not guaranteed. Callbacks registered with
ctx.add_shutdown_callback()
are stored and run when the session ends, but the framework does not enforce a specific execution order. Design your callbacks so they do not depend on one another. Two additional facts to note: • All shutdown callbacks must complete within the default 60-second timeout, after which the process is forcefully terminated. • You can adjust that timeout via the
shutdown_process_timeout
parameter in agent server options. Would you like an example pattern for making callbacks order-independent or help adjusting the timeout? Sources: Job lifecycle | LiveKit docs
f
does shutdown callbacks end before I exit entrypoint function or after?
t
They run after the entrypoint function exits. Shutdown callbacks added with
ctx.add_shutdown_callback()
execute during the shutdown phase (after your entrypoint returns or
ctx.shutdown()
is triggered), and must finish within the shutdown timeout (default 60s, configurable via
shutdown_process_timeout
). Sources: Job lifecycle | LiveKit docs
f
can I implement some cleanup after all the callbacks?
t
Regarding running cleanup after all registered shutdown callbacks: There is no built-in guarantee or hook that runs strictly after all shutdown callbacks. Shutdown callbacks run during the shutdown phase and their execution order is not guaranteed, so you cannot rely on a separate post-callback hook running after every callback finishes. Recommended approaches you can use instead: • Register a single shutdown callback that performs the coordinated cleanup after calling or awaiting other internal cleanup tasks in the order you control. • Have each callback signal completion (e.g., set an
asyncio.Event
, write to a shared coordinator, or resolve a
Future
). Then register one shutdown callback that waits for those signals and runs final cleanup (ensure you handle timeouts so the overall shutdown stays within
shutdown_process_timeout
). • Consolidate dependent cleanup into one callback so ordering and finalization are deterministic. If you tell me which language/runtime you’re using (Python asyncio, Node, etc.), I can provide a short example implementing one of these patterns.