Skip to content

Commit 69726aa

Browse files
saphidgithub-actions[bot]claudemaria-rcks
authored
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 <[email protected]> Co-authored-by: SAPHID <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent fba1997 commit 69726aa

4 files changed

Lines changed: 65 additions & 3 deletions

File tree

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as Cause from "effect/Cause";
22
import * as Config from "effect/Config";
3-
import * as Duration from "effect/Duration";
43
import * as Effect from "effect/Effect";
54
import * as Option from "effect/Option";
65
import * as Ref from "effect/Ref";
@@ -19,6 +18,7 @@ import * as DesktopClerk from "./DesktopClerk.ts";
1918
import * as DesktopApplicationMenu from "../window/DesktopApplicationMenu.ts";
2019
import * as DesktopWindow from "../window/DesktopWindow.ts";
2120
import * as PreviewPasskeys from "../preview/Passkeys.ts";
21+
import * as DesktopBackendManager from "../backend/DesktopBackendManager.ts";
2222
import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
2323
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
2424
import * as DesktopLegacyLocalStorage from "./DesktopLegacyLocalStorage.ts";
@@ -160,7 +160,7 @@ const stopAllPoolInstances = Effect.fn("desktop.app.stopAllPoolInstances")(
160160
const instances = yield* pool.list;
161161
yield* Effect.forEach(
162162
instances,
163-
(instance) => instance.stop({ timeout: Duration.seconds(5) }),
163+
(instance) => instance.stop({ timeout: DesktopBackendManager.BACKEND_QUIT_STOP_TIMEOUT }),
164164
{ concurrency: "unbounded" },
165165
);
166166
},

‎apps/desktop/src/backend/DesktopBackendManager.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,4 +720,58 @@ describe("DesktopBackendManager", () => {
720720
}).pipe(Effect.provide(TestClock.layer())),
721721
),
722722
);
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+
);
723777
});

‎apps/desktop/src/backend/DesktopBackendManager.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ const DEFAULT_BACKEND_READINESS_INTERVAL = Duration.millis(100);
4141
const DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT = Duration.seconds(1);
4242
// Match the service launcher's grace period so backends can stop provider sessions.
4343
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+
);
4450
const DEFAULT_BACKEND_OUTPUT_DRAIN_TIMEOUT = Duration.seconds(5);
4551
const BACKEND_READINESS_PATH = "/.well-known/t3/environment";
4652
// Three restarts without readiness is about 2 s of backoff: past a blip, early enough to help.

‎apps/desktop/src/updates/DesktopUpdates.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as Scope from "effect/Scope";
2525
import * as Semaphore from "effect/Semaphore";
2626
import * as Stream from "effect/Stream";
2727

28+
import * as DesktopBackendManager from "../backend/DesktopBackendManager.ts";
2829
import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
2930
import * as DesktopConfig from "../app/DesktopConfig.ts";
3031
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
@@ -819,7 +820,8 @@ export const make = Effect.gen(function* () {
819820
yield* Ref.set(installRunningBackendsRef, runningInstances);
820821
yield* Effect.forEach(
821822
instances,
822-
(instance) => instance.stop({ timeout: Duration.seconds(5) }),
823+
(instance) =>
824+
instance.stop({ timeout: DesktopBackendManager.BACKEND_QUIT_STOP_TIMEOUT }),
823825
{ concurrency: "unbounded" },
824826
);
825827
yield* electronUpdater.quitAndInstall({

0 commit comments

Comments
 (0)