From 023fb42f430df69f5e58c2bf7086d869ab862043 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 3 Dec 2026 21:36:01 +0000 Subject: [PATCH] fix(server): an idle service claims a freed T3 home before an SSH launch retakes it An installed service idling behind an SSH-started server only reclaimed the home after two free probes 30 s apart, while a connected desktop relaunches its SSH server within seconds, so the service never started. The idle launcher now probes every second and takes the serving lock as soon as the home is free, so the SSH launch waits for and adopts the service's server. It still waits for the home to stay free for 30 s before starting, and gives the lock back if another server takes the home in that window. Fixes #48447 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/serviceLauncher.test.ts | 6 ++++- apps/server/src/serviceLauncher.ts | 33 ++++++++++++++++++------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index 742765a183177..12a77e164ee51 100644 --- a/apps/server/src/serviceLauncher.test.ts +++ b/apps/server/src/serviceLauncher.test.ts @@ -1008,11 +1008,13 @@ process.exit(0); using otherOwner = new NodeSqlite.DatabaseSync(ownerPath); otherOwner.exec("BEGIN EXCLUSIVE;"); const refused = Promise.withResolvers(); + const freed = Promise.withResolvers(); const owned = Promise.withResolvers(); yield* Effect.acquireRelease( Effect.sync(() => vi.spyOn(process.stderr, "write").mockImplementation((chunk) => { if (String(chunk).includes("owns this T3 home")) refused.resolve(); + if (String(chunk).includes("once it stays free")) freed.resolve(); return true; }), ), @@ -1042,8 +1044,10 @@ process.exit(0); // An idle launcher must not make an SSH launch wait for a server it is not starting. assert.isFalse(yield* Effect.promise(() => serviceLauncherIsServing(root))); otherOwner.close(); - yield* Effect.promise(() => owned.promise); + yield* Effect.promise(() => freed.promise); + // A reconnecting SSH launch must wait for this server, not start one in the free gap. assert.isTrue(yield* Effect.promise(() => serviceLauncherIsServing(root))); + yield* Effect.promise(() => owned.promise); yield* Effect.promise(() => launcher.stop("SIGTERM")); yield* Effect.promise(() => running); assert.strictEqual(spawnedChildren.length - spawnsBefore, 2); diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.ts index 2688b7fcb0915..93865580d3ce4 100644 --- a/apps/server/src/serviceLauncher.ts +++ b/apps/server/src/serviceLauncher.ts @@ -63,7 +63,11 @@ const LOCK_RETRY_MS = 500; const LOCK_ATTEMPTS = 240; // Match contracts' SERVER_EXIT_CODE_STATE_DIR_OWNED without depending on Effect. const STATE_DIR_OWNED_EXIT_CODE = 78; -const STATE_DIR_OWNER_POLL_MS = 30_000; +const STATE_DIR_OWNER_POLL_MS = 1_000; +// A relaunching desktop app frees the home for a few seconds; leave it that gap. +const STATE_DIR_FREE_PROBES = 30; +const STATE_DIR_OWNED_MESSAGE = + "[service-launcher] Another T3 Code server owns this T3 home. This service starts again once it stops.\n"; type TerminalStatus = "committed" | "rolled-back" | "failed"; type ChildRole = "active" | "trial"; @@ -1586,9 +1590,7 @@ export class Launcher { } } this.#releaseServing(); - this.#log( - "[service-launcher] Another T3 Code server owns this T3 home. This service starts again once it stops.\n", - ); + this.#log(STATE_DIR_OWNED_MESSAGE); this.#stateDirFreeProbes = 0; // The interval also keeps Node alive: signal listeners alone do not. this.#timer = setInterval( @@ -1606,12 +1608,25 @@ export class Launcher { const owned = await stateDirOwned(this.#baseDir); // A stop during the probe clears the timer. if (this.#timer === undefined) return; - // A relaunching desktop app frees the home for a few seconds; leave it that gap. - this.#stateDirFreeProbes = owned ? 0 : this.#stateDirFreeProbes + 1; - if (this.#stateDirFreeProbes < 2) return; - this.#clearTimer(); + if (owned) { + if (this.#stateDirFreeProbes > 0) { + this.#releaseServing(); + this.#log(STATE_DIR_OWNED_MESSAGE); + } + this.#stateDirFreeProbes = 0; + return; + } + // Claim the home at once so a reconnecting SSH launch waits for this server instead of + // starting its own in the gap. await this.#claimServing(); - this.#log("[service-launcher] The T3 home is free again. Starting the server.\n"); + if (this.#timer === undefined) return; + if (this.#stateDirFreeProbes++ === 0) { + this.#log( + "[service-launcher] The T3 home is free again. Starting the server once it stays free.\n", + ); + } + if (this.#stateDirFreeProbes < STATE_DIR_FREE_PROBES) return; + this.#clearTimer(); const update = this.#state.update; if (update?.status === "pending") await this.#startTrial(update); else await this.#startChild(this.#state.activeVersion, "active", update);