broad-painter-9222
11/28/2025, 7:35 PMtall-airline-16756
11/28/2025, 7:35 PMtall-airline-16756
11/28/2025, 7:36 PMawait session.aclose() — begins teardown and waits for completion, or
◦ session.shutdown(drain=True) — gracefully drains pending operations.
Register post-shutdown work in on_session_end or with ctx.add_shutdown_callback().
• Node.js: call ctx.shutdown('reason') to end the session and run registered shutdown hooks.
Important: graceful shutdown is required for the hooks to execute — forcible process termination will not run them. Would you like example snippets for your codebase (Python or Node.js)?
Sources: Job lifecycle | LiveKit docs | Observability data hooks | LiveKit docsbroad-painter-9222
11/28/2025, 7:40 PMtall-airline-16756
11/28/2025, 7:40 PMaclose() and shutdown() and examples. Short answer: both perform a graceful end of a session, but they differ in intent and behavior:
• aclose() — immediate async teardown you await. Use when you want to begin teardown now and wait until the session has fully closed and all shutdown hooks finished.
• shutdown() — requests a graceful shutdown (optionally draining work) but is more of a controlled/ordered shutdown signal; it may return immediately and let the session drain before full close (depending on args/impl). Use when you want to ask the session to stop (optionally finish pending work) without necessarily blocking until every finalization step completes.
Python examples
1) Use await session.aclose() when you need to synchronously finish teardown and then continue:
# immediate teardown, wait until complete so on_session_end runs before next steps
await session.aclose()
# safe to access persisted transcripts or continue shutdown-dependent logic here
2) Use session.shutdown(drain=True) when you want the session to finish pending work but not necessarily block here:
# ask session to gracefully stop and drain pending operations
session.shutdown(drain=True)
# your code can continue; shutdown callbacks will run as session drains
Node.js examples
1) Immediate shutdown-and-wait pattern (if your API awaits shutdown):
// ensure shutdown completes before proceeding
await ctx.shutdown('time-up');
// ctx.shutdown runs registered shutdown hooks and disconnects the agent
2) Fire-and-allow-drain pattern:
// signal shutdown and let the session drain while this thread continues
ctx.shutdown('user-requested');
// don't await — session will run shutdown hooks and finalize in background
When to choose which
• Use aclose() / awaiting shutdown() when subsequent code depends on completed cleanup (transcripts saved, reports generated).
• Use non-blocking shutdown() (or shutdown(drain=True) without awaiting) when you want the session to finish naturally but don't need to block the caller.
Note: shutdown hooks (like on_session_end / registered shutdown callbacks) run only on graceful shutdown. If you want sample full handlers or snippets wired to on_session_end, tell me which language and I’ll provide a compact example.broad-painter-9222
11/28/2025, 7:42 PMtall-airline-16756
11/28/2025, 7:42 PMawait session.aclose()
# safe: on_session_end has completed, transcripts persisted
2) Signal graceful stop and continue immediately:
session.shutdown(drain=True)
# returns quickly; session will run on_session_end while your code continues
Node.js 1) Wait for shutdown to finish:
await ctx.shutdown('time-up');
// shutdown hooks finished; safe to read persisted outputs
2) Signal and continue:
ctx.shutdown('user-requested');
// non-blocking: hooks run while this call returns immediately
Which to pick
• If subsequent code depends on the session being fully closed or on outputs produced by shutdown hooks → use aclose() / await shutdown().
• If you only need to request the session to stop and don’t need to block → call shutdown() without awaiting.
If you tell me which language and whether you need to wait for transcripts/reports before continuing, I’ll give a short copy-paste example wired to your hooks.broad-painter-9222
11/28/2025, 7:47 PMtall-airline-16756
11/28/2025, 7:47 PM_session_.shutdown() after removal requests a graceful session end so your on_close/on_session_end hook runs (assuming graceful shutdown, not a forced process kill).
Recommended improvements
• Await the remove call to ensure it succeeded before calling shutdown.
• If you need to wait until the shutdown and hooks finish (e.g., to read persisted transcripts), use await _session_.aclose() instead of the non-blocking shutdown().
Tightened example (async, Python):
async def remove_participant(_session_: AgentSession) -> None:
job_ctx = get_job_context()
participant_identity = _session_.userdata.participant_identity
if participant_identity:
await job_ctx.api.room.remove_participant(
RoomParticipantIdentity(room=job_ctx.room.name, identity=participant_identity)
)
else:
logger.warning("Participant identity not found, skipping removal")
# Option A: signal graceful shutdown and continue immediately
_session_.shutdown()
# Option B: wait for full teardown and for on_session_end to complete
# await _session_.aclose()
Choose Option A if you want to fire-and-forget and let hooks run in background. Choose Option B if subsequent logic depends on shutdown completion.broad-painter-9222
11/28/2025, 8:01 PMtall-airline-16756
11/28/2025, 8:01 PM