Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(maker-core): app-server 弃用后 SIGTERM 无效时补 SIGKILL 收尸 (#3723)

fico-hubc42883f84756
diff --git a/packages/maker-core/src/agents/codex/app-server/stdioTransport.test.ts b/packages/maker-core/src/agents/codex/app-server/stdioTransport.test.ts
index 017c8b51cb..4b64a73354 100644
--- a/packages/maker-core/src/agents/codex/app-server/stdioTransport.test.ts
+++ b/packages/maker-core/src/agents/codex/app-server/stdioTransport.test.ts
@@ -109,3 +109,59 @@ describe('createStdioTransport process observer', () => {
})).not.toThrow();
});
});
+
+describe('close() 强杀兜底 (#3699)', () => {
+ it('SIGTERM 后进程未退出 → 宽限期到点补 SIGKILL(卡死 app-server 不再活体泄漏)', async () => {
+ vi.useFakeTimers();
+ try {
+ const child = makeChild();
+ mocks.spawn.mockReturnValue(child);
+ const transport = createStdioTransport({ binaryPath: '/codex' });
+
+ await transport.close();
+ expect(child.kill).toHaveBeenCalledWith('SIGTERM');
+ expect(child.kill).not.toHaveBeenCalledWith('SIGKILL');
+
+ // 进程无视 SIGTERM(exitCode/signalCode 保持 null)→ 宽限期后强杀。
+ vi.advanceTimersByTime(5_000);
+ expect(child.kill).toHaveBeenCalledWith('SIGKILL');
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('宽限期内正常退出 → 不发 SIGKILL', async () => {
+ vi.useFakeTimers();
+ try {
+ const child = makeChild();
+ mocks.spawn.mockReturnValue(child);
+ const transport = createStdioTransport({ binaryPath: '/codex' });
+
+ await transport.close();
+ child.signalCode = 'SIGTERM';
+ child.emit('exit', null, 'SIGTERM');
+ vi.advanceTimersByTime(5_000);
+ expect(child.kill).toHaveBeenCalledTimes(1); // 仅 SIGTERM
+ expect(child.kill).not.toHaveBeenCalledWith('SIGKILL');
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('close() 前已退出 → 全程不发信号', async () => {
+ vi.useFakeTimers();
+ try {
+ const child = makeChild();
+ mocks.spawn.mockReturnValue(child);
+ const transport = createStdioTransport({ binaryPath: '/codex' });
+
+ child.exitCode = 0;
+ child.emit('exit', 0, null);
+ await transport.close();
+ vi.advanceTimersByTime(5_000);
+ expect(child.kill).not.toHaveBeenCalled();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+});
diff --git a/packages/maker-core/src/agents/codex/app-server/stdioTransport.ts b/packages/maker-core/src/agents/codex/app-server/stdioTransport.ts
index f0c8d34740..a080594b0a 100644
--- a/packages/maker-core/src/agents/codex/app-server/stdioTransport.ts
+++ b/packages/maker-core/src/agents/codex/app-server/stdioTransport.ts
@@ -32,8 +32,17 @@ export interface StdioTransportOptions {
extraArgs?: string[];
/** 本地进程生命周期观察器;仅用于宿主诊断,不得影响 transport 启动。 */
onProcessSpawned?: (pid: number) => void | (() => void);
+ /** SIGTERM 后强杀宽限毫秒数;仅测试注入,生产走默认值。 */
+ forceKillGraceMs?: number;
}
+/**
+ * close() 发出 SIGTERM 后等待进程自行退出的宽限期。健康的 app-server 在毫秒级
+ * 退出;卡死的(见 close() 内注释)永远不退,5s 已足够区分两者且不拖慢关闭方
+ * (close() 本身不等待,timer 在后台收尸)。
+ */
+const FORCE_KILL_GRACE_MS = 5_000;
+
export function createStdioTransport(opts: StdioTransportOptions): Transport {
if (!opts.binaryPath) {
throw new Error('createStdioTransport: binaryPath is required');
@@ -174,6 +183,21 @@ export function createStdioTransport(opts: StdioTransportOptions): Transport {
try { child.stdin.end(); } catch { /* swallow */ }
if (child.exitCode === null && child.signalCode === null) {
try { child.kill('SIGTERM'); } catch { /* swallow */ }
+ // #3699: SIGTERM 只对健康的 app-server 有效。当它的主循环卡死(实测
+ // 场景:内部模型刷新子进程 wedged,manager 反复报 "timeout waiting for
+ // child process to exit")时,进程可能既不响应 stdin EOF 也不处理
+ // SIGTERM —— 而调用方(retireHostKey / 切换凭证 / 关闭会话)在 close()
+ // 后即视其为已弃用,不再持有引用。此前没有任何强杀兜底,弃用的
+ // app-server 会以活体泄漏:继续周期性模型刷新、打错误日志、占用
+ // 网络与文件句柄。宽限期后仍未退出则 SIGKILL 收尸;timer unref,
+ // 不阻塞父进程退出(父进程退出时同进程组的子进程本就会被收割)。
+ const killTimer = setTimeout(() => {
+ if (child.exitCode === null && child.signalCode === null) {
+ try { child.kill('SIGKILL'); } catch { /* swallow */ }
+ }
+ }, opts.forceKillGraceMs ?? FORCE_KILL_GRACE_MS);
+ killTimer.unref?.();
+ child.once('exit', () => clearTimeout(killTimer));
}
fireClose(reason);
},