From 9c5e408e824c81cefda50bcceae80ab420435e68 Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 20:20:28 +0000 Subject: [PATCH] fix(core): stop what a failed daemon shell command left in its group --- packages/core/src/shell.ts | 10 +++++++++- packages/core/test/shell-daemon.test.ts | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/core/src/shell.ts b/packages/core/src/shell.ts index be20ae8b8ae..f3e8b0e032b 100644 --- a/packages/core/src/shell.ts +++ b/packages/core/src/shell.ts @@ -316,7 +316,8 @@ const daemonLayer = Layer.effect( if (run.stopping) yield* Fiber.interrupt(run.stopping) // Stop waiting first, so forgetting the process is not mistaken for losing it. if (run.wait) yield* Fiber.interrupt(run.wait) - // Forget the process before deleting its file. + // Forget the process before deleting its file. This also kills what is left of its group, even after a + // successful exit, which a spawned command would leave running: the daemon cannot forget a process otherwise. yield* pty.terminatePipe(run.process).pipe(Effect.ignore) yield* Effect.promise(() => unlink(run.info.file).catch(() => {})) yield* Deferred.fail(run.done, new NotFoundError({ id })) @@ -420,6 +421,13 @@ const daemonLayer = Layer.effect( ), ), ), + // Like spawned commands: what a failed command left running in its group is stopped with it, while a + // successful one may leave background processes (`cmd &`) behind. + Effect.tap((outcome) => + outcome.status === "exited" && outcome.exit !== undefined && outcome.exit !== 0 + ? pty.signalPipe(run.process, "SIGTERM").pipe(Effect.ignore) + : Effect.void, + ), Effect.flatMap((outcome) => finish(run, outcome)), ), ) diff --git a/packages/core/test/shell-daemon.test.ts b/packages/core/test/shell-daemon.test.ts index d087f4de833..d18f5b897b0 100644 --- a/packages/core/test/shell-daemon.test.ts +++ b/packages/core/test/shell-daemon.test.ts @@ -157,6 +157,24 @@ describe("Shell on the process daemon", () => { ) })) + test("stops what a failed command left in its group, and leaves a successful one's", () => + Effect.gen(function* () { + const env = yield* daemonShells() + yield* env.withShell(env.directory, (shell) => + Effect.gen(function* () { + const succeeded = yield* shell.create({ shell: "/bin/sh", command: "serve & exit 0" }) + const failed = yield* shell.create({ shell: "/bin/sh", command: "serve & exit 3" }) + yield* until(() => env.daemon.waiters(1) > 0 && env.daemon.waiters(2) > 0) + env.daemon.exit(1, 0) + env.daemon.exit(2, 3) + expect(yield* shell.wait(failed.id)).toMatchObject({ status: "exited", exit: 3 }) + expect(yield* shell.wait(succeeded.id)).toMatchObject({ status: "exited", exit: 0 }) + expect(env.signals(2)).toEqual(["SIGTERM"]) + expect(env.signals(1)).toEqual([]) + }), + ) + })) + test("fails a command the daemon rejected instead of spawning it", () => Effect.gen(function* () { const env = yield* daemonShells()