mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-21 04:12:08 +03:00
fix(core): stop what a failed daemon shell command left in its group
This commit is contained in:
@@ -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)),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user