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(pi-manager): shutdown collector 提前到 drain spawn 之前

yan27dd23bf583f
diff --git a/packages/maker-pi-manager/src/__tests__/session-registry.test.ts b/packages/maker-pi-manager/src/__tests__/session-registry.test.ts
index 9503708c5d..b60881c3e1 100644
--- a/packages/maker-pi-manager/src/__tests__/session-registry.test.ts
+++ b/packages/maker-pi-manager/src/__tests__/session-registry.test.ts
@@ -551,6 +551,35 @@ describe('list / shutdownAll / teardown identity', () => {
expect(fs.existsSync(envFile)).toBe(false);
});
+ it('collects natural-exit teardown while shutdownAll drains pending spawns', async () => {
+ const { registry, tmpDir } = await createRegistry();
+ cleanupFns.push(() => { registry.close(); fs.rmSync(tmpDir, { recursive: true, force: true }); });
+
+ const live = makeChild();
+ mockSpawn.mockReturnValueOnce(live);
+ mockCreateServer.mockReturnValueOnce(makeServer());
+ await registry.ensure('live-exit', 'cmd', { CINDY_PI_API_KEY: 'gateway-secret' }, 'h1', false);
+ const envFile = path.join(tmpDir, 'envs', 'env-live-exit');
+ expect(fs.existsSync(envFile)).toBe(true);
+
+ let resolveListen: () => void;
+ const gate = new Promise<void>((r) => { resolveListen = r; });
+ const pendingServer = makeServer();
+ pendingServer.listen.mockImplementation((_p: string, cb?: () => void) => {
+ gate.then(() => { if (cb) cb(); });
+ });
+ mockSpawn.mockReturnValueOnce(makeChild());
+ mockCreateServer.mockReturnValueOnce(pendingServer);
+ const pending = registry.ensure('pending', 'cmd', { K: 'v' }, 'h2', false);
+
+ const shutdownPromise = registry.shutdownAll();
+ live.emit('close', 0, null);
+ resolveListen!();
+ await expect(pending).rejects.toThrow(/shutting down/);
+ await shutdownPromise;
+ expect(fs.existsSync(envFile)).toBe(false);
+ });
+
// 轮 40-w4-t3 CRITICAL:shutdownAll 遇杀不死的 session 不得 teardown ——
// 保留 entry(防凭证进程残留不受管理)并聚合抛出 SESSION_KILL_SURVIVED。
it('shutdownAll keeps survivor entry and rejects (round 40-w4-t3 CRITICAL)', async () => {
diff --git a/packages/maker-pi-manager/src/session-registry.ts b/packages/maker-pi-manager/src/session-registry.ts
index 5e71aff34c..97b3ca7c9f 100644
--- a/packages/maker-pi-manager/src/session-registry.ts
+++ b/packages/maker-pi-manager/src/session-registry.ts
@@ -97,7 +97,7 @@ export class PiSessionRegistry {
/** 关停中:拒绝新 ensure, 防孤儿子进程(自审轮 2 HIGH)。 */
private shuttingDown = false;
/** shutdownAll 收集 close-handler teardown 的异步 rm, 避免 process.exit 抢在凭证文件删除前。 */
- private shutdownCleanupBatch: Array<(p: Promise<void>) => void> | null = null;
+ private shutdownCleanupBatch: ((p: Promise<void>) => void) | null = null;
constructor(opts: PiSessionRegistryOptions) {
this.sockDir = opts.sockDir;
@@ -322,11 +322,14 @@ export class PiSessionRegistry {
/** Kill all sessions (daemon shutdown). */
async shutdownAll(reason: 'killed' | 'completed' = 'killed'): Promise<void> {
this.shuttingDown = true;
+ // collector 必须在 drain pending spawn **之前**挂上 —— 等待期间已注册
+ // 的 child 可能自然退出, close-handler teardown 否则无人收集, daemon
+ // process.exit 会抢在 env-file rm 前。
+ const cleanupBatch: Promise<void>[] = [];
+ this.shutdownCleanupBatch = (p) => { cleanupBatch.push(p); };
// 先等 in-flight spawn 完成(否则 spawnSession 会在关停后注册孤儿 —— 自审轮 2 HIGH)。
await Promise.allSettled([...this.pendingSpawns.values()]);
// 二次扫描:spawn 完成后可能注册了新 entry, 一并收掉。
- const cleanupBatch: Promise<void>[] = [];
- this.shutdownCleanupBatch = (p) => { cleanupBatch.push(p); };
// 轮 40-w4-t3 CRITICAL:杀不死的 entry 不得 teardown —— 否则删了 registry
// 状态/env-file/socket 但进程仍活着, 残留持凭证进程不受管理, 与 kill()/
// recycleIdle() 的「杀不死保留 entry 防双 spawn」不变量冲突。收集并最后抛出,