agents-js - if you shutdown the session, it looks ...
# ask-ai
n
agents-js - if you shutdown the session, it looks like the on("Close") event of the agent never triggers
t
Thanks for reaching out, Francisco. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I hear you: you're calling shutdown on an agents-js AgentSession and the session's on("close") handler doesn't appear to fire. I can help troubleshoot this — here are the most likely causes and a few quick checks, plus a couple of targeted questions so I can narrow it down. What to check first • Confirm the listener is attached to the exact same AgentSession instance you call shutdown on. If you create multiple session objects or reassign the variable, the handler might be on a different instance. • Make sure you attach the listener before calling
session.shutdown()
(or before any code path that might close the session). • Listen for
error
events as well — an error during shutdown could abort the closing sequence before the
close
event is emitted. • If you use async shutdown (
aclose()
or an async function), ensure you
await
it. Not awaiting async shutdown could let the process exit before events are emitted. • If you run inside Docker / containers or use process signals (SIGTERM), confirm the signal is reaching the process and you aren’t force-killing the container before the session can finish cleanup. Look for logs like "draining worker" which indicate the Closing phase is running. Example listener setup (verify your code matches this pattern)
Copy code
session.on("close", () => {
  console.log("Session has been closed");
});
session.on("error", (err) => {
  console.error("Session error:", err);
});
If close still doesn't fire, collect these details • A minimal code snippet showing: session creation, where you attach
on("close", ...)
, and where/when you call
session.shutdown()
(or
session.aclose()
). • Execution environment: local Node, Docker, Kubernetes, LiveKit Cloud, etc. • Any logs around shutdown (especially any errors or "draining worker" messages) and whether you see
session_state_changed
to
closed
. • Whether you use multiple session instances or re-create sessions in the same scope. What I can do next • If you share a minimal reproducible snippet and the environment, I’ll point out the exact cause or show a corrected example. • If you prefer, I can list additional debugging steps for containerized environments or async shutdown patterns. Share the code snippet and environment details and I’ll help pinpoint the issue. Sources: Agent session | LiveKit Documentation | Deployment reliability on LiveKit Cloud | LiveKit