@devries-cameron20 / t3code Mirror · read-only / commits / 8ddd9f7efc
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 02:45 UTC

fix(desktop): bound backend shutdown wait during quit (#7599)

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>

Ishaan Kothari committed Sep 12, 2026 at 16:43 UTC 8ddd9f7efcdfb6e32b614595242b5986942d37b0
2 files changed +95 -12
apps/desktop/src/app/DesktopApp.ts
+21 -12
@@ -1,4 +1,5 @@
1 import * as Cause from "effect/Cause";
2 +import * as Duration from "effect/Duration";
3 import * as Effect from "effect/Effect";
4 import * as Option from "effect/Option";
5 import * as Ref from "effect/Ref";
@@ -142,6 +143,20 @@ const handleFatalStartupError = Effect.fn("desktop.startup.handleFatalStartupErr
143 const fatalStartupCause = <E>(stage: string, cause: Cause.Cause<E>) =>
144 handleFatalStartupError(stage, Cause.pretty(cause)).pipe(Effect.andThen(Effect.failCause(cause)));
145
146 +export const stopAllPoolInstances = Effect.fn("desktop.app.stopAllPoolInstances")(
147 + function* (): Effect.fn.Return<void, never, DesktopBackendPool.DesktopBackendPool> {
148 + // Stop every backend in the pool with a timeout to guarantee the quit
149 + // path makes progress even if a backend hangs during teardown.
150 + const pool = yield* DesktopBackendPool.DesktopBackendPool;
151 + const instances = yield* pool.list;
152 + yield* Effect.forEach(
153 + instances,
154 + (instance) => instance.stop({ timeout: Duration.seconds(5) }),
155 + { concurrency: "unbounded" },
156 + );
157 + },
158 +);
159 +
160 const bootstrap = Effect.gen(function* () {
161 const pool = yield* DesktopBackendPool.DesktopBackendPool;
162 const primaryBackend = yield* pool.primary;
@@ -313,18 +328,12 @@ const scopedProgram = Effect.scoped(
328 const shutdown = yield* DesktopShutdown.DesktopShutdown;
329
330 yield* Effect.addFinalizer(() =>
316 - Effect.gen(function* () {
317 - const pool = yield* DesktopBackendPool.DesktopBackendPool;
318 - // Stop every backend in the pool, not just the primary. The
319 - // electronApp.quit() path can race ahead of the layer-scope
320 - // cascade, so leaving the WSL instance for its parent scope
321 - // finalizer means it gets hard-killed by the OS instead of
322 - // receiving SIGTERM + grace. Stops run concurrently.
323 - const instances = yield* pool.list;
324 - yield* Effect.forEach(instances, (instance) => instance.stop(), {
325 - concurrency: "unbounded",
326 - });
327 - }).pipe(Effect.ensuring(shutdown.markComplete)),
331 + // Stop every backend in the pool, not just the primary. The
332 + // electronApp.quit() path can race ahead of the layer-scope
333 + // cascade, so leaving the WSL instance for its parent scope
334 + // finalizer means it gets hard-killed by the OS instead of
335 + // receiving SIGTERM + grace.
336 + stopAllPoolInstances().pipe(Effect.ensuring(shutdown.markComplete)),
337 );
338
339 yield* startup;
apps/desktop/src/backend/DesktopBackendManager.test.ts
+74
@@ -23,6 +23,8 @@ import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstab
23 import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
24
25 import * as DesktopBackendManager from "./DesktopBackendManager.ts";
26 +import * as DesktopApp from "../app/DesktopApp.ts";
27 +import * as DesktopBackendPool from "./DesktopBackendPool.ts";
28 import * as DesktopObservability from "../app/DesktopObservability.ts";
29 import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts";
30 import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts";
@@ -1503,4 +1505,76 @@ describe("DesktopBackendManager", () => {
1505 }).pipe(Effect.provide(TestClock.layer())),
1506 ),
1507 );
1508 +
1509 + it.effect("stopAllPoolInstances bounds the quit finalizer when backends hang", () =>
1510 + Effect.scoped(
1511 + Effect.gen(function* () {
1512 + // Each backend's process-scope finalizer reports when it starts and
1513 + // when it finishes, keyed by instance name, so the test can prove
1514 + // both backends reached each milestone instead of inferring it from
1515 + // a shared flag or a clock advance.
1516 + const teardownStarted = yield* Queue.unbounded<string>();
1517 + const teardownFinished = yield* Queue.unbounded<string>();
1518 + const allowTeardown = yield* Deferred.make<void>();
1519 +
1520 + const makeInstance = (name: string) =>
1521 + makeTestInstance({
1522 + spawnerLayer: Layer.succeed(
1523 + ChildProcessSpawner.ChildProcessSpawner,
1524 + ChildProcessSpawner.make(() =>
1525 + Effect.gen(function* () {
1526 + const scope = yield* Scope.Scope;
1527 + yield* Scope.addFinalizer(
1528 + scope,
1529 + Queue.offer(teardownStarted, name).pipe(
1530 + Effect.andThen(Deferred.await(allowTeardown)),
1531 + Effect.andThen(Queue.offer(teardownFinished, name)),
1532 + Effect.asVoid,
1533 + ),
1534 + );
1535 + return makeProcess({ exitCode: Effect.never });
1536 + }),
1537 + ),
1538 + ),
1539 + httpClientLayer: httpClientLayer(() => Effect.never),
1540 + });
1541 +
1542 + const instance1 = yield* makeInstance("instance1");
1543 + const instance2 = yield* makeInstance("instance2");
1544 +
1545 + yield* instance1.start;
1546 + yield* instance2.start;
1547 +
1548 + const mockPool = Layer.succeed(DesktopBackendPool.DesktopBackendPool, {
1549 + list: Effect.succeed([instance1, instance2]),
1550 + get: () => Effect.succeed(Option.none()),
1551 + primary: Effect.die(new Error("primary not implemented")),
1552 + register: () => Effect.die(new Error("register not implemented")),
1553 + unregister: () => Effect.die(new Error("unregister not implemented")),
1554 + });
1555 +
1556 + // Mirror the quit path: register stopAllPoolInstances as a scope
1557 + // finalizer and let the scope close run it, rather than calling it
1558 + // as an ordinary interruptible effect.
1559 + const quitFiber = yield* Effect.scoped(
1560 + Effect.addFinalizer(() => DesktopApp.stopAllPoolInstances()),
1561 + ).pipe(Effect.provide(mockPool), Effect.forkChild);
1562 +
1563 + const started = yield* Queue.takeN(teardownStarted, 2);
1564 + assert.deepEqual(started.toSorted(), ["instance1", "instance2"]);
1565 +
1566 + // Both backends are now hung in teardown. Advancing past the 5s
1567 + // budget must let the quit finalizer return without them.
1568 + yield* TestClock.adjust(Duration.seconds(5));
1569 + yield* Fiber.join(quitFiber);
1570 + assert.equal(yield* Queue.size(teardownFinished), 0);
1571 +
1572 + // The timed-out closes keep running in the background and finish
1573 + // once the backends unblock.
1574 + yield* Deferred.succeed(allowTeardown, undefined);
1575 + const finished = yield* Queue.takeN(teardownFinished, 2);
1576 + assert.deepEqual(finished.toSorted(), ["instance1", "instance2"]);
1577 + }).pipe(Effect.provide(TestClock.layer())),
1578 + ),
1579 + );
1580 });