Skip to content

Commit e444059

Browse files
PR Batch Testerclaude
andcommitted
fix(server): keep waiting while a pre-lock server owns the T3 home
A free owner lock doesn't mean the server will start: it also refuses a live server-runtime.json record without an ownerId. The launcher now checks that record too, so it doesn't start a refused server every poll. The recovery test now waits for the new child to hold the lock. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dfa6c7a commit e444059

2 files changed

Lines changed: 73 additions & 14 deletions

File tree

‎apps/server/src/serviceLauncher.test.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
readServiceEnvironment,
1818
type BackupSpaceProbe,
1919
readServiceState,
20+
stateDirOwned,
2021
writeServiceState,
2122
} from "./serviceLauncher.ts";
2223
import {
@@ -29,12 +30,16 @@ import {
2930
} from "./cloud/serviceProtocol.ts";
3031

3132
const spawnedChildren = vi.hoisted((): Array<NodeChildProcess.ChildProcess> => []);
33+
const spawnHook = vi.hoisted(() => ({
34+
current: undefined as ((child: NodeChildProcess.ChildProcess) => void) | undefined,
35+
}));
3236

3337
vi.mock("node:child_process", async (importOriginal) => {
3438
const actual = await importOriginal<typeof NodeChildProcess>();
3539
const spawn = (...args: Parameters<typeof actual.spawn>) => {
3640
const child = actual.spawn(...args);
3741
spawnedChildren.push(child);
42+
spawnHook.current?.(child);
3843
return child;
3944
};
4045
return { ...actual, spawn: spawn as typeof actual.spawn };
@@ -270,6 +275,7 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
270275
`const db = new (require("node:sqlite").DatabaseSync)(${JSON.stringify(ownerPath)});`,
271276
`try { db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;"); }`,
272277
`catch { process.exit(${SERVER_EXIT_CODE_STATE_DIR_OWNED}); }`,
278+
'process.send("owner");',
273279
"setInterval(() => {}, 1_000);",
274280
].join("\n"),
275281
);
@@ -282,17 +288,25 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
282288
const otherOwner = new NodeSqlite.DatabaseSync(ownerPath);
283289
otherOwner.exec("BEGIN EXCLUSIVE;");
284290
const refused = Promise.withResolvers<void>();
285-
const reclaimed = Promise.withResolvers<void>();
291+
const owned = Promise.withResolvers<void>();
286292
yield* Effect.acquireRelease(
287293
Effect.sync(() =>
288294
vi.spyOn(process.stderr, "write").mockImplementation((chunk) => {
289295
if (String(chunk).includes("owns this T3 home")) refused.resolve();
290-
if (String(chunk).includes("T3 home is free again")) reclaimed.resolve();
291296
return true;
292297
}),
293298
),
294299
(spy) => Effect.sync(() => spy.mockRestore()),
295300
);
301+
yield* Effect.acquireRelease(
302+
Effect.sync(() => {
303+
spawnHook.current = (child) => child.once("message", () => owned.resolve());
304+
}),
305+
() =>
306+
Effect.sync(() => {
307+
spawnHook.current = undefined;
308+
}),
309+
);
296310
const spawnsBefore = spawnedChildren.length;
297311
const launcher = new Launcher(
298312
root,
@@ -304,13 +318,38 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
304318
const running = launcher.run();
305319
yield* Effect.promise(() => refused.promise);
306320
otherOwner.close();
307-
yield* Effect.promise(() => reclaimed.promise);
321+
yield* Effect.promise(() => owned.promise);
308322
yield* Effect.promise(() => launcher.stop("SIGTERM"));
309323
yield* Effect.promise(() => running);
310324
assert.strictEqual(spawnedChildren.length - spawnsBefore, 2);
311325
}),
312326
);
313327

