Skip to content

Commit e252065

Browse files
committed
🤖 fix: preserve killed status when onExit callback fires
The onExit callback was unconditionally setting status to 'exited', overwriting the 'killed' status set by terminate(). Now guards with if (process.status === 'running') to preserve killed/failed states. Added test to verify killed status is preserved after termination.
1 parent ef2bc7b commit e252065

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/node/services/backgroundProcessManager.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,24 @@ describe("BackgroundProcessManager", () => {
198198
expect(process?.stdoutBuffer.toArray()).toContain("test");
199199
}
200200
});
201+
202+
it("should preserve killed status after onExit callback fires", async () => {
203+
// Spawn a long-running process
204+
const result = await manager.spawn("workspace-1", "sleep 60", {
205+
cwd: process.cwd(),
206+
});
207+
208+
if (result.success) {
209+
// Terminate it
210+
await manager.terminate(result.processId);
211+
212+
// Wait for onExit callback to fire
213+
await new Promise((resolve) => setTimeout(resolve, 100));
214+
215+
// Status should still be "killed", not "exited"
216+
const proc = manager.getProcess(result.processId);
217+
expect(proc?.status).toBe("killed");
218+
}
219+
});
201220
});
202221
});

src/node/services/backgroundProcessManager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ export class BackgroundProcessManager {
7474
onExit: (exitCode: number) => {
7575
log.debug(`Background process ${processId} exited with code ${exitCode}`);
7676
process.exitCode = exitCode;
77-
process.exitTime = Date.now();
78-
process.status = "exited";
77+
process.exitTime ??= Date.now();
78+
// Don't overwrite status if already marked as killed/failed by terminate()
79+
if (process.status === "running") {
80+
process.status = "exited";
81+
}
7982
},
8083
});
8184

0 commit comments

Comments
 (0)