Skip to content

Commit 6aab59a

Browse files
fix(desktop): probe the T3 home ownership lock before starting the backend
The desktop picked a free port and started a backend even when another server held the T3 home, so that backend could only exit 78. Before choosing a port, the desktop now reads server-owner.sqlite read-only with no busy timeout: SQLITE_BUSY means a live server holds the server's own ownership lock, unlike a PID in server-runtime.json, which can be reused. When the lock is held it shows the owner from server-runtime.json and quits without spawning. The advice now explains that a T3 Code app which started the server over SSH restarts it, so that SSH environment must be removed or disconnected (or that app quit) first. AI-assisted; verified with the tests and tools listed above.
1 parent 3443b02 commit 6aab59a

4 files changed

Lines changed: 93 additions & 9 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ it.effect.each([false, true])(
108108
platform: "linux",
109109
isDevelopment: false,
110110
isPackaged: true,
111+
stateDir: "/isolated/userdata",
111112
configuredBackendPort: Option.some(3773),
112113
defaultDesktopSettings: { serverExposureMode: "local-only" },
113114
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]),

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as DesktopWindow from "../window/DesktopWindow.ts";
2121
import * as PreviewPasskeys from "../preview/Passkeys.ts";
2222
import * as DesktopBackendManager from "../backend/DesktopBackendManager.ts";
2323
import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
24+
import * as DesktopHomeOwner from "../backend/DesktopHomeOwner.ts";
2425
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
2526
import * as DesktopLegacyLocalStorage from "./DesktopLegacyLocalStorage.ts";
2627
import * as DesktopLifecycle from "./DesktopLifecycle.ts";
@@ -223,6 +224,22 @@ const bootstrap = Effect.gen(function* () {
223224
return yield* new DesktopDevelopmentBackendPortRequiredError();
224225
}
225226

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())
231+
) {
232+
const owner = yield* DesktopHomeOwner.readLiveHomeOwner();
233+
yield* logBootstrapWarning("bootstrap found another server owning the T3 home", {
234+
owner: Option.getOrNull(owner),
235+
});
236+
yield* (yield* ElectronDialog.ElectronDialog).showErrorBox(
237+
"This T3 home is unavailable",
238+
DesktopHomeOwner.homeOwnedMessage(owner),
239+
);
240+
return yield* (yield* ElectronApp.ElectronApp).quit;
241+
}
242+
226243
const backendPortSelection = yield* resolveDesktopBackendPort(environment.configuredBackendPort);
227244
const backendPort = backendPortSelection.port;
228245
yield* logBootstrapInfo(

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1+
import * as NodeSqlite from "node:sqlite";
2+
13
import * as NodeServices from "@effect/platform-node/NodeServices";
24
import { assert, describe, it } from "@effect/vitest";
35
import * as Effect from "effect/Effect";
46
import * as FileSystem from "effect/FileSystem";
57
import * as Layer from "effect/Layer";
68
import * as Option from "effect/Option";
9+
import * as Path from "effect/Path";
710

811
import * as DesktopConfig from "../app/DesktopConfig.ts";
912
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
1013
import * as DesktopHomeOwner from "./DesktopHomeOwner.ts";
1114

12-
const readOwnerWithRuntimeFile = (contents: string | undefined) =>
15+
const withTempHome = <A, E>(
16+
run: (
17+
userdata: string,
18+
) => Effect.Effect<
19+
A,
20+
E,
21+
DesktopEnvironment.DesktopEnvironment | FileSystem.FileSystem | Path.Path
22+
>,
23+
) =>
1324
Effect.gen(function* () {
1425
const fs = yield* FileSystem.FileSystem;
1526
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-desktop-home-owner-" });
16-
if (contents !== undefined) {
17-
yield* fs.makeDirectory(`${home}/userdata`, { recursive: true });
18-
yield* fs.writeFileString(`${home}/userdata/server-runtime.json`, contents);
19-
}
20-
return yield* DesktopHomeOwner.readLiveHomeOwner().pipe(
27+
yield* fs.makeDirectory(`${home}/userdata`, { recursive: true });
28+
return yield* run(`${home}/userdata`).pipe(
2129
Effect.provide(
2230
DesktopEnvironment.layer({
2331
dirname: "/repo/apps/desktop/src",
@@ -38,6 +46,19 @@ const readOwnerWithRuntimeFile = (contents: string | undefined) =>
3846
);
3947
}).pipe(Effect.provide(NodeServices.layer));
4048

49+
const readOwnerWithRuntimeFile = (contents: string | undefined) =>
50+
withTempHome((userdata) =>
51+
Effect.gen(function* () {
52+
if (contents !== undefined) {
53+
yield* (yield* FileSystem.FileSystem).writeFileString(
54+
`${userdata}/server-runtime.json`,
55+
contents,
56+
);
57+
}
58+
return yield* DesktopHomeOwner.readLiveHomeOwner();
59+
}),
60+
);
61+
4162
describe("DesktopHomeOwner.readLiveHomeOwner", () => {
4263
it.effect("reports a live server recorded in server-runtime.json", () =>
4364
Effect.gen(function* () {
@@ -72,3 +93,18 @@ describe("DesktopHomeOwner.readLiveHomeOwner", () => {
7293
}),
7394
);
7495
});
96+
97+
describe("DesktopHomeOwner.isHomeOwnershipLocked", () => {
98+
it.effect("reports the ownership lock only while a server holds it", () =>
99+
withTempHome((userdata) =>
100+
Effect.gen(function* () {
101+
assert.isFalse(yield* DesktopHomeOwner.isHomeOwnershipLocked());
102+
const lock = new NodeSqlite.DatabaseSync(`${userdata}/server-owner.sqlite`);
103+
lock.exec("BEGIN EXCLUSIVE;");
104+
assert.isTrue(yield* DesktopHomeOwner.isHomeOwnershipLocked());
105+
lock.close();
106+
assert.isFalse(yield* DesktopHomeOwner.isHomeOwnershipLocked());
107+
}),
108+
),
109+
);
110+
});

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Probes the server's native SQLite ownership lock.
2+
import * as NodeSqlite from "node:sqlite";
3+
14
import * as Effect from "effect/Effect";
25
import * as FileSystem from "effect/FileSystem";
36
import * as Option from "effect/Option";
@@ -28,7 +31,34 @@ const isProcessAlive = (pid: number): boolean => {
2831
}
2932
};
3033

31-
/** The live server recorded as owning the desktop's T3 home. Only call once the ownership lock refused a backend: a live PID alone may be reused. */
34+
/**
35+
* Whether a server holds the T3 home's ownership lock (an exclusive transaction on
36+
* `server-owner.sqlite`). A read-only read is refused with SQLITE_BUSY only while it is held.
37+
*/
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(() => {
44+
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;
56+
}
57+
});
58+
},
59+
);
60+
61+
/** 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. */
3262
export const readLiveHomeOwner = Effect.fn("DesktopHomeOwner.readLiveHomeOwner")(function* () {
3363
const fs = yield* FileSystem.FileSystem;
3464
const path = yield* Path.Path;
@@ -52,7 +82,7 @@ export const homeOwnedMessage = (owner: Option.Option<HomeOwner>): string =>
5282
const version = owner.serverVersion ? `, version ${owner.serverVersion}` : "";
5383
const stop = owner.serviceManaged
5484
? "Stop it with `t3 service uninstall`, passing `--base-dir` with this T3 home"
55-
: "Stop that server";
56-
return `${kind} is using this T3 home (PID ${owner.pid}, port ${owner.port}${version}). ${stop}, then reopen T3 Code. You can also set a separate T3CODE_HOME and pair with the running server.`;
85+
: "If a T3 Code app on another machine started it over SSH, that app restarts it: remove or disconnect that SSH environment there, or quit that app. Then stop the server";
86+
return `${kind} is using this T3 home (PID ${owner.pid}, port ${owner.port}${version}). ${stop}, and reopen T3 Code. You can also set a separate T3CODE_HOME and pair with the running server.`;
5787
},
5888
});

0 commit comments

Comments
 (0)