fix(core): forget stale finished daemon processes in the shell sweep

This commit is contained in:
James Long
2026-11-05 20:21:22 +00:00
Unverified
parent 9c5e408e82
commit 744907b928
2 changed files with 27 additions and 2 deletions
+13
View File
@@ -211,6 +211,19 @@ export const cleanup = Effect.fn("Shell.cleanup")(function* () {
return process ? pty.terminatePipe(process.id).pipe(Effect.ignore) : Effect.void
},
)
// Finished processes whose file is gone, or that ended long ago, are of no use to any Location: forget them.
const cutoff = Date.now() - Duration.toMillis(RETENTION)
yield* Effect.forEach(
processes.values(),
(process) =>
Effect.gen(function* () {
if (process.lifecycle.status === "running") return
const stale = (process.exitedAt ?? process.startedAt) < cutoff
if (!stale && (yield* fs.exists(process.outputPath).pipe(Effect.orElseSucceed(() => true)))) return
yield* pty.terminatePipe(process.id).pipe(Effect.ignore)
}),
{ concurrency: 8, discard: true },
)
})
const cleanupLayer = Layer.effectDiscard(
+14 -2
View File
@@ -1,6 +1,6 @@
import { describe, expect } from "bun:test"
import { existsSync, readdirSync, realpathSync } from "node:fs"
import { mkdir, utimes, writeFile } from "node:fs/promises"
import { mkdir, rm, utimes, writeFile } from "node:fs/promises"
import path from "node:path"
import { Effect, Layer } from "effect"
import { TestClock } from "effect/testing"
@@ -414,9 +414,19 @@ describe("Shell on the process daemon", () => {
const finished = yield* shell.create({ shell: "/bin/sh", command: "finished" })
env.daemon.exit(2, 0)
yield* shell.wait(finished.id)
return { running, finished }
const lostFile = yield* shell.create({ shell: "/bin/sh", command: "lost file" })
env.daemon.exit(3, 0)
yield* shell.wait(lostFile.id)
const old = yield* shell.create({ shell: "/bin/sh", command: "old" })
env.daemon.exit(4, 0)
yield* shell.wait(old.id)
return { running, finished, lostFile, old }
}),
)
// A finished process whose file was deleted elsewhere, and one that ended more than 7 days ago.
yield* Effect.promise(() => rm(ids.lostFile.file))
const ended = env.daemon.processes.find((process) => process.id === 4)
if (ended) ended.exited_at = Date.now() - 8 * 24 * 60 * 60 * 1_000
const expired = new Date(Date.now() - 8 * 24 * 60 * 60 * 1_000)
yield* Effect.promise(() => utimes(ids.running.file, expired, expired))
yield* Effect.promise(() => utimes(ids.finished.file, expired, expired))
@@ -437,6 +447,8 @@ describe("Shell on the process daemon", () => {
expect(existsSync(ids.finished.file)).toBe(false)
expect(existsSync(orphan)).toBe(false)
expect(env.daemon.processes.map((process) => process.id)).toEqual([1])
// The old process's file is recent, so it stays.
expect(existsSync(ids.old.file)).toBe(true)
}))
})