Skip to content

fix(server): prevent live session resource leaks on shutdown#1276

Open
morluto wants to merge 2 commits into
MoonshotAI:mainfrom
morluto:fix/close-live-sessions-on-shutdown
Open

fix(server): prevent live session resource leaks on shutdown#1276
morluto wants to merge 2 commits into
MoonshotAI:mainfrom
morluto:fix/close-live-sessions-on-shutdown

Conversation

@morluto

@morluto morluto commented Jul 1, 2026

Copy link
Copy Markdown

Related Issue

Fixes #1273

Problem

Server shutdown currently disposes the DI graph without first closing live KimiCore sessions.

Formal proof of the bug:

  1. KimiCore owns live sessions in this.sessions.
  2. KimiCore.closeSession() closes exactly one session by calling session.close(), then removes that session from the map.
  3. Before this change, server shutdown closed WebSocket connections and Fastify, then called ix.dispose().
  4. Before this change, CoreProcessService.dispose() only marked the core process service as disposed so future RPC calls reject. It did not call Session.close() for live sessions.
  5. Session.close() is the path that cancels active turns, stops background tasks, flushes metadata, emits session-end behavior, shuts down MCP, and closes the session log handle.
  6. Therefore, if the server shuts down while sessions are still live and no client explicitly closed them first, those sessions skip normal Session.close() cleanup.

Impact:

Live sessions can leave session-owned work and resources running past server shutdown, including active turns, background tasks, MCP transports, unflushed metadata, and log handles.

What changed

Added a core lifecycle method that closes all live sessions before disposal.

The implementation snapshots the live session map, attempts to close every session, deletes each entry after its close attempt, and reports any close failures with an AggregateError after all sessions have been attempted.

Server shutdown now awaits this lifecycle method before disposing the DI graph. If one or more session closes fail, shutdown logs a warning and continues with the existing best-effort cleanup path.

A focused regression test covers both important behaviors:

  • every live session is closed and removed;
  • later sessions are still closed even when one session close fails.

Validation

  • pnpm --filter @moonshot-ai/agent-core exec vitest run test/rpc/core-close-all.test.ts passes: 1 file, 2 tests.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9339a2f

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

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code 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

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.

Server shutdown can leak live session resources

1 participant