Skip to content

fix(ipc): close the session as JOB_SHUTDOWN when the job shuts down - #2962

Merged
chenghao-mou merged 2 commits into
mainfrom
scheming-trussing-worded
Nov 9, 2026
Merged

chenghao-mou merged 2 commits into
mainfrom
scheming-trussing-worded

Conversation

@rosetta-livekit-bot

@rosetta-livekit-bot rosetta-livekit-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Ports livekit/agents#8039.

Job finalization now closes the primary AgentSession through its reason-aware internal path with CloseReason.JOB_SHUTDOWN, preserving the existing timeout and cleanup order while avoiding the public close() path that reports USER_INITIATED.

Source diff coverage

Source diff coverage

  • livekit-agents/livekit/agents/ipc/job_proc_lazy_main.py: Adapted to agents/src/job_lifecycle.ts, because the JS job runner delegates primary-session closure to finalizeSession. It now invokes the internal reason-aware close with JOB_SHUTDOWN. agents/src/voice/agent_session.ts supplies the awaitable internal close counterpart the target lacked, and agents/etc/agents.api.md records that internal API.
  • tests/test_ipc.py: Adapted to agents/src/job_lifecycle.test.ts, the JS unit-test counterpart for the delegated shutdown sequence. Existing close failure/timeout fakes use the internal path, and the source-equivalent test proves shutdown bypasses public close() and records only JOB_SHUTDOWN.

No source files were omitted as not applicable.

Testing

  • pnpm test agents (4,320 passed, 5 skipped)
  • pnpm --filter @livekit/agents build
  • pnpm --filter @livekit/agents lint
  • pnpm test agents/src/job_lifecycle.test.ts agents/src/voice/agent_session.test.ts
  • Prettier and git diff --check

Includes a patch changeset for @livekit/agents.


Ported from livekit/agents#8039

Original PR description

When a job shuts down with the AgentSession still open, the session's close event reports CloseReason.USER_INITIATED. That covers worker drain or migration, SIGTERM, ctx.shutdown(), a room disconnect without a participant event, and an entrypoint crash. So the close event (including the one recorded in the session report) and lk.close_reason all blame the user for a shutdown the job started.

_shutdown_job closes the primary session through the public aclose() (added in #3541; #5875 only bounded it with a timeout), and aclose() always uses USER_INITIATED. AgentSession.start() registers a job shutdown callback that closes with JOB_SHUTDOWN (#2398), but callbacks run later. By then the session is already closed, and only the first close counts.

Fix

_shutdown_job now closes with session._aclose_impl(reason=CloseReason.JOB_SHUTDOWN). That's the same call start() already registers, so it doesn't change the public API.

Testing

  • New test_thread_job_shutdown_closes_session_as_job_shutdown (thread executor, in-process): a fake session records the close reason. On main it fails with [USER_INITIATED] == [JOB_SHUTDOWN].
  • The _HangingSession fake follows the call change, and its test still passes.
  • tests/test_ipc.py and uv run pytest --unit --audio_eot pass, ruff is clean, and make type-check reports no new errors.

Why no regression

  • The session closes at the same point, with the same timeout. Only the reason changes.
  • Sessions already closed keep their reason, because this close is then a no-op. Examples: PARTICIPANT_DISCONNECTED from room I/O, ERROR, and session.shutdown(), which stays USER_INITIATED.
  • No test or consumer in the repo branches on USER_INITIATED at job shutdown.
  • A ctx.shutdown() from user code is still a job shutdown, as the JOB_SHUTDOWN callback from fix(voice): cancel parked preemptive generation before pausing the activity #2398 already treats it. The lk.shutdown.user_initiated span attribute still records who started it.
  • A user subclass overriding aclose() is no longer called at this point. That matches the existing JOB_SHUTDOWN callback, which already goes through _aclose_impl.

@rosetta-livekit-bot
rosetta-livekit-bot Bot requested a review from a team as a code owner November 9, 2026 10:54
@changeset-bot

changeset-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 93d1a37

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 42 packages
Name Type
@livekit/agents Patch
@livekit/agents-plugin-anam Patch
@livekit/agents-plugin-anthropic Patch
@livekit/agents-plugin-assemblyai Patch
@livekit/agents-plugin-azure Patch
@livekit/agents-plugin-baseten Patch
@livekit/agents-plugin-bey Patch
@livekit/agents-plugin-cartesia Patch
@livekit/agents-plugin-cerebras Patch
@livekit/agents-plugin-deepgram Patch
@livekit/agents-plugin-did Patch
@livekit/agents-plugin-elevenlabs Patch
@livekit/agents-plugin-fishaudio Patch
@livekit/agents-plugin-google Patch
@livekit/agents-plugin-hume Patch
@livekit/agents-plugin-inworld Patch
@livekit/agents-plugin-krisp Patch
@livekit/agents-plugin-lemonslice Patch
@livekit/agents-plugin-liveavatar Patch
@livekit/agents-plugin-livekit Patch
@livekit/agents-plugin-meta Patch
@livekit/agents-plugin-minimax Patch
@livekit/agents-plugin-mistral Patch
@livekit/agents-plugin-mistralai Patch
@livekit/agents-plugin-neuphonic Patch
@livekit/agents-plugin-nvidia Patch
@livekit/agents-plugin-openai Patch
@livekit/agents-plugin-perplexity Patch
@livekit/agents-plugin-phonic Patch
@livekit/agents-plugin-protoface Patch
@livekit/agents-plugin-resemble Patch
@livekit/agents-plugin-rime Patch
@livekit/agents-plugin-runway Patch
@livekit/agents-plugin-sarvam Patch
@livekit/agents-plugin-silero Patch
@livekit/agents-plugin-soniox Patch
@livekit/agents-plugin-speechify Patch
@livekit/agents-plugin-synthesia Patch
@livekit/agents-plugin-tavus Patch
@livekit/agents-plugins-test Patch
@livekit/agents-plugin-trugen Patch
@livekit/agents-plugin-xai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

devin-ai-integration[bot]

This comment was marked as resolved.

@chenghao-mou
chenghao-mou enabled auto-merge (squash) November 9, 2026 11:44
@chenghao-mou
chenghao-mou merged commit 6300203 into main Nov 9, 2026
11 checks passed
@chenghao-mou
chenghao-mou deleted the scheming-trussing-worded branch November 9, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant