From 92496f6d64cf660a43e32a1ed1b74780f69901d9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 06:17:48 +0000 Subject: [PATCH 1/3] test(server): the trial hears committed before the database backup is discarded Reproduces #42653: the launcher deletes and fsyncs the pre-update backup before replying committed, while the trial waits only 30 seconds. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/serviceLauncher.test.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index 86df5b092ac8..e06a9244da1c 100644 --- a/apps/server/src/serviceLauncher.test.ts +++ b/apps/server/src/serviceLauncher.test.ts @@ -1210,6 +1210,7 @@ process.exit(0); const statePath = path.join(root, "runtime", "service-state.json"); const databasePath = path.join(root, "userdata", "state.sqlite"); const activatedPath = path.join(root, "activated"); + const backupAtCommitPath = path.join(root, "backup-at-commit"); yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); yield* fs.writeFileString(databasePath, "before trial"); const unit = yield* fakeServiceUnit(fs, path, root, "1.0.0", render); @@ -1235,6 +1236,10 @@ if (context.update?.status === "pending") { assert.equal(state.activeVersion, "1.1.0"); assert.equal(state.update.status, "committed"); writeFileSync(${JSON.stringify(activatedPath)}, message.updateId); + writeFileSync( + ${JSON.stringify(backupAtCommitPath)}, + String(existsSync(${JSON.stringify(path.join(root, "runtime", "db-backup"))} + "/" + message.updateId)), + ); process.exit(0); } }); @@ -1293,9 +1298,17 @@ if (context.update?.status === "pending") { yield* Effect.promise(() => fresh.stop("SIGTERM")); yield* Effect.promise(() => running); }); - return { fs, root, unit, recover }; + return { fs, root, unit, recover, backupAtCommitPath }; }); + // The trial waits only 30 seconds for "committed"; backup cleanup can take minutes on a full disk. + it.effect("tells the trial it is committed before discarding the database backup", () => + Effect.gen(function* () { + const { fs, backupAtCommitPath } = yield* commitUpdate(); + assert.equal(yield* fs.readFileString(backupAtCommitPath), "true"); + }), + ); + it.effect.each([ { name: "systemd after a protocol-3 local upgrade", From e75845e065e951fbf935866a2cb106060e6cb12d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 06:26:41 +0000 Subject: [PATCH 2/3] fix(server): launcher confirms an update before discarding the database backup The trial waits 30 seconds for "committed". The launcher removed the pre-update database backup (rm -rf plus a directory fsync) before replying, so a slow cleanup made the trial give up and the committed version start a second time. Reply first, then discard the backup. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/serviceLauncher.test.ts | 36 ++++++++++++++++++++++++- apps/server/src/serviceLauncher.ts | 2 +- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index e06a9244da1c..016bb4eeaec9 100644 --- a/apps/server/src/serviceLauncher.test.ts +++ b/apps/server/src/serviceLauncher.test.ts @@ -52,13 +52,18 @@ const spawnHook = vi.hoisted(() => ({ })); const failRm = vi.hoisted(() => ({ path: undefined as string | undefined })); +const holdRm = vi.hoisted(() => ({ + current: undefined as ((path: unknown) => Promise) | undefined, +})); vi.mock("node:fs/promises", async (importOriginal) => { const actual = await importOriginal(); const rm: typeof actual.rm = (path, options) => path === failRm.path ? Promise.reject(Object.assign(new Error("EACCES"), { code: "EACCES" })) - : actual.rm(path, options); + : holdRm.current === undefined + ? actual.rm(path, options) + : holdRm.current(path).then(() => actual.rm(path, options)); return { ...actual, rm }; }); @@ -405,6 +410,9 @@ setInterval(() => {}, 1_000); }[scenario], ); assert.equal(yield* fs.readFileString(databasePath), "before trial"); + // The child can answer "committed" before the launcher discards the backup; let it finish. + yield* Effect.promise(() => launcher.stop("SIGTERM")); + yield* Effect.promise(() => running); assert.isFalse(yield* fs.exists(backupPath)); }), ); @@ -1304,6 +1312,32 @@ if (context.update?.status === "pending") { // The trial waits only 30 seconds for "committed"; backup cleanup can take minutes on a full disk. it.effect("tells the trial it is committed before discarding the database backup", () => Effect.gen(function* () { + // Once "committed" is sent, hold the backup removal until the trial has read it and exited. + let committedSent = false; + const trialExited = Promise.withResolvers(); + yield* Effect.acquireRelease( + Effect.sync(() => { + spawnHook.current = (child) => { + child.send = new Proxy(child.send, { + apply(send, self, args: Parameters) { + if ((args[0] as { type?: unknown } | null)?.type === "committed") { + committedSent = true; + child.once("exit", () => trialExited.resolve()); + } + return Reflect.apply(send, self, args); + }, + }); + }; + holdRm.current = async (target) => { + if (committedSent && String(target).includes("db-backup")) await trialExited.promise; + }; + }), + () => + Effect.sync(() => { + spawnHook.current = undefined; + holdRm.current = undefined; + }), + ); const { fs, backupAtCommitPath } = yield* commitUpdate(); assert.equal(yield* fs.readFileString(backupAtCommitPath), "true"); }), diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.ts index 61e52b19d63f..cb363ef48d35 100644 --- a/apps/server/src/serviceLauncher.ts +++ b/apps/server/src/serviceLauncher.ts @@ -1392,10 +1392,10 @@ export class Launcher { }; await this.#selectState(next); child.role = "active"; - await discardDatabaseBackup(this.#baseDir, committed.id).catch(() => undefined); 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 ca08db0c63f910408f61b7383ca8c98240e5376c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 06:36:08 +0000 Subject: [PATCH 3/3] fix(server): a trial accepts its durable commit when the launcher replies late A remote update keeps the running launcher, so an older one can still finish backup cleanup before it replies "committed". After the 30-second wait, the trial now reads service-state.json and continues when it holds this update as committed instead of exiting and restarting. Co-Authored-By: Claude Opus 5.5 --- .../src/cloud/serviceLauncherClient.test.ts | 60 +++++++++++++++++++ .../server/src/cloud/serviceLauncherClient.ts | 48 +++++++++------ docs/internals/07_server-updates.md | 6 +- 3 files changed, 95 insertions(+), 19 deletions(-) diff --git a/apps/server/src/cloud/serviceLauncherClient.test.ts b/apps/server/src/cloud/serviceLauncherClient.test.ts index 1a98b7b2703f..970eb14f857d 100644 --- a/apps/server/src/cloud/serviceLauncherClient.test.ts +++ b/apps/server/src/cloud/serviceLauncherClient.test.ts @@ -7,7 +7,9 @@ import { assert, expect, it } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; import * as Deferred from "effect/Deferred"; +import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; +import * as TestClock from "effect/testing/TestClock"; import { SERVICE_LAUNCHER_CONTEXT_ENV, @@ -148,6 +150,64 @@ it.effect.each([3, SERVICE_LAUNCHER_PROTOCOL])( }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); +// regression: #42653 an older launcher can commit durably but reply after the 30-second wait +it.effect.each([ + { status: "committed" as const, continues: true }, + { status: "pending" as const, continues: false }, +])( + "after an unanswered prepared, a $status service state decides the trial", + ({ status, continues }) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-trial-timeout-" }); + const baseDir = path.join(root, ".t3"); + const pending = { + id: "update-1", + fromVersion: "1.0.0", + targetVersion: "1.1.0", + dbPath: "/tmp/state.sqlite", + status: "pending" as const, + }; + const committed = { + id: pending.id, + fromVersion: pending.fromVersion, + targetVersion: pending.targetVersion, + status: "committed" as const, + }; + yield* fs.makeDirectory(path.join(baseDir, "runtime"), { recursive: true }); + yield* fs.writeFileString( + path.join(baseDir, "runtime", "service-state.json"), + JSON.stringify( + status === "committed" + ? { protocol: SERVICE_LAUNCHER_PROTOCOL, activeVersion: "1.1.0", update: committed } + : { protocol: SERVICE_LAUNCHER_PROTOCOL, activeVersion: "1.0.0", update: pending }, + ), + ); + const host = new FakeLauncherProcess({ + protocol: SERVICE_LAUNCHER_PROTOCOL, + childVersion: "1.1.0", + update: pending, + }); + host.env["HOME"] = root; + const client = yield* makeClient(host, "1.1.0").pipe( + Effect.provideService(HostProcess.Platform, "darwin"), + ); + const prepared = yield* Effect.forkChild(Effect.exit(client.prepareTrial({ baseDir })), { + startImmediately: true, + }); + yield* TestClock.adjust("30 seconds"); + const exit = yield* Fiber.join(prepared); + if (continues) { + expect(exit).toEqual(Exit.succeed(committed)); + } else { + expect(exit).toEqual( + Exit.fail(new ServiceLauncherClient.ServiceLauncherClientError({ operation: "timeout" })), + ); + } + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + it.effect("returns the launcher-generated ID only after update acceptance", () => Effect.gen(function* () { const host = new FakeLauncherProcess({ diff --git a/apps/server/src/cloud/serviceLauncherClient.ts b/apps/server/src/cloud/serviceLauncherClient.ts index 50b596b0e30d..1a2d301163fb 100644 --- a/apps/server/src/cloud/serviceLauncherClient.ts +++ b/apps/server/src/cloud/serviceLauncherClient.ts @@ -21,7 +21,9 @@ import { import { decodeServiceLauncherContext, decodeServiceLauncherParentMessage, + parseServiceState, SERVICE_LAUNCHER_CONTEXT_ENV, + SERVICE_STATE_FILE, type ServiceLauncherChildMessage, type ServiceLauncherParentMessage, type SupportedLauncherProtocol, @@ -368,24 +370,36 @@ export const make = Effect.fn("cloud.service_launcher_client.make")(function* (o { type: "prepared", updateId: pending.id }, (reply) => reply.type === "committed" && reply.updateId === pending.id, ).pipe( - Effect.flatMap((reply) => - reply.type !== "committed" - ? Effect.die("service launcher returned an impossible prepared response") - : repointLaunchers({ - baseDir: input.baseDir, - targetVersion: pending.targetVersion, - }).pipe( - Effect.provideService(FileSystem.FileSystem, fs), - Effect.provideService(Path.Path, path), - Effect.provideService(HostProcess.Environment, environment), - Effect.provideService(HostProcess.Platform, platform), - Effect.as({ - id: pending.id, - fromVersion: pending.fromVersion, - targetVersion: pending.targetVersion, - status: "committed" as const, - }), + // An older launcher may finish slow cleanup before it replies; its durable commit is the decision. + Effect.catchIf( + (error) => error.operation === "timeout", + (error) => + fs.readFileString(path.join(input.baseDir, "runtime", SERVICE_STATE_FILE)).pipe( + Effect.map(parseServiceState), + Effect.orElseSucceed(() => undefined), + Effect.flatMap((state) => + state?.update?.id === pending.id && state.update.status === "committed" + ? Effect.void + : Effect.fail(error), ), + ), + ), + Effect.andThen( + repointLaunchers({ + baseDir: input.baseDir, + targetVersion: pending.targetVersion, + }).pipe( + Effect.provideService(FileSystem.FileSystem, fs), + Effect.provideService(Path.Path, path), + Effect.provideService(HostProcess.Environment, environment), + Effect.provideService(HostProcess.Platform, platform), + Effect.as({ + id: pending.id, + fromVersion: pending.fromVersion, + targetVersion: pending.targetVersion, + status: "committed" as const, + }), + ), ), ), ) diff --git a/docs/internals/07_server-updates.md b/docs/internals/07_server-updates.md index 5e181607dd50..fa10bdbafaca 100644 --- a/docs/internals/07_server-updates.md +++ b/docs/internals/07_server-updates.md @@ -24,8 +24,10 @@ restarts never depend on npm cache eviction or a moving tag. 3. Start the new runtime as a trial. It must finish migrations, acquire fallible dependencies, bind HTTP, and hold long-running roots at the activation gate before reporting `prepared`. 4. Only after the launcher commits the new version durably and replies `committed` may the child - release activation gates and accept work. A failed or timed-out trial stops and restores the - snapshot before booting the old version. + release activation gates and accept work. The launcher replies before slow cleanup, but a remote + update keeps the old launcher, so a trial whose wait times out also accepts this update's commit + from the state file. A failed or timed-out trial stops and restores the snapshot before booting + the old version. ## Client acknowledgement