diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index 86df5b092ac8..c11603e6fc93 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 }; }); @@ -386,6 +397,8 @@ setInterval(() => {}, 1_000); recovered = true; yield* Effect.promise(() => vi.advanceTimersByTimeAsync(30_000)); yield* Effect.promise(() => Promise.race([ready.promise, running])); + // 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, @@ -1107,6 +1120,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); } }