GeezerVison is showing github.com — fix(desktop): let lifecycle shutdown finish before exiting · pingdotgg/t3code@902293a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopApp.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -153,7 +153,8 @@ export const stopAllPoolInstances = Effect.fn("desktop.app.stopAllPoolInstances" | 153 | 153 | const instances = yield* pool.list; | 154 | 154 | yield* Effect.forEach( | 155 | 155 | instances, | 156 | | - (instance) => instance.stop({ timeout: Duration.seconds(5) }), | | 156 | + // Exceeds the backend's 30s graceful termination and output drain budget. | | 157 | + (instance) => instance.stop({ timeout: Duration.seconds(45) }), | 157 | 158 | { concurrency: "unbounded" }, | 158 | 159 | ); | 159 | 160 | }, | |
Collapse file Copy file name to clipboard +64Lines changed: 64 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// @effect-diagnostics-next-line nodeBuiltinImport:off - A real Node process verifies OS signal ownership outside Effect's process runtime. | | 2 | +import * as NodeChildProcess from "node:child_process"; | | 3 | +import * as NodeReadline from "node:readline"; | | 4 | +import * as NodeURL from "node:url"; | | 5 | +import { expect, it } from "@effect/vitest"; | | 6 | + | | 7 | +it("leaves SIGTERM to the lifecycle until its graceful shutdown completes", async ({ | | 8 | + onTestFinished, | | 9 | +}) => { | | 10 | + const runner = new URL("./DesktopRuntime.ts", import.meta.url).href; | | 11 | + const child = NodeChildProcess.spawn( | | 12 | + process.execPath, | | 13 | + [ | | 14 | + "--experimental-strip-types", | | 15 | + "--input-type=module", | | 16 | + "-e", | | 17 | + ` | | 18 | + import * as Effect from 'effect/Effect'; | | 19 | + import * as Deferred from 'effect/Deferred'; | | 20 | + import { runMain } from ${JSON.stringify(runner)}; | | 21 | + let released = false; | | 22 | + runMain(Effect.scoped(Effect.gen(function* () { | | 23 | + const done = yield* Deferred.make(); | | 24 | + yield* Effect.addFinalizer(() => Effect.sync(() => console.log(released ? 'CLEANED' : 'PREMATURE'))); | | 25 | + process.once('SIGTERM', () => { | | 26 | + console.log('STOPPING'); | | 27 | + process.stdin.once('data', () => { | | 28 | + released = true; | | 29 | + void Effect.runPromise(Deferred.succeed(done, undefined)).then(() => process.stdin.destroy()); | | 30 | + }); | | 31 | + }); | | 32 | + console.log('READY'); | | 33 | + yield* Deferred.await(done); | | 34 | + }))); | | 35 | + `, | | 36 | + ], | | 37 | + { | | 38 | + cwd: NodeURL.fileURLToPath(new URL("../../", import.meta.url)), | | 39 | + stdio: ["pipe", "pipe", "pipe"], | | 40 | + }, | | 41 | + ); | | 42 | + const exited = new Promise<readonly [number | null, string | null]>((resolve, reject) => { | | 43 | + child.once("exit", (code, signal) => resolve([code, signal])); | | 44 | + child.once("error", reject); | | 45 | + }); | | 46 | + const lines: string[] = []; | | 47 | + let stderr = ""; | | 48 | + child.stderr.on("data", (chunk) => { | | 49 | + stderr += chunk; | | 50 | + }); | | 51 | + onTestFinished(async () => { | | 52 | + if (child.exitCode === null && child.signalCode === null) { | | 53 | + child.kill("SIGKILL"); | | 54 | + await exited; | | 55 | + } | | 56 | + }); | | 57 | + for await (const line of NodeReadline.createInterface({ input: child.stdout })) { | | 58 | + lines.push(line); | | 59 | + if (line === "READY") child.kill("SIGTERM"); | | 60 | + if (line === "STOPPING") child.stdin.write("finish"); | | 61 | + } | | 62 | + expect(await exited, stderr).toEqual([0, null]); | | 63 | + expect(lines).toEqual(["READY", "STOPPING", "CLEANED"]); | | 64 | +}); |
Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as Runtime from "effect/Runtime"; | | 2 | + | | 3 | +// DesktopLifecycle owns signals and waits for backend shutdown before quitting | | 4 | +// Electron. NodeRuntime's competing signal handler interrupts that sequence. | | 5 | +export const runMain = Runtime.makeRunMain(({ fiber, teardown }) => { | | 6 | + fiber.addObserver((exit) => { | | 7 | + teardown(exit, (code) => { | | 8 | + if (code !== 0) process.exit(code); | | 9 | + }); | | 10 | + }); | | 11 | +}); |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/backend/DesktopBackendManager.test.ts +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -262,7 +262,7 @@ describe("DesktopBackendManager", () => { | 262 | 262 | assert.equal(spawnedCommand.options.additionalFds?.fd5?.type, "output"); | 263 | 263 | assert.equal( | 264 | 264 | Duration.toMillis(Duration.fromInputUnsafe(spawnedCommand.options.forceKillAfter)), | 265 | | - 5_000, | | 265 | + 30_000, | 266 | 266 | ); | 267 | 267 |
| 268 | 268 | assert.deepEqual(yield* decodeBootstrap(bootstrapJson), configWithObservability); | @@ -1572,7 +1572,10 @@ describe("DesktopBackendManager", () => { | 1572 | 1572 | const started = yield* Queue.takeN(teardownStarted, 2); | 1573 | 1573 | assert.deepEqual(started.toSorted(), ["instance1", "instance2"]); | 1574 | 1574 |
| 1575 | | - yield* TestClock.adjust(Duration.seconds(5)); | | 1575 | + yield* TestClock.adjust(Duration.seconds(6)); | | 1576 | + assert.isUndefined(quitFiber.pollUnsafe()); | | 1577 | + yield* TestClock.adjust(Duration.seconds(39)); | | 1578 | + | 1576 | 1579 | yield* Fiber.join(quitFiber); | 1577 | 1580 | assert.equal(yield* Queue.size(teardownFinished), 0); | 1578 | 1581 |
| |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/backend/DesktopBackendManager.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,7 +39,7 @@ const DEFAULT_BACKEND_READINESS_INTERVAL = Duration.millis(100); | 39 | 39 | const DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT = Duration.seconds(1); | 40 | 40 | // Same as the service launcher. A killed backend leaves its agents running, | 41 | 41 | // and a busy one needs a few seconds to stop every provider session. | 42 | | -const DEFAULT_BACKEND_TERMINATE_GRACE = Duration.seconds(5); | | 42 | +const DEFAULT_BACKEND_TERMINATE_GRACE = Duration.seconds(30); | 43 | 43 | const DEFAULT_BACKEND_OUTPUT_DRAIN_TIMEOUT = Duration.seconds(5); | 44 | 44 | const BACKEND_READINESS_PATH = "/.well-known/t3/environment"; | 45 | 45 | const { logWarning: logBackendProcessWarning } = | |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/main.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,7 +6,7 @@ for (const stream of [process.stdout, process.stderr]) { | 6 | 6 | } | 7 | 7 |
| 8 | 8 | import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient"; | 9 | | -import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; | | 9 | +import * as DesktopRuntime from "./app/DesktopRuntime.ts"; | 10 | 10 | import * as NodeServices from "@effect/platform-node/NodeServices"; | 11 | 11 | // @effect-diagnostics-next-line nodeBuiltinImport:off - Version output must flush before Electron exits, without acquiring the runtime. | 12 | 12 | import * as NodeFS from "node:fs"; | @@ -242,4 +242,4 @@ const layerDesktopRuntime = layerDesktopClerk.pipe( | 242 | 242 | Layer.provideMerge(DesktopPreReadyPlatform.layer), | 243 | 243 | ); | 244 | 244 |
| 245 | | -DesktopApp.program.pipe(Effect.provide(layerDesktopRuntime), NodeRuntime.runMain); | | 245 | +DesktopApp.program.pipe(Effect.provide(layerDesktopRuntime), DesktopRuntime.runMain); |
|