Skip to content

addShutdownCallback not called after ctx.shutdown or hangup #896

@igerlster

Description

@igerlster

Describe the bug

after the user hangs up, agent calls a tool that hangs up or in my simple test after a timer the addShutdownCallback should be fired so i can update the db with info about the call, but this does not happen

Relevant log output

[08:38:21.500] WARN (62255): job is unresponsive
[08:43:34.323] WARN (51134): OpenAI Realtime API connection failed, retrying in 0.1s

but never logged the [SHUTDOWN CALLBACK] now running

Describe your environment

System:
OS: macOS 26.1
CPU: (12) arm64 Apple M4 Pro
Memory: 179.59 MB / 24.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.18.0 - /opt/homebrew/opt/nvm/versions/node/v22.18.0/bin/node
Yarn: 1.22.22 - /opt/homebrew/bin/yarn
npm: 10.9.3 - /opt/homebrew/opt/nvm/versions/node/v22.18.0/bin/npm
pnpm: 10.24.0 - /opt/homebrew/bin/pnpm
bun: 1.2.15 - /opt/homebrew/bin/bun

Minimal reproducible example

import { JobContext, JobProcess, ServerOptions, cli, defineAgent, metrics, voice } from '@livekit/agents';
import * as openai from '@livekit/agents-plugin-openai';
import { fileURLToPath } from 'node:url';

async function endCall(roomName: string) {
    const roomService = new RoomServiceClient(
        process.env.LIVEKIT_URL!,
        process.env.LIVEKIT_API_KEY!,
        process.env.LIVEKIT_API_SECRET!,
    );
    await roomService.deleteRoom(roomName).catch((err) => {
        console.error("[CALL END endThisCall]: Error deleting room");
    });
}

export default defineAgent({
  prewarm: async (proc: JobProcess) => {
    proc.userData.vad = /* ... */;
  },
  entry: async (ctx: JobContext) => {
    const session = new voice.AgentSession({
      llm: new openai.realtime.RealtimeModel({ turnDetection: { type: 'semantic_vad', create_response: false } }),
    });

    const usageCollector = new metrics.UsageCollector();
    ctx.addShutdownCallback(async () => {
      const summary = usageCollector.getSummary();
      console.log(`[SHUTDOWN CALLBACK]: ${JSON.stringify(summary)}`);
    });
    ctx.addShutdownCallback(async () => {
      console.log('[SHUTDOWN CALLBACK] now running');
    });

    await session.start({ agent: new voice.Agent({ instructions: '...' }), room: ctx.room });
    await ctx.connect();

    // shutdown 
    setTimeout(() => {
      console.log('[TEST]: Shutdown timer fired, calling ctx.shutdown()');
      ctx.shutdown('test timeout');
      //callong endCall not working too....
    }, 5000);

    console.log('[TEST]: Entry function returning, shutdown scheduled in 5s');
  },
});

cli.runApp(new ServerOptions({ agent: fileURLToPath(import.meta.url) }));

Additional information

seems like it works in python

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions