@devries-cameron20 / t3code-1 / commits / 44ee2b9c34
Fork 0

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

fix(server): restart the service after T3 home ownership clears (#25237)

Co-authored-by: shivam <91240327+shivamhwp@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

maria committed Nov 12, 2026 at 22:47 UTC 44ee2b9c3459ae232a96f6abd98179bf97320200
4 files changed +377 -36
apps/server/src/legacyServerOwner.ts new
+29
@@ -0,0 +1,29 @@
1 +// Effect-free so the service launcher shares the pre-lock owner identity check with server startup.
2 +
3 +export const legacyOwnerStartTimeCommand = (pid: number, platform: NodeJS.Platform) => {
4 + const windows = platform === "win32";
5 + return {
6 + command: windows ? "powershell.exe" : "ps",
7 + args: windows
8 + ? [
9 + "-NoProfile",
10 + "-NonInteractive",
11 + "-Command",
12 + `(Get-Process -Id ${pid} -ErrorAction Stop).StartTime.ToUniversalTime().ToString('o')`,
13 + ]
14 + : ["-p", String(pid), "-o", "lstart="],
15 + env: { LC_ALL: "C", TZ: "UTC" },
16 + };
17 +};
18 +
19 +// ps reports whole seconds. Unknown identity (no output) stays conservative.
20 +export const legacyOwnerStartedBeforeRecord = (
21 + output: string | undefined,
22 + recordedAt: number,
23 + platform: NodeJS.Platform,
24 +): boolean => {
25 + if (output === undefined) return true;
26 + const trimmed = output.trim();
27 + const startedAt = Date.parse(platform === "win32" ? trimmed : `${trimmed} UTC`);
28 + return !Number.isFinite(startedAt) || startedAt <= recordedAt + 1_000;
29 +};
apps/server/src/serverOwnership.ts
+13 -17
@@ -27,6 +27,10 @@ import {
27 compareExactServiceVersions,
28 serviceStateActiveVersion,
29 } from "./cloud/serviceProtocol.ts";
30 +import {
31 + legacyOwnerStartedBeforeRecord,
32 + legacyOwnerStartTimeCommand,
33 +} from "./legacyServerOwner.ts";
34 import * as ProcessRunner from "./processRunner.ts";
35 import {
36 clearPersistedServerRuntimeState,
@@ -80,29 +84,20 @@ const legacyOwnerIsLive = Effect.fn("legacyOwnerIsLive")(function* (
84 if (!isProcessAlive(state.pid)) return false;
85 const recordedAt = Date.parse(state.startedAt);
86 if (!Number.isFinite(recordedAt)) return true;
83 - const windows = (yield* HostProcess.Platform) === "win32";
87 + const platform = yield* HostProcess.Platform;
88 const runner = yield* ProcessRunner.ProcessRunner;
89 const result = yield* runner
90 .run({
87 - command: windows ? "powershell.exe" : "ps",
88 - args: windows
89 - ? [
90 - "-NoProfile",
91 - "-NonInteractive",
92 - "-Command",
93 - `(Get-Process -Id ${state.pid} -ErrorAction Stop).StartTime.ToUniversalTime().ToString('o')`,
94 - ]
95 - : ["-p", String(state.pid), "-o", "lstart="],
96 - env: { LC_ALL: "C", TZ: "UTC" },
91 + ...legacyOwnerStartTimeCommand(state.pid, platform),
92 timeout: Duration.seconds(2),
93 maxOutputBytes: 16_384,
94 })
95 .pipe(Effect.option);
101 - if (Option.isNone(result) || result.value.code !== 0) return true;
102 - const output = result.value.stdout.trim();
103 - const startedAt = Date.parse(windows ? output : `${output} UTC`);
104 - // ps reports whole seconds. Unknown identity stays conservative.
105 - return !Number.isFinite(startedAt) || startedAt <= recordedAt + 1_000;
96 + return legacyOwnerStartedBeforeRecord(
97 + Option.isSome(result) && result.value.code === 0 ? result.value.stdout : undefined,
98 + recordedAt,
99 + platform,
100 + );
101 });
102
103 // Pre-lock services become invisible when another server replaces their runtime
@@ -193,7 +188,8 @@ const openOwnershipLock = (stateDir: string) =>
188 }
189 const db = new NodeSqlite.DatabaseSync(lockPath);
190 try {
196 - db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;");
191 + // An idle service launcher probes this lock for a moment; wait out that probe.
192 + db.exec("PRAGMA busy_timeout = 250; BEGIN EXCLUSIVE;");
193 const identity = NodeFS.statSync(lockPath, { bigint: true });
194 return {
195 db,
apps/server/src/serviceLauncher.test.ts
+234 -4
@@ -30,6 +30,7 @@ import {
30 registerServiceTask,
31 serviceLauncherIsRunning,
32 serviceLauncherLockPath,
33 + stateDirOwned,
34 writeServiceState,
35 } from "./serviceLauncher.ts";
36 import {
@@ -44,12 +45,27 @@ import {
45 } from "./cloud/serviceProtocol.ts";
46
47 const spawnedChildren = vi.hoisted((): Array<NodeChildProcess.ChildProcess> => []);
48 +const spawnHook = vi.hoisted(() => ({
49 + current: undefined as ((child: NodeChildProcess.ChildProcess) => void) | undefined,
50 +}));
51 +
52 +const failRm = vi.hoisted(() => ({ path: undefined as string | undefined }));
53 +
54 +vi.mock("node:fs/promises", async (importOriginal) => {
55 + const actual = await importOriginal<typeof NodeFSP>();
56 + const rm: typeof actual.rm = (path, options) =>
57 + path === failRm.path
58 + ? Promise.reject(Object.assign(new Error("EACCES"), { code: "EACCES" }))
59 + : actual.rm(path, options);
60 + return { ...actual, rm };
61 +});
62
63 vi.mock("node:child_process", async (importOriginal) => {
64 const actual = await importOriginal<typeof NodeChildProcess>();
65 const spawn = (...args: Parameters<typeof actual.spawn>) => {
66 const child = actual.spawn(...args);
67 spawnedChildren.push(child);
68 + spawnHook.current?.(child);
69 return child;
70 };
71 return { ...actual, spawn: spawn as typeof actual.spawn };
@@ -578,17 +594,25 @@ process.exit(0);
594 }),
595 );
596
581 - it.effect("stays idle instead of exiting when another server owns the T3 home", () =>
597 + it.effect("waits without exiting and restarts once the other owner releases the T3 home", () =>
598 Effect.gen(function* () {
599 const fs = yield* FileSystem.FileSystem;
600 const path = yield* Path.Path;
585 - const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-refused-" });
601 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-reclaim-" });
602 const statePath = path.join(root, "runtime", "service-state.json");
603 + const ownerPath = path.join(root, "userdata", "server-owner.sqlite");
604 + yield* fs.makeDirectory(path.dirname(ownerPath), { recursive: true });
605 yield* writeFakeRuntime(
606 fs,
607 path,
608 path.join(root, "runtime", "versions", "1.0.0"),
591 - `process.exit(${SERVER_EXIT_CODE_STATE_DIR_OWNED});\n`,
609 + [
610 + `const db = new (require("node:sqlite").DatabaseSync)(${JSON.stringify(ownerPath)});`,
611 + `try { db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;"); }`,
612 + `catch { process.exit(${SERVER_EXIT_CODE_STATE_DIR_OWNED}); }`,
613 + 'process.send("owner");',
614 + "setInterval(() => {}, 1_000);",
615 + ].join("\n"),
616 );
617 yield* Effect.promise(() =>
618 writeServiceState(statePath, {
@@ -596,7 +620,10 @@ process.exit(0);
620 activeVersion: "1.0.0",
621 }),
622 );
623 + using otherOwner = new NodeSqlite.DatabaseSync(ownerPath);
624 + otherOwner.exec("BEGIN EXCLUSIVE;");
625 const refused = Promise.withResolvers<void>();
626 + const owned = Promise.withResolvers<void>();
627 yield* Effect.acquireRelease(
628 Effect.sync(() =>
629 vi.spyOn(process.stderr, "write").mockImplementation((chunk) => {
@@ -606,15 +633,218 @@ process.exit(0);
633 ),
634 (spy) => Effect.sync(() => spy.mockRestore()),
635 );
609 - const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
636 + yield* Effect.acquireRelease(
637 + Effect.sync(() => {
638 + spawnHook.current = (child) => child.once("message", () => owned.resolve());
639 + }),
640 + () =>
641 + Effect.sync(() => {
642 + spawnHook.current = undefined;
643 + }),
644 + );
645 + const spawnsBefore = spawnedChildren.length;
646 + const launcher = new Launcher(
647 + root,
648 + yield* Effect.promise(() => readServiceState(statePath)),
649 + { stateDirOwnerPollMs: 10 },
650 + );
651 let completed = false;
652 const running = launcher.run().finally(() => {
653 completed = true;
654 });
655 yield* Effect.promise(() => refused.promise);
656 assert.isFalse(completed);
657 + otherOwner.close();
658 + yield* Effect.promise(() => owned.promise);
659 + yield* Effect.promise(() => launcher.stop("SIGTERM"));
660 + yield* Effect.promise(() => running);
661 + assert.strictEqual(spawnedChildren.length - spawnsBefore, 2);
662 + }),
663 + );
664 +
665 + it.effect(
666 + "keeps a pending update while owned and rolls a failed resumed trial back to the other owner's writes",
667 + () =>
668 + Effect.gen(function* () {
669 + const fs = yield* FileSystem.FileSystem;
670 + const path = yield* Path.Path;
671 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-reclaim-update-" });
672 + const statePath = path.join(root, "runtime", "service-state.json");
673 + const ownerPath = path.join(root, "userdata", "server-owner.sqlite");
674 + const databasePath = path.join(root, "userdata", "state.sqlite");
675 + const startsPath = path.join(root, "starts.log");
676 + yield* fs.makeDirectory(path.dirname(ownerPath), { recursive: true });
677 + yield* fs.writeFileString(databasePath, "before update");
678 + for (const version of ["1.0.0", "1.1.0"]) {
679 + yield* writeFakeRuntime(
680 + fs,
681 + path,
682 + path.join(root, "runtime", "versions", version),
683 + [
684 + `const db = new (require("node:sqlite").DatabaseSync)(${JSON.stringify(ownerPath)});`,
685 + `try { db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;"); }`,
686 + `catch { process.exit(${SERVER_EXIT_CODE_STATE_DIR_OWNED}); }`,
687 + "const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);",
688 + `require("node:fs").appendFileSync(${JSON.stringify(startsPath)}, context.childVersion + "\\n");`,
689 + 'if (context.update?.status === "pending") {',
690 + ` require("node:fs").writeFileSync(${JSON.stringify(databasePath)}, "migrated by failed trial");`,
691 + " process.exit(1);",
692 + "}",
693 + 'process.send("active");',
694 + "setInterval(() => {}, 1_000);",
695 + ].join("\n"),
696 + );
697 + }
698 + yield* Effect.promise(() =>
699 + writeServiceState(statePath, {
700 + protocol: SERVICE_LAUNCHER_PROTOCOL,
701 + activeVersion: "1.0.0",
702 + update: {
703 + id: "update-1",
704 + fromVersion: "1.0.0",
705 + targetVersion: "1.1.0",
706 + dbPath: databasePath,
707 + status: "pending",
708 + },
709 + }),
710 + );
711 + using otherOwner = new NodeSqlite.DatabaseSync(ownerPath);
712 + otherOwner.exec("BEGIN EXCLUSIVE;");
713 + const refused = Promise.withResolvers<void>();
714 + const active = Promise.withResolvers<void>();
715 + yield* Effect.acquireRelease(
716 + Effect.sync(() =>
717 + vi.spyOn(process.stderr, "write").mockImplementation((chunk) => {
718 + if (String(chunk).includes("owns this T3 home")) refused.resolve();
719 + return true;
720 + }),
721 + ),
722 + (spy) => Effect.sync(() => spy.mockRestore()),
723 + );
724 + yield* Effect.acquireRelease(
725 + Effect.sync(() => {
726 + spawnHook.current = (child) =>
727 + child.on("message", (message) => message === "active" && active.resolve());
728 + }),
729 + () =>
730 + Effect.sync(() => {
731 + spawnHook.current = undefined;
732 + }),
733 + );
734 + const launcher = new Launcher(
735 + root,
736 + yield* Effect.promise(() => readServiceState(statePath)),
737 + { stateDirOwnerPollMs: 10 },
738 + );
739 + const running = launcher.run();
740 + yield* Effect.promise(() => refused.promise);
741 + const idle = yield* Effect.promise(() => readServiceState(statePath));
742 + assert.strictEqual(idle.activeVersion, "1.0.0");
743 + assert.strictEqual(idle.update?.status, "pending");
744 + assert.isFalse(yield* fs.exists(path.join(root, "runtime", "db-backup", "update-1")));
745 +
746 + yield* fs.writeFileString(databasePath, "written by the other owner");
747 + otherOwner.close();
748 + yield* Effect.promise(() => active.promise);
749 + yield* Effect.promise(() => launcher.stop("SIGTERM"));
750 + yield* Effect.promise(() => running);
751 +
752 + const state = yield* Effect.promise(() => readServiceState(statePath));
753 + assert.strictEqual(state.activeVersion, "1.0.0");
754 + assert.strictEqual(state.update?.status, "rolled-back");
755 + assert.strictEqual(yield* fs.readFileString(startsPath), "1.1.0\n1.0.0\n");
756 + assert.strictEqual(yield* fs.readFileString(databasePath), "written by the other owner");
757 + }),
758 + );
759 +
760 + it.effect("fails a pending update when its backup cannot be dropped while owned", () =>
761 + Effect.gen(function* () {
762 + const fs = yield* FileSystem.FileSystem;
763 + const path = yield* Path.Path;
764 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-stale-backup-" });
765 + const statePath = path.join(root, "runtime", "service-state.json");
766 + const ownerPath = path.join(root, "userdata", "server-owner.sqlite");
767 + const databasePath = path.join(root, "userdata", "state.sqlite");
768 + yield* fs.makeDirectory(path.dirname(ownerPath), { recursive: true });
769 + yield* fs.writeFileString(databasePath, "before update");
770 + yield* writeFakeRuntime(
771 + fs,
772 + path,
773 + path.join(root, "runtime", "versions", "1.1.0"),
774 + `process.exit(${SERVER_EXIT_CODE_STATE_DIR_OWNED});\n`,
775 + );
776 + yield* Effect.promise(() =>
777 + writeServiceState(statePath, {
778 + protocol: SERVICE_LAUNCHER_PROTOCOL,
779 + activeVersion: "1.0.0",
780 + update: {
781 + id: "update-1",
782 + fromVersion: "1.0.0",
783 + targetVersion: "1.1.0",
784 + dbPath: databasePath,
785 + status: "pending",
786 + },
787 + }),
788 + );
789 + const refused = Promise.withResolvers<void>();
790 + yield* Effect.acquireRelease(
791 + Effect.sync(() =>
792 + vi.spyOn(process.stderr, "write").mockImplementation((chunk) => {
793 + if (String(chunk).includes("owns this T3 home")) refused.resolve();
794 + return true;
795 + }),
796 + ),
797 + (spy) => Effect.sync(() => spy.mockRestore()),
798 + );
799 + yield* Effect.acquireRelease(
800 + Effect.sync(() => {
801 + failRm.path = path.join(root, "runtime", "db-backup", "update-1");
802 + }),
803 + () =>
804 + Effect.sync(() => {
805 + failRm.path = undefined;
806 + }),
807 + );
808 + const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
809 + const running = launcher.run();
810 + yield* Effect.promise(() => refused.promise);
811 yield* Effect.promise(() => launcher.stop("SIGTERM"));
812 yield* Effect.promise(() => running);
813 +
814 + const state = yield* Effect.promise(() => readServiceState(statePath));
815 + assert.strictEqual(state.activeVersion, "1.0.0");
816 + assert.strictEqual(state.update?.status, "failed");
817 + }),
818 + );
819 +
820 + it.effect("treats the T3 home as owned while the lock or a pre-lock server holds it", () =>
821 + Effect.gen(function* () {
822 + const fs = yield* FileSystem.FileSystem;
823 + const path = yield* Path.Path;
824 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-owned-" });
825 + const stateDir = path.join(root, "userdata");
826 + const recordPath = path.join(stateDir, "server-runtime.json");
827 + yield* fs.makeDirectory(stateDir, { recursive: true });
828 + assert.isFalse(yield* Effect.promise(() => stateDirOwned(root)));
829 +
830 + using owner = new NodeSqlite.DatabaseSync(path.join(stateDir, "server-owner.sqlite"));
831 + owner.exec("BEGIN EXCLUSIVE;");
832 + assert.isTrue(yield* Effect.promise(() => stateDirOwned(root)));
833 + owner.close();
834 +
835 + const record = { version: 1, pid: process.pid, port: 1, origin: "", startedAt: "" };
836 + yield* fs.writeFileString(recordPath, JSON.stringify(record));
837 + assert.isTrue(yield* Effect.promise(() => stateDirOwned(root)));
838 + yield* fs.writeFileString(recordPath, JSON.stringify({ ...record, ownerId: "lock-aware" }));
839 + assert.isFalse(yield* Effect.promise(() => stateDirOwned(root)));
840 + // This process started after 1970, so its PID was reused since that record.
841 + yield* fs.writeFileString(
842 + recordPath,
843 + JSON.stringify({ ...record, startedAt: "1970-01-01T00:00:00Z" }),
844 + );
845 + assert.isFalse(yield* Effect.promise(() => stateDirOwned(root)));
846 + yield* fs.writeFileString(recordPath, JSON.stringify({ ...record, pid: 2 ** 22 + 1 }));
847 + assert.isFalse(yield* Effect.promise(() => stateDirOwned(root)));
848 }),
849 );
850
apps/server/src/serviceLauncher.ts
+101 -15
@@ -36,6 +36,10 @@ import {
36 SERVICE_STOP_MARKER_FILE,
37 SERVICE_UNIT_ENV,
38 } from "./cloud/serviceProtocol.ts";
39 +import {
40 + legacyOwnerStartedBeforeRecord,
41 + legacyOwnerStartTimeCommand,
42 +} from "./legacyServerOwner.ts";
43
44 const HANDOFF_DELAY_MS = 2_000;
45 /**
@@ -49,6 +53,7 @@ const LOCK_RETRY_MS = 500;
53 const LOCK_ATTEMPTS = 240;
54 // Match contracts' SERVER_EXIT_CODE_STATE_DIR_OWNED without depending on Effect.
55 const STATE_DIR_OWNED_EXIT_CODE = 78;
56 +const STATE_DIR_OWNER_POLL_MS = 30_000;
57
58 type TerminalStatus = "committed" | "rolled-back" | "failed";
59 type ChildRole = "active" | "trial";
@@ -703,7 +708,7 @@ export const serviceLauncherLockPath = (baseDir: string) =>
708 NodePath.join(baseDir, "runtime", SERVICE_LAUNCHER_LOCK_FILE);
709
710 // An exclusive SQLite transaction, like the server's owner lock: the OS drops it when the holder dies.
706 -async function tryAcquireLauncherLock(
711 +async function tryAcquireSqliteLock(
712 lockPath: string,
713 ): Promise<{ readonly close: () => void } | undefined> {
714 const { DatabaseSync } = await import("node:sqlite");
@@ -720,7 +725,7 @@ async function tryAcquireLauncherLock(
725
726 export async function serviceLauncherIsRunning(lockPath: string): Promise<boolean> {
727 if (!NodeFS.existsSync(lockPath)) return false;
723 - const lock = await tryAcquireLauncherLock(lockPath);
728 + const lock = await tryAcquireSqliteLock(lockPath);
729 lock?.close();
730 return lock === undefined;
731 }
@@ -736,6 +741,50 @@ export async function waitForServiceLauncherExit(
741 }
742 }
743
744 +// Mirror acquireServerOwnership: the lock, then a live pre-lock server-runtime.json owner;
745 +// leave other failures for server startup to report.
746 +export async function stateDirOwned(baseDir: string): Promise<boolean> {
747 + const stateDir = NodePath.join(baseDir, "userdata");
748 + const lock = await tryAcquireSqliteLock(NodePath.join(stateDir, "server-owner.sqlite")).catch(
749 + () => null,
750 + );
751 + if (lock === null) return false;
752 + if (lock === undefined) return true;
753 + lock.close();
754 + let record: unknown;
755 + try {
756 + record = JSON.parse(
757 + await NodeFSP.readFile(NodePath.join(stateDir, "server-runtime.json"), "utf8"),
758 + );
759 + } catch {
760 + return false;
761 + }
762 + if (typeof record !== "object" || record === null || "ownerId" in record) return false;
763 + const pid = "pid" in record ? record.pid : undefined;
764 + if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 0) return false;
765 + try {
766 + process.kill(pid, 0);
767 + } catch (error) {
768 + if (!(error instanceof Error && "code" in error && error.code === "EPERM")) return false;
769 + }
770 + const recordedAt = Date.parse(
771 + "startedAt" in record && typeof record.startedAt === "string" ? record.startedAt : "",
772 + );
773 + if (!Number.isFinite(recordedAt)) return true;
774 + // oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone launcher has no Effect runtime.
775 + const platform = process.platform;
776 + const { command, args, env } = legacyOwnerStartTimeCommand(pid, platform);
777 + const output = await new Promise<string | undefined>((resolve) =>
778 + NodeChildProcess.execFile(
779 + command,
780 + args,
781 + { env: { ...process.env, ...env }, timeout: 2_000, maxBuffer: 16_384 },
782 + (error, stdout) => resolve(error === null ? stdout : undefined),
783 + ),
784 + );
785 + return legacyOwnerStartedBeforeRecord(output, recordedAt, platform);
786 +}
787 +
788 const stopMarkerPath = (baseDir: string) =>
789 NodePath.join(baseDir, "runtime", SERVICE_STOP_MARKER_FILE);
790 const restartPendingPath = (baseDir: string) =>
@@ -755,6 +804,8 @@ export class Launcher {
804 readonly #backupSpaceProbe: Partial<BackupSpaceProbe> | undefined;
805 readonly #serviceUnit: ServiceUnit | undefined;
806 readonly #environment: Readonly<NodeJS.ProcessEnv>;
807 + readonly #stateDirOwnerPollMs: number;
808 + #stateDirFreeProbes = 0;
809 readonly #output: number | undefined;
810 readonly #lockPath: string | undefined;
811 readonly #stopChildOverIpc: boolean;
@@ -766,6 +817,7 @@ export class Launcher {
817 readonly backupSpaceProbe?: Partial<BackupSpaceProbe>;
818 readonly serviceUnit?: ServiceUnit;
819 readonly environment?: Readonly<NodeJS.ProcessEnv>;
820 + readonly stateDirOwnerPollMs?: number;
821 /** Launcher and server output, for managers that cannot redirect it. */
822 readonly output?: number;
823 /** Makes this the only launcher for its home; a replacement waits for the one stopping. */
@@ -775,6 +827,7 @@ export class Launcher {
827 } = {},
828 ) {
829 this.#baseDir = baseDir;
830 + this.#stateDirOwnerPollMs = options.stateDirOwnerPollMs ?? STATE_DIR_OWNER_POLL_MS;
831 this.#backupSpaceProbe = options.backupSpaceProbe;
832 this.#serviceUnit = options.serviceUnit;
833 this.#environment = { ...(options.environment ?? process.env) };
@@ -815,7 +868,7 @@ export class Launcher {
868
869 async #acquireLock(lockPath: string): Promise<{ readonly close: () => void } | undefined> {
870 for (let attempt = 1; !this.#stopRequested; attempt++) {
818 - const lock = await tryAcquireLauncherLock(lockPath);
871 + const lock = await tryAcquireSqliteLock(lockPath);
872 if (lock !== undefined) return lock;
873 if (attempt >= LOCK_ATTEMPTS) {
874 throw new Error("Another T3 Code service launcher still runs for this T3 home.");
@@ -867,6 +920,8 @@ export class Launcher {
920 this.#enqueue(async () => {
921 // Let in-progress updates start replacements first; replacements observe the marker and own pre-activation tunnel cleanup.
922 this.#stopping = true;
923 + // An exit-78 transition queued before this one may have started the owner poll.
924 + this.#clearTimer();
925 const child = this.#child?.process;
926 this.#child = null;
927 if (child !== undefined) await this.#terminate(child, signal);
@@ -1233,27 +1288,58 @@ export class Launcher {
1288 throw new Error(`Active child exited unexpectedly (${String(code ?? signal ?? "unknown")}).`);
1289 }
1290
1236 - // Stay idle to avoid supervisor restart loops; never restore backups over another owner's possible writes.
1291 + // Stay idle until the lock frees to avoid supervisor restart loops. A pending update resumes its
1292 + // trial, since the other owner may have migrated the database past fromVersion. It stays pending
1293 + // only once its backup is gone, so no later trial, even after a launcher restart, restores it over
1294 + // the other owner's writes; exit 78 comes before the child opens the database.
1295 async #idleWhileStateDirOwned(): Promise<void> {
1296 this.#clearTimer();
1297 const pending = this.#state.update;
1298 if (pending?.status === "pending") {
1241 - const next: ServiceState = {
1242 - ...this.#state,
1243 - activeVersion: pending.fromVersion,
1244 - update: terminalUpdate({ pending, status: "failed", reason: "state-dir-owned" }),
1245 - };
1246 - await this.#selectState(next);
1247 - await discardDatabaseBackup(this.#baseDir, pending.id).catch(() => undefined);
1248 - await this.#repointServiceUnit(next.activeVersion);
1299 + const discarded = await discardDatabaseBackup(this.#baseDir, pending.id).then(
1300 + () => true,
1301 + () => false,
1302 + );
1303 + if (!discarded) {
1304 + const next: ServiceState = {
1305 + ...this.#state,
1306 + activeVersion: pending.fromVersion,
1307 + update: terminalUpdate({ pending, status: "failed", reason: "state-dir-owned" }),
1308 + };
1309 + await this.#selectState(next);
1310 + await this.#repointServiceUnit(next.activeVersion);
1311 + }
1312 }
1250 - // Signal listeners alone do not keep Node alive.
1251 - this.#timer = setInterval(() => {}, 2_147_483_647);
1313 this.#log(
1253 - "[service-launcher] Another T3 Code server owns this T3 home. Stop it, then restart this service.\n",
1314 + "[service-launcher] Another T3 Code server owns this T3 home. This service starts again once it stops.\n",
1315 + );
1316 + this.#stateDirFreeProbes = 0;
1317 + // The interval also keeps Node alive: signal listeners alone do not.
1318 + this.#timer = setInterval(
1319 + () => this.#enqueue(() => this.#reclaimStateDir()),
1320 + this.#stateDirOwnerPollMs,
1321 );
1322 }
1323
1324 + async #reclaimStateDir(): Promise<void> {
1325 + if (this.#stopRequested) {
1326 + this.#clearTimer();
1327 + return;
1328 + }
1329 + if (this.#timer === undefined || this.#child !== null) return;
1330 + const owned = await stateDirOwned(this.#baseDir);
1331 + // A stop during the probe clears the timer.
1332 + if (this.#timer === undefined) return;
1333 + // A relaunching desktop app frees the home for a few seconds; leave it that gap.
1334 + this.#stateDirFreeProbes = owned ? 0 : this.#stateDirFreeProbes + 1;
1335 + if (this.#stateDirFreeProbes < 2) return;
1336 + this.#clearTimer();
1337 + this.#log("[service-launcher] The T3 home is free again. Starting the server.\n");
1338 + const update = this.#state.update;
1339 + if (update?.status === "pending") await this.#startTrial(update);
1340 + else await this.#startChild(this.#state.activeVersion, "active", update);
1341 + }
1342 +
1343 async #returnToPrevious(
1344 pending: PendingServiceUpdate,
1345 status: "rolled-back" | "failed",