@devries-cameron20 / t3code Mirror · read-only / commits / 69726aac2f
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 22, 03:15 UTC

fix(desktop): quit no longer orphans a backend that is slow to stop (#22572)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Alex Southwell committed Nov 6, 2026 at 04:22 UTC 69726aac2f6f194c3e3582c134bfabcb44e6ca8c
4 files changed +65 -3
apps/desktop/src/app/DesktopApp.ts
+2 -2
@@ -1,6 +1,5 @@
1 import * as Cause from "effect/Cause";
2 import * as Config from "effect/Config";
3 -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";
@@ -19,6 +18,7 @@ import * as DesktopClerk from "./DesktopClerk.ts";
18 import * as DesktopApplicationMenu from "../window/DesktopApplicationMenu.ts";
19 import * as DesktopWindow from "../window/DesktopWindow.ts";
20 import * as PreviewPasskeys from "../preview/Passkeys.ts";
21 +import * as DesktopBackendManager from "../backend/DesktopBackendManager.ts";
22 import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
23 import * as DesktopEnvironment from "./DesktopEnvironment.ts";
24 import * as DesktopLegacyLocalStorage from "./DesktopLegacyLocalStorage.ts";
@@ -160,7 +160,7 @@ const stopAllPoolInstances = Effect.fn("desktop.app.stopAllPoolInstances")(
160 const instances = yield* pool.list;
161 yield* Effect.forEach(
162 instances,
163 - (instance) => instance.stop({ timeout: Duration.seconds(5) }),
163 + (instance) => instance.stop({ timeout: DesktopBackendManager.BACKEND_QUIT_STOP_TIMEOUT }),
164 { concurrency: "unbounded" },
165 );
166 },
apps/desktop/src/backend/DesktopBackendManager.test.ts
+54
@@ -720,4 +720,58 @@ describe("DesktopBackendManager", () => {
720 }).pipe(Effect.provide(TestClock.layer())),
721 ),
722 );
723 +
724 + it.effect("the quit stop deadline waits for a backend that only exits at the force kill", () =>
725 + Effect.scoped(
726 + Effect.gen(function* () {
727 + const spawned = yield* Deferred.make<void>();
728 + const teardownStarted = yield* Deferred.make<void>();
729 + const teardownFinished = yield* Deferred.make<void>();
730 + // The real spawner sends SIGTERM, waits the grace, then SIGKILLs. A backend that
731 + // ignores SIGTERM therefore finishes tearing down just after the grace.
732 + const instance = yield* makeTestInstance({
733 + spawnerLayer: Layer.succeed(
734 + ChildProcessSpawner.ChildProcessSpawner,
735 + ChildProcessSpawner.make(() =>
736 + Effect.gen(function* () {
737 + const scope = yield* Scope.Scope;
738 + yield* Deferred.succeed(spawned, undefined);
739 + yield* Scope.addFinalizer(
740 + scope,
741 + Deferred.succeed(teardownStarted, undefined).pipe(
742 + Effect.andThen(Effect.sleep(Duration.seconds(6))),
743 + Effect.andThen(Deferred.succeed(teardownFinished, undefined)),
744 + Effect.asVoid,
745 + ),
746 + );
747 + return makeProcess({
748 + exitCode: Deferred.await(teardownFinished).pipe(
749 + Effect.as(ChildProcessSpawner.ExitCode(0)),
750 + ),
751 + });
752 + }),
753 + ),
754 + ),
755 + httpClientLayer: layerHttpClient(() => Effect.never),
756 + });
757 + yield* instance.start;
758 + yield* Deferred.await(spawned);
759 +
760 + // App quit and the updater stop every backend with this deadline.
761 + const quitDone = yield* Deferred.make<void>();
762 + const quitFiber = yield* instance
763 + .stop({ timeout: DesktopBackendManager.BACKEND_QUIT_STOP_TIMEOUT })
764 + .pipe(Effect.andThen(Deferred.succeed(quitDone, undefined)), Effect.forkChild);
765 +
766 + yield* Deferred.await(teardownStarted);
767 + // Past the SIGTERM grace but before the force kill lands: quit must still be waiting.
768 + yield* TestClock.adjust(Duration.millis(5_500));
769 + assert.isFalse(yield* Deferred.isDone(quitDone));
770 +
771 + yield* TestClock.adjust(Duration.millis(500));
772 + yield* Fiber.join(quitFiber);
773 + assert.isTrue(yield* Deferred.isDone(teardownFinished));
774 + }).pipe(Effect.provide(TestClock.layer())),
775 + ),
776 + );
777 });
apps/desktop/src/backend/DesktopBackendManager.ts
+6
@@ -41,6 +41,12 @@ const DEFAULT_BACKEND_READINESS_INTERVAL = Duration.millis(100);
41 const DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT = Duration.seconds(1);
42 // Match the service launcher's grace period so backends can stop provider sessions.
43 const DEFAULT_BACKEND_TERMINATE_GRACE = Duration.seconds(5);
44 +// Allow SIGKILL and the spawner exit wait after the SIGTERM grace so quit cannot
45 +// orphan a backend whose shutdown hangs.
46 +export const BACKEND_QUIT_STOP_TIMEOUT = Duration.sum(
47 + DEFAULT_BACKEND_TERMINATE_GRACE,
48 + Duration.seconds(2),
49 +);
50 const DEFAULT_BACKEND_OUTPUT_DRAIN_TIMEOUT = Duration.seconds(5);
51 const BACKEND_READINESS_PATH = "/.well-known/t3/environment";
52 // Three restarts without readiness is about 2 s of backoff: past a blip, early enough to help.
apps/desktop/src/updates/DesktopUpdates.ts
+3 -1
@@ -25,6 +25,7 @@ import * as Scope from "effect/Scope";
25 import * as Semaphore from "effect/Semaphore";
26 import * as Stream from "effect/Stream";
27
28 +import * as DesktopBackendManager from "../backend/DesktopBackendManager.ts";
29 import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
30 import * as DesktopConfig from "../app/DesktopConfig.ts";
31 import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
@@ -819,7 +820,8 @@ export const make = Effect.gen(function* () {
820 yield* Ref.set(installRunningBackendsRef, runningInstances);
821 yield* Effect.forEach(
822 instances,
822 - (instance) => instance.stop({ timeout: Duration.seconds(5) }),
823 + (instance) =>
824 + instance.stop({ timeout: DesktopBackendManager.BACKEND_QUIT_STOP_TIMEOUT }),
825 { concurrency: "unbounded" },
826 );
827 yield* electronUpdater.quitAndInstall({