From 30459af6abc8308e7f13822d9dbef3a5be80dd4a Mon Sep 17 00:00:00 2001 From: 7Sageer <12210216@mail.sustech.edu.cn> Date: Thu, 11 Jun 2026 11:35:34 +0800 Subject: [PATCH] fix: clean up background tasks on session exit (#641) * fix: clean up background tasks on session exit * Stop background tasks on session close Change versioning for agent-core and kimi-code to patch. Signed-off-by: 7Sageer <12210216@mail.sustech.edu.cn> --------- Signed-off-by: 7Sageer <12210216@mail.sustech.edu.cn> --- .changeset/stop-background-tasks-on-exit.md | 6 +++++ docs/en/configuration/config-files.md | 2 +- docs/en/configuration/env-vars.md | 2 +- docs/zh/configuration/config-files.md | 2 +- docs/zh/configuration/env-vars.md | 2 +- packages/agent-core/src/session/index.ts | 2 +- .../test/session/lifecycle-hooks.test.ts | 25 +++++++++++++++++-- 7 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 .changeset/stop-background-tasks-on-exit.md diff --git a/.changeset/stop-background-tasks-on-exit.md b/.changeset/stop-background-tasks-on-exit.md new file mode 100644 index 000000000..b88111c97 --- /dev/null +++ b/.changeset/stop-background-tasks-on-exit.md @@ -0,0 +1,6 @@ +--- +"@moonshot-ai/agent-core": patch +"@moonshot-ai/kimi-code": patch +--- + +Stop background tasks by default when sessions close. diff --git a/docs/en/configuration/config-files.md b/docs/en/configuration/config-files.md index 7cf622736..0d64f5044 100644 --- a/docs/en/configuration/config-files.md +++ b/docs/en/configuration/config-files.md @@ -169,7 +169,7 @@ You can also switch models temporarily without touching the config file — by s | Field | Type | Default | Description | | --- | --- | --- | --- | | `max_running_tasks` | `integer` | — | Maximum number of background tasks running concurrently | -| `keep_alive_on_exit` | `boolean` | `true` | Whether to keep still-running background tasks when the session closes. Set to `false` to request that all background tasks stop before the process exits | +| `keep_alive_on_exit` | `boolean` | `false` | Whether to keep still-running background tasks when the session closes. By default, Kimi Code requests that all background tasks stop before the process exits; set this to `true` only when you want tasks to outlive the session | `keep_alive_on_exit` can be overridden by the `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` environment variable, which takes higher priority than `config.toml`. diff --git a/docs/en/configuration/env-vars.md b/docs/en/configuration/env-vars.md index 50acb9121..77b7bdfb2 100644 --- a/docs/en/configuration/env-vars.md +++ b/docs/en/configuration/env-vars.md @@ -123,7 +123,7 @@ Switches that control the behavior of subsystems such as telemetry, background t | Variable | Purpose | Valid values | | --- | --- | --- | | `KIMI_DISABLE_TELEMETRY` | Disable anonymous telemetry reporting | `1`, `true`, `yes`, `y` (case-insensitive) | -| `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` | Whether to keep background tasks when the session closes; takes higher priority than `config.toml` | Truthy: `1`/`true`/`yes`/`on`; falsy: `0`/`false`/`no`/`off` | +| `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` | Whether to keep background tasks when the session closes; takes higher priority than `config.toml`. The default is to stop them on exit | Truthy: `1`/`true`/`yes`/`on`; falsy: `0`/`false`/`no`/`off` | | `KIMI_CODE_PLUGIN_MARKETPLACE_URL` | Override the plugin marketplace JSON loaded by `/plugins` | URL or local path | | `KIMI_CODE_EXPERIMENTAL_FLAG` | Enable all registered experimental features for this process; `micro_compaction` is already enabled by default | `1`, `true`, `yes`, `on` | | `KIMI_CODE_EXPERIMENTAL_MICRO_COMPACTION` | Override [`[experimental].micro_compaction`](./config-files.md#experimental) for this process | Truthy or falsy | diff --git a/docs/zh/configuration/config-files.md b/docs/zh/configuration/config-files.md index 885840245..e0a215f56 100644 --- a/docs/zh/configuration/config-files.md +++ b/docs/zh/configuration/config-files.md @@ -169,7 +169,7 @@ max_context_size = 1047576 | 字段 | 类型 | 默认值 | 说明 | | --- | --- | --- | --- | | `max_running_tasks` | `integer` | — | 同时运行的最大后台任务数 | -| `keep_alive_on_exit` | `boolean` | `true` | 会话关闭时是否保留仍在运行的后台任务。设为 `false` 时,进程退出前会请求停止所有后台任务 | +| `keep_alive_on_exit` | `boolean` | `false` | 会话关闭时是否保留仍在运行的后台任务。默认情况下,Kimi Code 会在进程退出前请求停止所有后台任务;只有希望任务在会话结束后继续运行时才设为 `true` | `keep_alive_on_exit` 可被环境变量 `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` 覆盖,优先级高于配置文件。 diff --git a/docs/zh/configuration/env-vars.md b/docs/zh/configuration/env-vars.md index 2f95c93ae..e87eb146a 100644 --- a/docs/zh/configuration/env-vars.md +++ b/docs/zh/configuration/env-vars.md @@ -123,7 +123,7 @@ kimi | 环境变量 | 用途 | 合法值 | | --- | --- | --- | | `KIMI_DISABLE_TELEMETRY` | 关闭匿名遥测上报 | `1`、`true`、`yes`、`y`(不区分大小写) | -| `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` | 会话关闭时是否保留后台任务,优先级高于 `config.toml` | 真值:`1`/`true`/`yes`/`on`;假值:`0`/`false`/`no`/`off` | +| `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` | 会话关闭时是否保留后台任务,优先级高于 `config.toml`。默认会在退出时停止后台任务 | 真值:`1`/`true`/`yes`/`on`;假值:`0`/`false`/`no`/`off` | | `KIMI_CODE_PLUGIN_MARKETPLACE_URL` | 替换 `/plugins` 加载的 marketplace JSON | URL 或本地路径 | | `KIMI_CODE_EXPERIMENTAL_FLAG` | 在当前进程启用所有已注册的实验功能;`micro_compaction` 已默认开启 | `1`、`true`、`yes`、`on` | | `KIMI_CODE_EXPERIMENTAL_MICRO_COMPACTION` | 覆盖当前进程的 [`[experimental].micro_compaction`](./config-files.md#experimental) | 真值或假值 | diff --git a/packages/agent-core/src/session/index.ts b/packages/agent-core/src/session/index.ts index 0c1950bb3..99efe086b 100644 --- a/packages/agent-core/src/session/index.ts +++ b/packages/agent-core/src/session/index.ts @@ -264,7 +264,7 @@ export class Session { env: process.env, envKey: BACKGROUND_KEEP_ALIVE_ON_EXIT_ENV, configValue: this.options.background?.keepAliveOnExit, - defaultValue: true, + defaultValue: false, parseEnv: parseBooleanEnv, }); if (keepAliveOnExit) return; diff --git a/packages/agent-core/test/session/lifecycle-hooks.test.ts b/packages/agent-core/test/session/lifecycle-hooks.test.ts index 7422272e7..aa63eb4c4 100644 --- a/packages/agent-core/test/session/lifecycle-hooks.test.ts +++ b/packages/agent-core/test/session/lifecycle-hooks.test.ts @@ -110,7 +110,7 @@ describe('Session lifecycle hooks', () => { await expect(session.close()).resolves.toBeUndefined(); }); - it('stops background tasks on close when keepAliveOnExit is false', async () => { + it('stops background tasks on close by default', async () => { const { sessionDir, workDir } = await hookFixture(); const session = new Session({ kaos: testKaos.withCwd(workDir), @@ -118,7 +118,6 @@ describe('Session lifecycle hooks', () => { homedir: sessionDir, rpc: createSessionRpc(), skills: { explicitDirs: [join(workDir, 'missing-skills')] }, - background: { keepAliveOnExit: false }, }); const agent = await session.createMain(); const { proc, killSpy } = pendingProcess(); @@ -132,6 +131,28 @@ describe('Session lifecycle hooks', () => { expect(agent.background.getTask(taskId)?.status).toBe('killed'); }); + it('keeps background tasks alive on close when keepAliveOnExit is true', async () => { + const { sessionDir, workDir } = await hookFixture(); + const session = new Session({ + kaos: testKaos.withCwd(workDir), + id: 'session-bg-keepalive', + homedir: sessionDir, + rpc: createSessionRpc(), + skills: { explicitDirs: [join(workDir, 'missing-skills')] }, + background: { keepAliveOnExit: true }, + }); + const agent = await session.createMain(); + const { proc, killSpy } = pendingProcess(); + const taskId = agent.background.registerTask( + new ProcessBackgroundTask(proc, 'sleep 60', 'keep alive'), + ); + + await session.close(); + + expect(killSpy).not.toHaveBeenCalled(); + expect(agent.background.getTask(taskId)?.status).toBe('running'); + }); + it('lets the environment override config when deciding background task cleanup', async () => { vi.stubEnv('KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT', '0'); const { sessionDir, workDir } = await hookFixture();