Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - The ownership lock is a native SQLite handle held for the server's lifetime.
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4import * as NodeSqlite from "node:sqlite";
5
6import { SERVER_EXIT_CODE_STATE_DIR_OWNED } from "@t3tools/contracts";
7import * as HostProcess from "@t3tools/shared/HostProcess";
8import * as Crypto from "effect/Crypto";
9import * as Duration from "effect/Duration";
10import * as Effect from "effect/Effect";
11import * as FileSystem from "effect/FileSystem";
12import * as Option from "effect/Option";
13import * as Runtime from "effect/Runtime";
14import * as Schema from "effect/Schema";
15
16import {
17 BOOT_SERVICE_LAUNCHD_LABEL,
18 BOOT_SERVICE_PLIST_FILE,
19 BOOT_SERVICE_UNIT_FILE,
20 bootServiceBaseDirOf,
21} from "./cloud/bootService.ts";
22import { ServiceLauncherHostProcess } from "./cloud/serviceLauncherClient.ts";
23import {
24 SERVICE_LAUNCHER_CONTEXT_ENV,
25 SERVICE_RESTART_PENDING_FILE,
26 SERVICE_STATE_FILE,
27 compareExactServiceVersions,
28 serviceStateActiveVersion,
29} from "./cloud/serviceProtocol.ts";
30import * as ProcessRunner from "./processRunner.ts";
31import {
32 clearPersistedServerRuntimeState,
33 isProcessAlive,
34 persistServerRuntimeState,
35 readPersistedServerRuntimeState,
36 type PersistedServerRuntimeState,
37} from "./serverRuntimeState.ts";
38
39export class ServerAlreadyRunningError extends Schema.TaggedError<ServerAlreadyRunningError>()(
40 "ServerAlreadyRunningError",
41 { stateDir: Schema.String },
42) {
43 // Distinct exit codes let supervisors stop restarting an already-owned server.
44 override readonly [Runtime.errorExitCode] = SERVER_EXIT_CODE_STATE_DIR_OWNED;
45
46 override get message(): string {
47 return `A T3 Code server already owns ${this.stateDir}. Stop that server, or use a separate T3 home and pair with it, then retry. No server was stopped.`;
48 }
49}
50
51export class OlderBootServiceError extends Schema.TaggedError<OlderBootServiceError>()(
52 "OlderBootServiceError",
53 { baseDir: Schema.String },
54) {
55 override readonly [Runtime.errorExitCode] = SERVER_EXIT_CODE_STATE_DIR_OWNED;
56
57 override get message(): string {
58 return `A T3 Code background service set up for ${this.baseDir} may still run a release without the ownership lock. On that machine, try \`t3 update\` and let it restart (or run \`t3 service restart\` if the update is already installed). If the older launcher cannot update, run \`t3 service uninstall\`, then reinstall with \`t3 service install\` from a current CLI. You can also leave the service uninstalled. Pass \`--base-dir ${this.baseDir}\` to each command, and use \`npx t3@latest\` if \`t3\` is not installed or is outdated. Then retry. No server was stopped.`;
59 }
60}
61
62export class ServerOwnershipError extends Schema.TaggedError<ServerOwnershipError>()(
63 "ServerOwnershipError",
64 { stateDir: Schema.String, cause: Schema.Defect() },
65) {
66 override get message(): string {
67 return `Could not acquire server ownership of ${this.stateDir}.`;
68 }
69}
70
71const isSqliteBusy = (cause: unknown) =>
72 cause instanceof Error &&
73 (("errcode" in cause && cause.errcode === 5) ||
74 ("code" in cause && cause.code === "SQLITE_BUSY"));
75
76// Pre-lock servers publish only runtime state; require start-time proof of PID reuse before treating it as stale.
77const legacyOwnerIsLive = Effect.fn("legacyOwnerIsLive")(function* (
78 state: PersistedServerRuntimeState,
79) {
80 if (!isProcessAlive(state.pid)) return false;
81 const recordedAt = Date.parse(state.startedAt);
82 if (!Number.isFinite(recordedAt)) return true;
83 const windows = (yield* HostProcess.Platform) === "win32";
84 const runner = yield* ProcessRunner.ProcessRunner;
85 const result = yield* runner
86 .run({
87 command: windows ? "powershell.exe" : "ps",
88 args: windows
89 ? [
90 "-NoProfile",
91 "-NonInteractive",
92 "-Command",
93 `(Get-Process -Id ${state.pid} -ErrorAction Stop).StartTime.ToUniversalTime().ToString('o')`,
94 ]
95 : ["-p", String(state.pid), "-o", "lstart="],
96 env: { LC_ALL: "C", TZ: "UTC" },
97 timeout: Duration.seconds(2),
98 maxOutputBytes: 16_384,
99 })
100 .pipe(Effect.option);
101 if (Option.isNone(result) || result.value.code !== 0) return true;
102 const output = result.value.stdout.trim();
103 const startedAt = Date.parse(windows ? output : `${output} UTC`);
104 // ps reports whole seconds. Unknown identity stays conservative.
105 return !Number.isFinite(startedAt) || startedAt <= recordedAt + 1_000;
106});
107
108// Pre-lock services become invisible when another server replaces their runtime
109// record. Unreadable configuration or an unavailable manager allows startup.
110const olderBootServiceHome = Effect.fn("olderBootServiceHome")(
111 function* (stateDir: string) {
112 if (
113 (yield* ServiceLauncherHostProcess).connected &&
114 (yield* HostProcess.Environment)[SERVICE_LAUNCHER_CONTEXT_ENV] !== undefined
115 ) {
116 return undefined;
117 }
118 const platform = yield* HostProcess.Platform;
119 const uid = yield* HostProcess.UserId;
120 if (platform !== "linux" && (platform !== "darwin" || uid === undefined)) return undefined;
121
122 const homeDir = yield* HostProcess.HomeDirectory;
123 const fs = yield* FileSystem.FileSystem;
124 const runner = yield* ProcessRunner.ProcessRunner;
125 const unitPath =
126 platform === "darwin"
127 ? NodePath.join(homeDir, "Library", "LaunchAgents", BOOT_SERVICE_PLIST_FILE)
128 : NodePath.join(homeDir, ".config", "systemd", "user", BOOT_SERVICE_UNIT_FILE);
129 const baseDir = bootServiceBaseDirOf(yield* fs.readFileString(unitPath));
130 if (
131 baseDir === undefined ||
132 (yield* fs.realPath(NodePath.join(baseDir, "userdata"))) !== (yield* fs.realPath(stateDir))
133 ) {
134 return undefined;
135 }
136 const runtimeDir = NodePath.join(baseDir, "runtime");
137 const version = serviceStateActiveVersion(
138 yield* fs.readFileString(NodePath.join(runtimeDir, SERVICE_STATE_FILE)),
139 );
140 if (version === undefined) return undefined;
141 const run = (command: string, args: ReadonlyArray<string>) =>
142 runner.run({ command, args, timeout: Duration.seconds(5), maxOutputBytes: 65_536 });
143 let loaded: boolean;
144 if (platform === "darwin") {
145 loaded =
146 (yield* run("launchctl", ["print", `gui/${uid}/${BOOT_SERVICE_LAUNCHD_LABEL}`])).code === 0;
147 if (!loaded) {
148 const disabled = yield* run("launchctl", ["print-disabled", `gui/${uid}`]);
149 // Older macOS versions print `true` instead of `disabled`.
150 if (
151 disabled.code !== 0 ||
152 disabled.stdout.includes(`"${BOOT_SERVICE_LAUNCHD_LABEL}" => disabled`) ||
153 disabled.stdout.includes(`"${BOOT_SERVICE_LAUNCHD_LABEL}" => true`)
154 )
155 return undefined;
156 }
157 } else {
158 const active = yield* run("systemctl", ["--user", "is-active", BOOT_SERVICE_UNIT_FILE]);
159 // A unit waiting out its Restart= delay is activating, with exit code 3.
160 loaded = active.code === 0 || (active.code === 3 && active.stdout.trim() === "activating");
161 if (!loaded) {
162 const enabled = yield* run("systemctl", ["--user", "is-enabled", BOOT_SERVICE_UNIT_FILE]);
163 if (enabled.code !== 0 || enabled.stdout.trim() !== "enabled") return undefined;
164 }
165 }
166 // A deferred update names the staged runtime, not the loaded service's runtime.
167 if (loaded && (yield* fs.exists(NodePath.join(runtimeDir, SERVICE_RESTART_PENDING_FILE)))) {
168 return baseDir;
169 }
170 // Stable 0.1.0 shipped the ownership lock. Earlier lock-aware prereleases
171 // are conservatively refused because older runtimes expose no capability marker.
172 return compareExactServiceVersions(version, "0.1.0") < 0 ? baseDir : undefined;
173 },
174 Effect.orElseSucceed(() => undefined),
175);
176
177/**
178 * Hold an exclusive lock on the state directory until the scope closes. The
179 * lock is an exclusive transaction on `server-owner.sqlite`, which never holds
180 * data and must never be unlinked: the OS releases it when the process exits,
181 * even on SIGKILL, so a crashed owner never blocks the next start.
182 */
183export const acquireServerOwnership = Effect.fn("acquireServerOwnership")(function* (
184 statePath: string,
185) {
186 const stateDir = NodePath.dirname(statePath);
187 const crypto = yield* Crypto.Crypto;
188 const ownerId = yield* crypto
189 .randomUUIDv4()
190 .pipe(Effect.mapError((cause) => new ServerOwnershipError({ stateDir, cause })));
191 yield* Effect.acquireRelease(
192 Effect.try({
193 try: () => {
194 NodeFS.mkdirSync(stateDir, { recursive: true });
195 const db = new NodeSqlite.DatabaseSync(NodePath.join(stateDir, "server-owner.sqlite"));
196 try {
197 db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;");
198 } catch (cause) {
199 db.close();
200 throw cause;
201 }
202 return db;
203 },
204 catch: (cause) =>
205 isSqliteBusy(cause)
206 ? new ServerAlreadyRunningError({ stateDir })
207 : new ServerOwnershipError({ stateDir, cause }),
208 }),
209 (db) =>
210 readPersistedServerRuntimeState(statePath).pipe(
211 Effect.flatMap((state) =>
212 Option.isSome(state) && state.value.ownerId === ownerId
213 ? clearPersistedServerRuntimeState(statePath)
214 : Effect.void,
215 ),
216 Effect.ignore({ log: true }),
217 Effect.ensuring(Effect.sync(() => db.close())),
218 ),
219 );
220
221 const previous = yield* readPersistedServerRuntimeState(statePath).pipe(
222 Effect.orElseSucceed(() => Option.none<PersistedServerRuntimeState>()),
223 );
224 if (
225 Option.isSome(previous) &&
226 previous.value.ownerId === undefined &&
227 (yield* legacyOwnerIsLive(previous.value))
228 ) {
229 return yield* new ServerAlreadyRunningError({ stateDir });
230 }
231 const olderServiceHome = yield* olderBootServiceHome(stateDir);
232 if (olderServiceHome !== undefined) {
233 return yield* new OlderBootServiceError({ baseDir: olderServiceHome });
234 }
235
236 return {
237 publish: (state: PersistedServerRuntimeState) =>
238 persistServerRuntimeState({ path: statePath, state: { ...state, ownerId } }),
239 };
240});