Clean up sessions
Make sure every session closes, with scoped disposal, timeouts, a shutdown handler, and a sweep for sessions that leaked.
A session holds a connection to the provider until it closes. A session you forget stays open until a timeout or the provider ends it. This page covers each way a session closes and how to find the ones that didn't.
Scope sessions with await using
A session is an AsyncDisposable. Open it with await using and it closes when the block ends, whether the code returns or throws.
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
async function summarize(audio: Uint8Array) {
await using session = await createSession(openai());
const result = await session.transcribe(audio);
if (!result.ok) return null;
return result.text;
} // the session closes here, on every pathUse await using whenever a session lives as long as one function or one connection. The exception is a keyed session a caller should be able to come back to; see One session per caller.
Close a session yourself
session.close() closes the session and releases the provider connection. It is idempotent: calling it on a session that is already closed, or closing, does nothing. You can call it from several places without tracking who got there first.
await session.close();
await session.close(); // safe, does nothingAfter close, converse resolves, and calls that need the session fail with a VoiceError with code closed.
Set timeouts as a backstop
Timeouts close sessions that your code didn't. Set them on every session that isn't scoped with await using.
| Option | Closes the session when | Use it for |
|---|---|---|
idleTimeoutMs | It has been idle for that long | Keyed sessions waiting for a reconnect, and callers who leave |
timeoutMs | It has been open for that long, whatever is happening | A hard limit on any one session |
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
const session = await createSession(openai(), {
key: "caller:+15551234567",
idleTimeoutMs: 2 * 60 * 1000,
timeoutMs: 30 * 60 * 1000,
});Run code when a session closes
The close event fires however the session closed: await using, close(), a timeout, or the provider. It is the one place to record usage and remove the session from your own bookkeeping.
session.on("close", () => {
const { audioInMs, audioOutMs, characters } = session.usage();
console.log(`session ${session.id} closed`, { audioInMs, audioOutMs, characters });
});Close sessions on shutdown
When your server stops, close the sessions it opened. Keep them in a set, remove each one on close, and close what is left on SIGTERM.
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
type Session = Awaited<ReturnType<typeof createSession>>;
const open = new Set<Session>();
export async function startSession() {
const session = await createSession(openai(), { idleTimeoutMs: 60_000 });
open.add(session);
session.on("close", () => open.delete(session));
return session;
}
process.on("SIGTERM", async () => {
await Promise.allSettled([...open].map((session) => session.close()));
process.exit(0);
});Close only the sessions in your own set. listSessions can include sessions that other servers opened, and closing those would hang up on their callers. Because close is idempotent, a session that closes on its own during shutdown causes no error.
Find leaked sessions
listSessions returns { id, key, startedAt, state } for each live session. A session that has been open far longer than any call you expect has probably leaked.
import { getSession, listSessions } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
const provider = openai();
const cutoff = Date.now() - 2 * 60 * 60 * 1000;
for (const info of await listSessions(provider)) {
if (new Date(info.startedAt).getTime() < cutoff) {
console.log("closing", info.id, info.key, info.state);
const session = await getSession(provider, info.id);
await session.close();
}
}To look without closing anything, use the CLI:
npx voiceai-sdk sessions list --provider openaiIf you find leaks, set timeoutMs so no session can outlive your longest expected call, then find the code path that skipped await using or close.
Next steps
- Lifecycle and cleanup: the states a session moves through.
- Errors: what
closedand the other error codes mean.