MoonshotAI / MoonshotAI/kimi-code

Server shutdown can leak live session resources

Open
#1,273 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.5k
Forks
1.2k
Avg merge
11h 53m
Merged PRs (30d)
350

Description

Problem

When the server shuts down while sessions are still live, the shutdown path disposes the server/service graph without first closing the live KimiCore sessions.

This means Session.close() is not invoked unless each session was explicitly closed before shutdown.

Why this is a resource leak

Session.close() owns session-scoped cleanup, including active turn cancellation, background task shutdown, metadata flushing, session-end hooks, MCP shutdown, and log handle closure.

If server shutdown skips Session.close() for live sessions, those resources do not get their normal cleanup path.

Code-path proof

  1. KimiCore tracks live sessions in this.sessions.
  2. KimiCore.closeSession() closes a single session with session.close(), then removes it from the session map.
  3. The server shutdown path closes WebSocket connections and Fastify, then disposes the DI graph.
  4. CoreProcessService.dispose() marks the service disposed so future RPC calls reject, but it does not close live sessions.
  5. Therefore, any session still present in KimiCore.sessions at server shutdown skips Session.close().

Expected behavior

Server shutdown should close all live sessions before disposing the service graph.

Shutdown should attempt to close every session even if one session close fails, then continue best-effort cleanup after reporting the failure.

Proposed fix

Add a core lifecycle method to close all live sessions and call it during server shutdown before DI disposal.

The fix should include a focused regression test proving:

  • all live sessions are closed and removed;
  • later sessions are still closed if an earlier session close fails.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing KimiCore.sessions and closeSession(), then inspect the server shutdown path and CoreProcessService.dispose(). Add a lifecycle path that closes every live session before DI disposal, continuing after individual failures. Verify the focused regression test proves all sessions are removed and later sessions still close after an earlier failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend, cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.