shutdown_callback is not running after I call aclo...
# ask-ai
l
shutdown_callback is not running after I call aclose() on agent session. is this expected behavior?
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 — you're asking whether it's expected that
shutdown_callback
doesn't run if you call
aclose()
on an agent session. Short answer: yes, that's expected. Explanation: • Shutdown callbacks are executed during the session's shutdown phase. Calling
session.shutdown()
(or
ctx.shutdown(drain=True)
) triggers those hooks and begins a graceful shutdown (draining pending work if requested). • Calling
session.aclose()
directly closes the session immediately and does not run the shutdown callbacks. That means pending operations may not be drained and callbacks registered with
ctx.add_shutdown_callback()
or
session.add_shutdown_callback()
won't run. Recommended sequence to ensure callbacks run: 1. Call
session.shutdown(drain=True)
to initiate graceful shutdown and run shutdown hooks. 2. Then
await session.aclose()
to finalize the session closure. If you want, tell me which language/runtime you’re using (Python, Node.js, etc.) and I can show the exact code snippet for the proper shutdown flow. Sources: Job lifecycle | LiveKit Documentation
l
Which one is supposed to be called first -- shutdown or aclose? Also what is difference between drain=True vs drain=False
t
Good questions — I'll be concise. 1) Which to call first • Call
shutdown()
first, then
aclose()
. •
shutdown()
initiates a graceful shutdown and runs shutdown callbacks.
aclose()
finalizes and closes the session. If you call
aclose()
directly, shutdown callbacks will not run. 2) What
drain=True
vs
drain=False
means • `drain=True`: The shutdown will wait for pending work (for example, any in-progress or queued speech output) to complete before finishing shutdown. This ensures work is delivered before the session fully closes. • `drain=False`: The shutdown will not wait for pending work to finish; it will stop immediately (or as quickly as the shutdown process allows) and callbacks will run without waiting for ongoing tasks to be drained. Recommended pattern (example): 1. Call
session.shutdown(drain=True)
to run callbacks and let pending work finish. 2. Await
session.aclose()
to complete the closure. If you tell me which runtime you’re using (Python or Node.js), I can provide a short code example showing the exact sequence.