Skip to content

Commit b8aec46

Browse files
author
Krish
committed
fix(desktop): startup waits for a draining owner to release the T3 home
A server keeps the ownership lock until its shutdown drain closes the state directory. When a desktop launch probes the lock in that window — most often right after an update relaunch, or while an orphaned backend finishes its self-shutdown — the one-shot check read the home as permanently owned and showed the blocking 'This T3 home is unavailable' dialog even though the lock was seconds from free. Poll the lock for a short grace window during bootstrap and only report the home unavailable when it is still held, so a draining owner finishing up no longer blocks the replacement launch.
1 parent 3a30b01 commit b8aec46

3 files changed

Lines changed: 67 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,12 @@ const bootstrap = Effect.gen(function* () {
224224
return yield* new DesktopDevelopmentBackendPortRequiredError();
225225
}
226226

227-
// A backend started while another server holds this home's lock can only exit with code 78.
227+
// A backend started while another server holds this home's lock can only exit
228+
// with code 78, but a previous server keeps the lock through its shutdown
229+
// drain, so give a draining owner a short grace to release first.
228230
if (
229231
!(settings.wslOnly && settings.wslBackendEnabled) &&
230-
(yield* DesktopHomeOwner.isHomeOwnershipLocked())
232+
!(yield* DesktopHomeOwner.awaitHomeOwnershipRelease())
231233
) {
232234
const owner = yield* DesktopHomeOwner.readLiveHomeOwner();
233235
yield* logBootstrapWarning("bootstrap found another server owning the T3 home", {

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import * as NodeSqlite from "node:sqlite";
22

33
import * as NodeServices from "@effect/platform-node/NodeServices";
44
import { assert, describe, it } from "@effect/vitest";
5+
import * as Duration from "effect/Duration";
56
import * as Effect from "effect/Effect";
7+
import * as Fiber from "effect/Fiber";
68
import * as FileSystem from "effect/FileSystem";
79
import * as Layer from "effect/Layer";
810
import * as Option from "effect/Option";
911
import * as Path from "effect/Path";
12+
import * as TestClock from "effect/testing/TestClock";
1013

1114
import * as DesktopConfig from "../app/DesktopConfig.ts";
1215
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
@@ -108,3 +111,37 @@ describe("DesktopHomeOwner.isHomeOwnershipLocked", () => {
108111
),
109112
);
110113
});
114+
115+
describe("DesktopHomeOwner.awaitHomeOwnershipRelease", () => {
116+
it.effect("resolves as soon as a draining owner releases the lock", () =>
117+
withTempHome((userdata) =>
118+
Effect.gen(function* () {
119+
assert.isTrue(yield* DesktopHomeOwner.awaitHomeOwnershipRelease());
120+
121+
const lock = new NodeSqlite.DatabaseSync(`${userdata}/server-owner.sqlite`);
122+
lock.exec("BEGIN EXCLUSIVE;");
123+
const awaiting = yield* Effect.forkChild(DesktopHomeOwner.awaitHomeOwnershipRelease());
124+
yield* TestClock.adjust(Duration.seconds(5));
125+
assert.isUndefined(awaiting.pollUnsafe());
126+
127+
// A previous server releases the lock when its shutdown drain finishes.
128+
lock.close();
129+
yield* TestClock.adjust(Duration.seconds(1));
130+
assert.isTrue(yield* Fiber.join(awaiting));
131+
}),
132+
),
133+
);
134+
135+
it.effect("reports a home still owned after the release grace", () =>
136+
withTempHome((userdata) =>
137+
Effect.gen(function* () {
138+
const lock = new NodeSqlite.DatabaseSync(`${userdata}/server-owner.sqlite`);
139+
lock.exec("BEGIN EXCLUSIVE;");
140+
const awaiting = yield* Effect.forkChild(DesktopHomeOwner.awaitHomeOwnershipRelease());
141+
yield* TestClock.adjust(Duration.minutes(1));
142+
assert.isFalse(yield* Fiber.join(awaiting));
143+
lock.close();
144+
}),
145+
),
146+
);
147+
});

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @effect-diagnostics nodeBuiltinImport:off - Probes the server's native SQLite ownership lock.
22
import * as NodeSqlite from "node:sqlite";
33

4+
import * as Duration from "effect/Duration";
45
import * as Effect from "effect/Effect";
56
import * as FileSystem from "effect/FileSystem";
67
import * as Option from "effect/Option";
@@ -58,6 +59,31 @@ export const isHomeOwnershipLocked = Effect.fn("DesktopHomeOwner.isHomeOwnership
5859
},
5960
);
6061

62+
const HOME_OWNERSHIP_RELEASE_GRACE = Duration.seconds(15);
63+
const HOME_OWNERSHIP_RELEASE_POLL_INTERVAL = Duration.millis(250);
64+
65+
/**
66+
* Whether the ownership lock frees within a grace window. The previous owner
67+
* holds the lock through its shutdown drain, so a launch that checks during
68+
* the drain must wait rather than read the home as permanently owned.
69+
*/
70+
export const awaitHomeOwnershipRelease = Effect.fn("DesktopHomeOwner.awaitHomeOwnershipRelease")(
71+
function* (options?: { readonly grace?: Duration.Input; readonly interval?: Duration.Input }) {
72+
const released = Effect.gen(function* () {
73+
while (yield* isHomeOwnershipLocked()) {
74+
yield* Effect.sleep(options?.interval ?? HOME_OWNERSHIP_RELEASE_POLL_INTERVAL);
75+
}
76+
return true;
77+
});
78+
return yield* released.pipe(
79+
Effect.timeoutOrElse({
80+
duration: options?.grace ?? HOME_OWNERSHIP_RELEASE_GRACE,
81+
orElse: () => Effect.succeed(false),
82+
}),
83+
);
84+
},
85+
);
86+
6187
/** The live server recorded as owning the desktop's T3 home. Only call once the ownership lock is known held: a live PID alone may be reused. */
6288
export const readLiveHomeOwner = Effect.fn("DesktopHomeOwner.readLiveHomeOwner")(function* () {
6389
const fs = yield* FileSystem.FileSystem;

0 commit comments

Comments
 (0)