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 SecureRandom from "effect/crypto/SecureRandom";
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. server-owner.sqlite is the ownership lock; do not delete it while a server is running.`;
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 random = yield* SecureRandom.SecureRandom;
188 const ownerId = yield* random
189 .uuidV4()
190 .pipe(Effect.mapError((cause) => new ServerOwnershipError({ stateDir, cause })));
191 const lockPath = NodePath.join(stateDir, "server-owner.sqlite");
192 const lock = yield* Effect.acquireRelease(
193 Effect.try({
194 try: () => {
195 NodeFS.mkdirSync(stateDir, { recursive: true });
196 let before = NodeFS.statSync(lockPath, { bigint: true, throwIfNoEntry: false });
197 if (before === undefined) {
198 NodeFS.closeSync(NodeFS.openSync(lockPath, "a", 0o644));
199 before = NodeFS.statSync(lockPath, { bigint: true });
200 }
201 const db = new NodeSqlite.DatabaseSync(lockPath);
202 try {
203 db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;");
204 const identity = NodeFS.statSync(lockPath, { bigint: true });
205 return {
206 db,
207 identity,
208 replacedDuringAcquire: before.dev !== identity.dev || before.ino !== identity.ino,
209 };
210 } catch (cause) {
211 db.close();
212 throw cause;
213 }
214 },
215 catch: (cause) =>
216 isSqliteBusy(cause)
217 ? new ServerAlreadyRunningError({ stateDir })
218 : new ServerOwnershipError({ stateDir, cause }),
219 }),
220 ({ db }) =>
221 readPersistedServerRuntimeState(statePath).pipe(
222 Effect.flatMap((state) =>
223 Option.isSome(state) && state.value.ownerId === ownerId
224 ? clearPersistedServerRuntimeState(statePath)
225 : Effect.void,
226 ),
227 Effect.ignore({ log: true }),
228 Effect.ensuring(Effect.sync(() => db.close())),
229 ),
230 );
231
232 const previous = yield* readPersistedServerRuntimeState(statePath).pipe(
233 Effect.orElseSucceed(() => Option.none<PersistedServerRuntimeState>()),
234 );
235 if (
236 Option.isSome(previous) &&
237 previous.value.ownerId === undefined &&
238 (yield* legacyOwnerIsLive(previous.value))
239 ) {
240 return yield* new ServerAlreadyRunningError({ stateDir });
241 }
242 const olderServiceHome = yield* olderBootServiceHome(stateDir);
243 if (olderServiceHome !== undefined) {
244 return yield* new OlderBootServiceError({ baseDir: olderServiceHome });
245 }
246
247 yield* Effect.gen(function* () {
248 let lost = lock.replacedDuringAcquire;
249 let statWarningLogged = false;
250 while (!lost) {
251 yield* Effect.sleep("5 seconds");
252 // Closing another descriptor for this inode releases SQLite's POSIX locks. Stat by path only.
253 const current = yield* Effect.sync(() => {
254 try {
255 return NodeFS.statSync(lockPath, { bigint: true, throwIfNoEntry: false });
256 } catch (cause) {
257 return cause instanceof Error && "code" in cause ? String(cause.code) : "unknown";
258 }
259 });
260 if (typeof current === "string") {
261 if (!statWarningLogged) {
262 yield* Effect.logWarning(
263 `Cannot check server ownership lock at ${lockPath} (${current}).`,
264 );
265 statWarningLogged = true;
266 }
267 continue;
268 }
269 statWarningLogged = false;
270 lost =
271 current === undefined ||
272 current.dev !== lock.identity.dev ||
273 current.ino !== lock.identity.ino;
274 }
275 yield* Effect.logError(
276 `Server ownership lock at ${lockPath} was deleted or replaced. Another server may now share this T3 home. Stop any other server using this home, then restart this server. Do not delete server-owner.sqlite while a server is running.`,
277 );
278 }).pipe(Effect.forkScoped);
279
280 return {
281 publish: (state: PersistedServerRuntimeState) =>
282 persistServerRuntimeState({ path: statePath, state: { ...state, ownerId } }),
283 };
284});