@devries-cameron20 / t3code Mirror · read-only / commits / 0b1b9d471a
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 21, 19:45 UTC

fix(desktop): app opens after an update while the old server shuts down (#34932)

Julius Marminge committed Nov 12, 2026 at 00:08 UTC 0b1b9d471aed8cd1576824653148c38057f593dc
3 files changed +54 -24
apps/desktop/src/app/DesktopApp.ts
+1 -1
@@ -227,7 +227,7 @@ const bootstrap = Effect.gen(function* () {
227 // A backend started while another server holds this home's lock can only exit with code 78.
228 if (
229 !(settings.wslOnly && settings.wslBackendEnabled) &&
230 - (yield* DesktopHomeOwner.isHomeOwnershipLocked())
230 + !(yield* DesktopHomeOwner.awaitHomeOwnershipRelease())
231 ) {
232 const owner = yield* DesktopHomeOwner.readLiveHomeOwner();
233 yield* logBootstrapWarning("bootstrap found another server owning the T3 home", {
apps/desktop/src/backend/DesktopHomeOwner.test.ts
+17 -5
@@ -3,10 +3,12 @@ import * as NodeSqlite from "node:sqlite";
3 import * as NodeServices from "@effect/platform-node/NodeServices";
4 import { assert, describe, it } from "@effect/vitest";
5 import * as Effect from "effect/Effect";
6 +import * as Fiber from "effect/Fiber";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Layer from "effect/Layer";
9 import * as Option from "effect/Option";
10 import * as Path from "effect/Path";
11 +import * as TestClock from "effect/testing/TestClock";
12
13 import * as DesktopConfig from "../app/DesktopConfig.ts";
14 import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
@@ -94,16 +96,26 @@ describe("DesktopHomeOwner.readLiveHomeOwner", () => {
96 );
97 });
98
97 -describe("DesktopHomeOwner.isHomeOwnershipLocked", () => {
98 - it.effect("reports the ownership lock only while a server holds it", () =>
99 +describe("DesktopHomeOwner.awaitHomeOwnershipRelease", () => {
100 + it.effect("waits for a shutting-down owner to release the lock, then gives up", () =>
101 withTempHome((userdata) =>
102 Effect.gen(function* () {
101 - assert.isFalse(yield* DesktopHomeOwner.isHomeOwnershipLocked());
103 + assert.isTrue(yield* DesktopHomeOwner.awaitHomeOwnershipRelease());
104 +
105 const lock = new NodeSqlite.DatabaseSync(`${userdata}/server-owner.sqlite`);
106 lock.exec("BEGIN EXCLUSIVE;");
104 - assert.isTrue(yield* DesktopHomeOwner.isHomeOwnershipLocked());
107 + const released = yield* Effect.forkChild(DesktopHomeOwner.awaitHomeOwnershipRelease());
108 + yield* TestClock.adjust("5 seconds");
109 lock.close();
106 - assert.isFalse(yield* DesktopHomeOwner.isHomeOwnershipLocked());
110 + yield* TestClock.adjust("1 second");
111 + assert.isTrue(yield* Fiber.join(released));
112 +
113 + const held = new NodeSqlite.DatabaseSync(`${userdata}/server-owner.sqlite`);
114 + held.exec("BEGIN EXCLUSIVE;");
115 + const owned = yield* Effect.forkChild(DesktopHomeOwner.awaitHomeOwnershipRelease());
116 + yield* TestClock.adjust("1 minute");
117 + assert.isFalse(yield* Fiber.join(owned));
118 + held.close();
119 }),
120 ),
121 );
apps/desktop/src/backend/DesktopHomeOwner.ts
+36 -18
@@ -1,6 +1,7 @@
1 // @effect-diagnostics nodeBuiltinImport:off - Probes the server's native SQLite ownership lock.
2 import * as NodeSqlite from "node:sqlite";
3
4 +import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Option from "effect/Option";
@@ -8,6 +9,7 @@ import * as Path from "effect/Path";
9 import * as Schema from "effect/Schema";
10
11 import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
12 +import { BACKEND_QUIT_STOP_TIMEOUT } from "./DesktopBackendManager.ts";
13
14 // The fields of the server's `server-runtime.json` that identify the owner of a T3 home.
15 const HomeOwnerRuntimeState = Schema.Struct({
@@ -35,27 +37,43 @@ const isProcessAlive = (pid: number): boolean => {
37 * Whether a server holds the T3 home's ownership lock (an exclusive transaction on
38 * `server-owner.sqlite`). A read-only read is refused with SQLITE_BUSY only while it is held.
39 */
38 -export const isHomeOwnershipLocked = Effect.fn("DesktopHomeOwner.isHomeOwnershipLocked")(
39 - function* () {
40 - const path = yield* Path.Path;
41 - const { stateDir } = yield* DesktopEnvironment.DesktopEnvironment;
42 - const lockPath = path.join(stateDir, "server-owner.sqlite");
43 - return yield* Effect.sync(() => {
40 +const isHomeOwnershipLocked = Effect.fn("DesktopHomeOwner.isHomeOwnershipLocked")(function* () {
41 + const path = yield* Path.Path;
42 + const { stateDir } = yield* DesktopEnvironment.DesktopEnvironment;
43 + const lockPath = path.join(stateDir, "server-owner.sqlite");
44 + return yield* Effect.sync(() => {
45 + try {
46 + const db = new NodeSqlite.DatabaseSync(lockPath, { readOnly: true });
47 try {
45 - const db = new NodeSqlite.DatabaseSync(lockPath, { readOnly: true });
46 - try {
47 - db.exec("PRAGMA busy_timeout = 0");
48 - db.prepare("SELECT count(*) FROM sqlite_master").get();
49 - return false;
50 - } finally {
51 - db.close();
52 - }
53 - } catch (cause) {
54 - // Anything but SQLITE_BUSY (a missing file included) leaves the decision to the backend.
55 - return cause instanceof Error && "errcode" in cause && cause.errcode === 5;
48 + db.exec("PRAGMA busy_timeout = 0");
49 + db.prepare("SELECT count(*) FROM sqlite_master").get();
50 + return false;
51 + } finally {
52 + db.close();
53 }
57 - });
54 + } catch (cause) {
55 + // Anything but SQLITE_BUSY (a missing file included) leaves the decision to the backend.
56 + return cause instanceof Error && "errcode" in cause && cause.errcode === 5;
57 + }
58 + });
59 +});
60 +
61 +// A quitting app's backend holds the lock until it exits, which can outlast the relaunch of an update.
62 +const HOME_OWNERSHIP_RELEASE_GRACE = Duration.sum(BACKEND_QUIT_STOP_TIMEOUT, Duration.seconds(3));
63 +const HOME_OWNERSHIP_RELEASE_POLL_INTERVAL = Duration.millis(250);
64 +
65 +/** Whether the ownership lock is free, or frees within the grace a shutting-down server needs. */
66 +export const awaitHomeOwnershipRelease = Effect.fn("DesktopHomeOwner.awaitHomeOwnershipRelease")(
67 + function* () {
68 + while (yield* isHomeOwnershipLocked()) {
69 + yield* Effect.sleep(HOME_OWNERSHIP_RELEASE_POLL_INTERVAL);
70 + }
71 + return true;
72 },
73 + Effect.timeoutOrElse({
74 + duration: HOME_OWNERSHIP_RELEASE_GRACE,
75 + orElse: () => Effect.succeed(false),
76 + }),
77 );
78
79 /** 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. */