how to intercept job shutdown event and prevent it...
# ask-ai
l
how to intercept job shutdown event and prevent it?
t
Thanks for reaching out, Daniel. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m replying about how to intercept a job shutdown event and prevent it. Short answer: LiveKit Agents does not support intercepting or preventing a job shutdown once it has been initiated. Shutdowns are final actions in the job lifecycle and cannot be aborted after
shutdown()
or
session.aclose()
is called. What you can do instead • Control when shutdown is called in your own logic. Make shutdown conditional so it only runs when you actually want the job to end. • Use shutdown hooks (
ctx.add_shutdown_callback()
) for cleanup or finalization tasks. Note these hooks run after shutdown has been initiated and cannot stop the shutdown. • Use graceful shutdown (e.g.,
session.shutdown(drain=True)
) to let pending operations complete before exit. This waits for work to finish but does not allow cancelling the shutdown once started. Example pattern (preventing shutdown by avoiding calling it)
Copy code
@session.on("user_state_changed")
async def _user_state_changed(ev: UserStateChangedEvent):
    if ev.new_state == "away":
        if should_continue_session():
            return  # do not call shutdown()
        else:
            session.shutdown(drain=True)
If you want, tell me: • What triggers the shutdown in your setup (explicit code call, external signal, scheduler)? • Whether you need to delay shutdown to finish work or to conditionally keep a job alive. With that I can suggest concrete patterns or code changes to ensure shutdown only happens when you intend. Sources: Job lifecycle | LiveKit Documentation
l
i have a client that connects to a livekit room, when they disconnect to that livekit room, i want to prevent the entire room from shutting down to allow others to stay in the call
t
Thanks — I’m responding to your question about preventing a LiveKit room from shutting down when one client disconnects so others can stay in the call. Short answer: You don’t need to prevent anything when a single client disconnects. LiveKit only shuts a room when it becomes empty (no participants). As long as at least one participant remains connected, the room will stay active and other users can continue the call. What to check and do • Confirm your disconnect logic does not call DeleteRoom or any explicit room-teardown API when a participant leaves. An explicit delete will close the room for everyone. • Ensure at least one participant remains in the room. If the last participant leaves, the room will eventually close. • If you want the room to remain available for a while after everyone leaves, set an appropriate empty timeout when creating the room (e.g.,
empty_timeout=300
to keep it alive 5 minutes after the last participant disconnects). Example (conceptual) • Do: ◦ Let participants leave normally; do not call DeleteRoom on participant disconnect. ◦ Create room with
empty_timeout
if you want a grace period after last leave. • Don’t: ◦ Trigger room deletion when an individual participant disconnects. If you want, tell me: • What currently triggers room shutdown in your system (automatic empty timeout or an explicit API call)? • Whether you need a specific empty timeout length or logic to keep the room alive under certain conditions. With that info I can suggest exact code/config changes or show how to set
empty_timeout
in the SDK you’re using. Sources: CreateRoom() is taking too long | LiveKit | Why is there extra audio at the end of my recordings of an agent session? |... | Server-side | livekit/livekit_composite | DeepWiki | Python SDKs | livekit/livekit_composite | DeepWiki