328+
it.effect("treats the T3 home as owned while the lock or a pre-lock server holds it", () =>
329+
Effect.gen(function* () {
330+
const fs = yield* FileSystem.FileSystem;
331+
const path = yield* Path.Path;
332+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-owned-" });
333+
const stateDir = path.join(root, "userdata");
334+
const recordPath = path.join(stateDir, "server-runtime.json");
335+
yield* fs.makeDirectory(stateDir, { recursive: true });
336+
assert.isFalse(stateDirOwned(root));
337+
338+
const owner = new NodeSqlite.DatabaseSync(path.join(stateDir, "server-owner.sqlite"));
339+
owner.exec("BEGIN EXCLUSIVE;");
340+
assert.isTrue(stateDirOwned(root));
341+
owner.close();
342+
343+
const record = { version: 1, pid: process.pid, port: 1, origin: "", startedAt: "" };
344+
yield* fs.writeFileString(recordPath, JSON.stringify(record));
345+
assert.isTrue(stateDirOwned(root));
346+
yield* fs.writeFileString(recordPath, JSON.stringify({ ...record, ownerId: "lock-aware" }));
347+
assert.isFalse(stateDirOwned(root));
348+
yield* fs.writeFileString(recordPath, JSON.stringify({ ...record, pid: 2 ** 22 + 1 }));
349+
assert.isFalse(stateDirOwned(root));
350+
}),
351+
);
352+
314353
it.effect("commits only after the trial reports prepared", () =>
315354
Effect.gen(function* () {
316355
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/serviceLauncher.ts‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -460,26 +460,46 @@ async function terminateChild(
460460
}
461461
}
462462

463+
const isSqliteBusy = (cause: unknown) =>
464+
cause instanceof Error &&
465+
(("errcode" in cause && cause.errcode === 5) ||
466+
("code" in cause && cause.code === "SQLITE_BUSY"));
467+
463468
/**
464-
* Takes and drops the same exclusive transaction the server holds for its
465-
* lifetime (see `acquireServerOwnership`). Only SQLITE_BUSY counts as owned:
466-
* any other failure is left for the server's own startup to report.
469+
* Mirrors the refusals in `acquireServerOwnership`: the lock, then a live
470+
* pre-lock server's `server-runtime.json`. That record's PID-reuse check is
471+
* skipped, so a reused PID keeps the launcher waiting rather than restarting
472+
* a server that would be refused. Any other failure is left for the server's
473+
* own startup to report.
467474
*/
468-
function stateDirOwned(baseDir: string): boolean {
475+
export function stateDirOwned(baseDir: string): boolean {
476+
const stateDir = NodePath.join(baseDir, "userdata");
469477
let db: NodeSqlite.DatabaseSync | undefined;
470478
try {
471-
db = new NodeSqlite.DatabaseSync(NodePath.join(baseDir, "userdata", "server-owner.sqlite"));
479+
db = new NodeSqlite.DatabaseSync(NodePath.join(stateDir, "server-owner.sqlite"));
472480
db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE; ROLLBACK;");
473-
return false;
474481
} catch (cause) {
475-
return (
476-
cause instanceof Error &&
477-
(("errcode" in cause && cause.errcode === 5) ||
478-
("code" in cause && cause.code === "SQLITE_BUSY"))
479-
);
482+
return isSqliteBusy(cause);
480483
} finally {
481484
db?.close();
482485
}
486+
let record: unknown;
487+
try {
488+
record = JSON.parse(
489+
NodeFS.readFileSync(NodePath.join(stateDir, "server-runtime.json"), "utf8"),
490+
);
491+
} catch {
492+
return false;
493+
}
494+
if (typeof record !== "object" || record === null || "ownerId" in record) return false;
495+
const pid = "pid" in record ? record.pid : undefined;
496+
if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 0) return false;
497+
try {
498+
process.kill(pid, 0);
499+
return true;
500+
} catch (error) {
501+
return error instanceof Error && "code" in error && error.code === "EPERM";
502+
}
483503
}
484504

485505
const stopMarkerPath = (baseDir: string) =>

0 commit comments

Comments
 (0)