Skip to content

Commit 80365cc

Browse files
committed
fix(server): a pending update keeps its backup and resumes once the T3 home frees
1 parent 7df3b97 commit 80365cc

2 files changed

Lines changed: 96 additions & 13 deletions

File tree

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,97 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
319319
}),
320320
);
321321

322+
it.effect("keeps a pending update and its backup while owned, then resumes the trial", () =>
323+
Effect.gen(function* () {
324+
const fs = yield* FileSystem.FileSystem;
325+
const path = yield* Path.Path;
326+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-reclaim-update-" });
327+
const statePath = path.join(root, "runtime", "service-state.json");
328+
const ownerPath = path.join(root, "userdata", "server-owner.sqlite");
329+
const databasePath = path.join(root, "userdata", "state.sqlite");
330+
const startsPath = path.join(root, "starts.log");
331+
yield* fs.makeDirectory(path.dirname(ownerPath), { recursive: true });
332+
yield* fs.writeFileString(databasePath, "before update");
333+
for (const version of ["1.0.0", "1.1.0"]) {
334+
yield* writeFakeRuntime(
335+
fs,
336+
path,
337+
path.join(root, "runtime", "versions", version),
338+
[
339+
`const db = new (require("node:sqlite").DatabaseSync)(${JSON.stringify(ownerPath)});`,
340+
`try { db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;"); }`,
341+
`catch { process.exit(${SERVER_EXIT_CODE_STATE_DIR_OWNED}); }`,
342+
"const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);",
343+
`require("node:fs").appendFileSync(${JSON.stringify(startsPath)}, context.childVersion + "\\n");`,
344+
'process.on("message", (message) => message.type === "committed" && process.send("committed"));',
345+
'if (context.update?.status === "pending") process.send({ type: "prepared", updateId: context.update.id });',
346+
"setInterval(() => {}, 1_000);",
347+
].join("\n"),
348+
);
349+
}
350+
yield* Effect.promise(() =>
351+
writeServiceState(statePath, {
352+
protocol: SERVICE_LAUNCHER_PROTOCOL,
353+
activeVersion: "1.0.0",
354+
update: {
355+
id: "update-1",
356+
fromVersion: "1.0.0",
357+
targetVersion: "1.1.0",
358+
dbPath: databasePath,
359+
status: "pending",
360+
},
361+
}),
362+
);
363+
using otherOwner = new NodeSqlite.DatabaseSync(ownerPath);
364+
otherOwner.exec("BEGIN EXCLUSIVE;");
365+
const refused = Promise.withResolvers<void>();
366+
const committed = Promise.withResolvers<void>();
367+
yield* Effect.acquireRelease(
368+
Effect.sync(() =>
369+
vi.spyOn(process.stderr, "write").mockImplementation((chunk) => {
370+
if (String(chunk).includes("owns this T3 home")) refused.resolve();
371+
return true;
372+
}),
373+
),
374+
(spy) => Effect.sync(() => spy.mockRestore()),
375+
);
376+
yield* Effect.acquireRelease(
377+
Effect.sync(() => {
378+
spawnHook.current = (child) =>
379+
child.on("message", (message) => message === "committed" && committed.resolve());
380+
}),
381+
() =>
382+
Effect.sync(() => {
383+
spawnHook.current = undefined;
384+
}),
385+
);
386+
const launcher = new Launcher(
387+
root,
388+
yield* Effect.promise(() => readServiceState(statePath)),
389+
{ stateDirOwnerPollMs: 10 },
390+
);
391+
const running = launcher.run();
392+
yield* Effect.promise(() => refused.promise);
393+
const idle = yield* Effect.promise(() => readServiceState(statePath));
394+
assert.strictEqual(idle.activeVersion, "1.0.0");
395+
assert.strictEqual(idle.update?.status, "pending");
396+
assert.isTrue(yield* fs.exists(path.join(root, "runtime", "db-backup", "update-1")));
397+
398+
// The other owner runs the target version and migrates before it releases the home.
399+
yield* fs.writeFileString(databasePath, "migrated by 1.1.0");
400+
otherOwner.close();
401+
yield* Effect.promise(() => committed.promise);
402+
yield* Effect.promise(() => launcher.stop("SIGTERM"));
403+
yield* Effect.promise(() => running);
404+
405+
const state = yield* Effect.promise(() => readServiceState(statePath));
406+
assert.strictEqual(state.activeVersion, "1.1.0");
407+
assert.strictEqual(state.update?.status, "committed");
408+
assert.strictEqual(yield* fs.readFileString(startsPath), "1.1.0\n");
409+
assert.strictEqual(yield* fs.readFileString(databasePath), "migrated by 1.1.0");
410+
}),
411+
);
412+
322413
it.effect("treats the T3 home as owned while the lock or a pre-lock server holds it", () =>
323414
Effect.gen(function* () {
324415
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/serviceLauncher.ts‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -847,20 +847,10 @@ export class Launcher {
847847
throw new Error(`Active child exited unexpectedly (${String(code ?? signal ?? "unknown")}).`);
848848
}
849849

850-
// Stay idle until the lock frees to avoid supervisor restart loops; never restore backups over another owner's possible writes.
850+
// Stay idle until the lock frees to avoid supervisor restart loops. A pending update keeps its
851+
// backup and resumes its trial, since the other owner may have migrated the database past fromVersion.
851852
async #idleWhileStateDirOwned(): Promise<void> {
852853
this.#clearTimer();
853-
const pending = this.#state.update;
854-
if (pending?.status === "pending") {
855-
const next: ServiceState = {
856-
...this.#state,
857-
activeVersion: pending.fromVersion,
858-
update: terminalUpdate({ pending, status: "failed", reason: "state-dir-owned" }),
859-
};
860-
await writeServiceState(this.#statePath, next);
861-
this.#state = next;
862-
await discardDatabaseBackup(this.#baseDir, pending.id).catch(() => undefined);
863-
}
864854
process.stderr.write(
865855
"[service-launcher] Another T3 Code server owns this T3 home. This service starts again once it stops.\n",
866856
);
@@ -881,7 +871,9 @@ export class Launcher {
881871
if ((await stateDirOwned(this.#baseDir)) || this.#timer === undefined) return;
882872
this.#clearTimer();
883873
process.stderr.write("[service-launcher] The T3 home is free again. Starting the server.\n");
884-
await this.#startChild(this.#state.activeVersion, "active", this.#state.update);
874+
const update = this.#state.update;
875+
if (update?.status === "pending") await this.#startTrial(update);
876+
else await this.#startChild(this.#state.activeVersion, "active", update);
885877
}
886878

887879
async #returnToPrevious(

0 commit comments

Comments
 (0)