From 27c008d6f944b5aa92552109b173270949198e52 Mon Sep 17 00:00:00 2001 From: 2SinceJuly <202121119+2SinceJuly@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:32:31 +0800 Subject: [PATCH 1/3] fix(server): confirm a committed update before dropping its backup The trial gives the launcher 30 seconds to answer "prepared". The launcher wrote the commit to disk, then dropped the pre-update database backup and flushed the folder before replying. On machines where that cleanup takes longer than 30 seconds the trial gave up, exited, and the launcher started the new version a second time, extending every self-update outage. The commit is already durable when the state is written, so reply first and let the cleanup run after it. A trial that exits after its reply still finds a committed update and is not rolled back. --- apps/server/src/serviceLauncher.test.ts | 107 +++++++++++++++++++++++- apps/server/src/serviceLauncher.ts | 4 +- 2 files changed, 108 insertions(+), 3 deletions(-) diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index 86df5b092ac8..bd44f46190c6 100644 --- a/apps/server/src/serviceLauncher.test.ts +++ b/apps/server/src/serviceLauncher.test.ts @@ -53,12 +53,23 @@ const spawnHook = vi.hoisted(() => ({ const failRm = vi.hoisted(() => ({ path: undefined as string | undefined })); +const slowRm = vi.hoisted(() => ({ + path: undefined as string | undefined, + started: undefined as (() => void) | undefined, + wait: undefined as Promise | undefined, +})); + vi.mock("node:fs/promises", async (importOriginal) => { const actual = await importOriginal(); - const rm: typeof actual.rm = (path, options) => - path === failRm.path + const rm: typeof actual.rm = async (path, options) => { + if (path === slowRm.path) { + slowRm.started?.(); + await slowRm.wait; + } + return path === failRm.path ? Promise.reject(Object.assign(new Error("EACCES"), { code: "EACCES" })) : actual.rm(path, options); + }; return { ...actual, rm }; }); @@ -1107,6 +1118,98 @@ process.exit(0); }), ); + it.effect("confirms a committed update before dropping its backup", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-slow-discard-" }); + const statePath = path.join(root, "runtime", "service-state.json"); + const databasePath = path.join(root, "userdata", "state.sqlite"); + const backupPath = path.join(root, "runtime", "db-backup", "update-1"); + yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); + yield* fs.writeFileString(databasePath, "before trial"); + // The trial gives the launcher 30 seconds to answer "prepared". A backup drop that + // takes longer used to sit in front of that reply, so the trial exited and the + // launcher started the new version a second time. + const childSource = ` +import { existsSync } from "node:fs"; +const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); +let answered = false; +process.on("message", (message) => { + if (message.type !== "committed" || answered) return; + answered = true; + process.send({ type: "confirms", backupPresent: existsSync(${JSON.stringify(backupPath)}) }); +}); +setTimeout(() => { + if (!answered) process.send({ type: "confirms", backupPresent: null }); +}, 5_000); +process.send({ type: "prepared", updateId: context.update.id }); +setInterval(() => {}, 1_000); +`; + for (const version of ["1.0.0", "1.1.0"]) { + yield* writeFakeRuntime( + fs, + path, + path.join(root, "runtime", "versions", version), + childSource, + ); + } + yield* fs.makeDirectory(path.join(backupPath, "database"), { recursive: true }); + yield* fs.writeFileString(path.join(backupPath, "database", "state.sqlite"), "before trial"); + yield* Effect.promise(() => + writeServiceState(statePath, { + protocol: SERVICE_LAUNCHER_PROTOCOL, + activeVersion: "1.0.0", + update: { + id: "update-1", + fromVersion: "1.0.0", + targetVersion: "1.1.0", + dbPath: databasePath, + status: "pending", + }, + }), + ); + const discardStarted = Promise.withResolvers(); + const releaseDiscard = Promise.withResolvers(); + const confirmed = Promise.withResolvers(); + yield* Effect.acquireRelease( + Effect.sync(() => { + slowRm.path = backupPath; + slowRm.started = () => discardStarted.resolve(); + slowRm.wait = releaseDiscard.promise; + spawnHook.current = (child) => + child.on("message", (message) => { + const confirms = message as { type?: unknown; backupPresent?: boolean | null }; + if (confirms.type === "confirms") confirmed.resolve(confirms.backupPresent ?? null); + }); + }), + () => + Effect.sync(() => { + releaseDiscard.resolve(); + slowRm.path = undefined; + slowRm.started = undefined; + slowRm.wait = undefined; + spawnHook.current = undefined; + }), + ); + const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath))); + const running = launcher.run(); + yield* Effect.addFinalizer(() => + Effect.promise(async () => { + releaseDiscard.resolve(); + await launcher.stop("SIGTERM"); + await running; + }), + ); + yield* Effect.promise(() => discardStarted.promise); + assert.isTrue(yield* Effect.promise(() => confirmed.promise)); + releaseDiscard.resolve(); + yield* Effect.promise(() => launcher.stop("SIGTERM")); + yield* Effect.promise(() => running); + assert.isFalse(yield* fs.exists(backupPath)); + }), + ); + it.effect("fails a pending update when its backup cannot be dropped while owned", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.ts index 61e52b19d63f..e4790ceeddeb 100644 --- a/apps/server/src/serviceLauncher.ts +++ b/apps/server/src/serviceLauncher.ts @@ -1392,10 +1392,12 @@ export class Launcher { }; await this.#selectState(next); child.role = "active"; - await discardDatabaseBackup(this.#baseDir, committed.id).catch(() => undefined); + // The commit is already durable, so answer before the cleanup: the trial waits only + // 30 seconds for this reply, and dropping the backup can take longer than that. try { await sendMessage(child.process, { type: "committed", updateId: committed.id }); } finally { + await discardDatabaseBackup(this.#baseDir, committed.id).catch(() => undefined); await this.#repointServiceUnit(committed.targetVersion); } } From 245e6bd20eb20be429c4b91646446982a9b2a6d2 Mon Sep 17 00:00:00 2001 From: 2SinceJuly <202121119+2SinceJuly@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:51:52 +0800 Subject: [PATCH 2/3] test(server): drain the launcher before the committed-update checks The backup drop now follows the "committed" reply, so the assertions about it can run before the queued cleanup. Stopping the launcher first drains that queue. --- apps/server/src/serviceLauncher.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index bd44f46190c6..e4ebaf577e78 100644 --- a/apps/server/src/serviceLauncher.test.ts +++ b/apps/server/src/serviceLauncher.test.ts @@ -213,6 +213,9 @@ const fakeServiceUnit = ( return unit; }); +// Stopping drains the launcher's queue, including work that follows the "committed" reply. +const drain = (launcher: Launcher) => launcher.stop("SIGTERM"); + it.layer(NodeServices.layer)("service state persistence", (it) => { it.effect.each([ "startup", @@ -397,6 +400,8 @@ setInterval(() => {}, 1_000); recovered = true; yield* Effect.promise(() => vi.advanceTimersByTimeAsync(30_000)); yield* Effect.promise(() => Promise.race([ready.promise, running])); + // The backup drop now follows the "committed" reply, so it lands after the child acts. + yield* Effect.promise(() => drain(launcher)); const state = yield* Effect.promise(() => readServiceState(statePath)); assert.equal( state.activeVersion, From 2ebff9898f6fd349c71b84954a771baf9ca6ed8a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 07:00:39 +0000 Subject: [PATCH 3/3] test(server): inline the launcher drain in the startup checks Co-Authored-By: Claude Opus 5.5 --- apps/server/src/serviceLauncher.test.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index e4ebaf577e78..c11603e6fc93 100644 --- a/apps/server/src/serviceLauncher.test.ts +++ b/apps/server/src/serviceLauncher.test.ts @@ -213,9 +213,6 @@ const fakeServiceUnit = ( return unit; }); -// Stopping drains the launcher's queue, including work that follows the "committed" reply. -const drain = (launcher: Launcher) => launcher.stop("SIGTERM"); - it.layer(NodeServices.layer)("service state persistence", (it) => { it.effect.each([ "startup", @@ -400,8 +397,8 @@ setInterval(() => {}, 1_000); recovered = true; yield* Effect.promise(() => vi.advanceTimersByTimeAsync(30_000)); yield* Effect.promise(() => Promise.race([ready.promise, running])); - // The backup drop now follows the "committed" reply, so it lands after the child acts. - yield* Effect.promise(() => drain(launcher)); + // Stopping drains the queue, including the backup drop that follows the "committed" reply. + yield* Effect.promise(() => launcher.stop("SIGTERM")); const state = yield* Effect.promise(() => readServiceState(statePath)); assert.equal( state.activeVersion,