@devries-cameron20 / t3code Mirror · read-only / commits / e41941779a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 17:15 UTC

fix(server): stopping the service during the pre-update backup ends the copy (#34854)

Julius Marminge committed Nov 13, 2026 at 08:45 UTC e41941779a6cebb6fc7048466607a394ce2871f5
2 files changed +73 -9
apps/server/src/serviceLauncher.test.ts
+50
@@ -1586,6 +1586,56 @@ process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: paths.dat
1586 }),
1587 );
1588
1589 + it.effect("starts no trial when stopped during the pre-update backup", () =>
1590 + Effect.gen(function* () {
1591 + const fs = yield* FileSystem.FileSystem;
1592 + const path = yield* Path.Path;
1593 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-backup-stop-" });
1594 + const statePath = path.join(root, "runtime", "service-state.json");
1595 + const childSource = "setInterval(() => {}, 1000);\n";
1596 + for (const version of ["1.0.0", "1.1.0"]) {
1597 + yield* writeFakeRuntime(
1598 + fs,
1599 + path,
1600 + path.join(root, "runtime", "versions", version),
1601 + childSource,
1602 + );
1603 + }
1604 + const dbPath = path.join(root, "userdata", "state.sqlite");
1605 + yield* fs.makeDirectory(path.dirname(dbPath), { recursive: true });
1606 + yield* fs.writeFileString(dbPath, "database");
1607 + const pending = {
1608 + protocol: SERVICE_LAUNCHER_PROTOCOL,
1609 + activeVersion: "1.0.0",
1610 + update: {
1611 + id: "update-1",
1612 + fromVersion: "1.0.0",
1613 + targetVersion: "1.1.0",
1614 + dbPath,
1615 + status: "pending",
1616 + },
1617 + } as const;
1618 + yield* Effect.promise(() => writeServiceState(statePath, pending));
1619 + const childCount = spawnedChildren.length;
1620 + const launcher = new Launcher(root, pending);
1621 +
1622 + // Recovery is already queued, so this stop lands while the backup runs.
1623 + const running = launcher.run();
1624 + yield* Effect.promise(() => launcher.stop("SIGTERM"));
1625 + yield* Effect.promise(() => running);
1626 +
1627 + // A trial started here arms its 30-minute prepared timer, which keeps the stopped launcher alive.
1628 + assert.deepEqual(
1629 + spawnedChildren.slice(childCount).map((child) => child.spawnargs.join(" ")),
1630 + [],
1631 + );
1632 + assert.deepEqual(yield* Effect.promise(() => readServiceState(statePath)), pending);
1633 + const backupDir = path.join(root, "runtime", "db-backup", "update-1");
1634 + assert.isFalse(yield* fs.exists(backupDir));
1635 + assert.isFalse(yield* fs.exists(`${backupDir}.staging`));
1636 + }),
1637 + );
1638 +
1639 it.effect("records no update when the server exits during the backup check", () =>
1640 Effect.gen(function* () {
1641 const fs = yield* FileSystem.FileSystem;
apps/server/src/serviceLauncher.ts
+23 -9
@@ -326,14 +326,18 @@ async function syncDirectory(directory: string): Promise<void> {
326 }
327 }
328
329 -function runCp(args: ReadonlyArray<string>): Promise<boolean> {
329 +function runCp(args: ReadonlyArray<string>, signal?: AbortSignal): Promise<boolean> {
330 return new Promise((resolve) => {
331 - NodeChildProcess.execFile("cp", args, (error) => resolve(error === null));
331 + NodeChildProcess.execFile("cp", args, { signal }, (error) => resolve(error === null));
332 });
333 }
334
335 // macOS fs.copyFile clone flags fall back to full copies.
336 -async function copyDatabaseFile(source: string, destination: string): Promise<void> {
336 +async function copyDatabaseFile(
337 + source: string,
338 + destination: string,
339 + signal?: AbortSignal,
340 +): Promise<void> {
341 // oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone launcher has no Effect runtime.
342 const platform = process.platform;
343 // cp -c replaces files and breaks hard links; copy into hard-linked destinations instead.
@@ -346,7 +350,9 @@ async function copyDatabaseFile(source: string, destination: string): Promise<vo
350 : platform === "linux"
351 ? ["--reflink=auto", source, destination]
352 : undefined;
349 - if (args === undefined || !(await runCp(args))) await NodeFSP.copyFile(source, destination);
353 + if (args !== undefined && (await runCp(args, signal))) return;
354 + signal?.throwIfAborted();
355 + await NodeFSP.copyFile(source, destination);
356 }
357
358 const GIB = 1024 ** 3;
@@ -525,7 +531,11 @@ async function startOnlineDatabaseBackup(
531 }
532
533 // Preserve completed backups; restarted trials may already have changed the live database.
528 -async function backupDatabaseOnce(baseDir: string, pending: PendingServiceUpdate): Promise<void> {
534 +async function backupDatabaseOnce(
535 + baseDir: string,
536 + pending: PendingServiceUpdate,
537 + signal: AbortSignal,
538 +): Promise<void> {
539 const backupDir = databaseBackupDir(baseDir, pending.id);
540 if (await pathExists(backupDir)) return;
541
@@ -534,10 +544,11 @@ async function backupDatabaseOnce(baseDir: string, pending: PendingServiceUpdate
544 await NodeFSP.mkdir(stagingDir, { recursive: true, mode: 0o700 });
545 try {
546 for (const suffix of DB_FILE_SUFFIXES) {
547 + signal.throwIfAborted();
548 const source = `${pending.dbPath}${suffix}`;
549 if (suffix !== "" && !(await pathExists(source))) continue;
550 const destination = databaseBackupFile(stagingDir, suffix);
540 - await copyDatabaseFile(source, destination);
551 + await copyDatabaseFile(source, destination, signal);
552 await syncFile(destination);
553 }
554 await NodeFSP.rename(stagingDir, backupDir);
@@ -830,6 +841,7 @@ export class Launcher {
841 #timer: NodeJS.Timeout | undefined;
842 #transitions: Promise<void> = Promise.resolve();
843 #stopRequested = false;
844 + readonly #stopSignal = new AbortController();
845 #stopping = false;
846 #done = false;
847 readonly #completion = Promise.withResolvers<void>();
@@ -948,6 +960,7 @@ export class Launcher {
960 return;
961 }
962 this.#stopRequested = true;
963 + this.#stopSignal.abort();
964 this.#clearTimer();
965 this.#enqueue(async () => {
966 // Let in-progress updates start replacements first; replacements observe the marker and own pre-activation tunnel cleanup.
@@ -1043,11 +1056,14 @@ export class Launcher {
1056
1057 async #startTrial(pending: PendingServiceUpdate): Promise<void> {
1058 try {
1046 - await backupDatabaseOnce(this.#baseDir, pending);
1059 + await backupDatabaseOnce(this.#baseDir, pending, this.#stopSignal.signal);
1060 } catch {
1061 + // A stop aborts the copy and leaves the update pending for the next start.
1062 + if (this.#stopRequested) return;
1063 await this.#returnToPrevious(pending, "failed", "db-backup-failed");
1064 return;
1065 }
1066 + if (this.#stopRequested) return;
1067 try {
1068 await this.#startChild(pending.targetVersion, "trial", pending);
1069 } catch {
@@ -1202,8 +1218,6 @@ export class Launcher {
1218 this.#child = null;
1219 await this.#terminate(child.process);
1220 await finishOnlineBackup?.();
1205 - // A stop during a minutes-long copy should not start a trial; restart resumes the pending update.
1206 - if (this.#stopRequested) return;
1221 await this.#startTrial(pending);
1222 }
1223