diff --git a/apps/server/src/cli/pair.test.ts b/apps/server/src/cli/pair.test.ts index e626461914a06..36b0b4fb1e17d 100644 --- a/apps/server/src/cli/pair.test.ts +++ b/apps/server/src/cli/pair.test.ts @@ -202,10 +202,14 @@ describe("t3 pair", () => { const statePath = NodePath.join(baseDir, "userdata", "server-runtime.json"); yield* persistServerRuntimeState({ path: statePath, - state: yield* makePersistedServerRuntimeState({ - config: { host: "127.0.0.1", devUrl: undefined }, - port, - }), + state: { + ...(yield* makePersistedServerRuntimeState({ + config: { host: "127.0.0.1", devUrl: undefined }, + port, + })), + // The hostname can change under a running server; a live PID that answers is local. + hostname: "renamed-host", + }, }); const output = yield* captureStdout(runCli(["pair", "--base-dir", baseDir])); @@ -343,6 +347,35 @@ describe("t3 pair", () => { ).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ); + // regression: #34054 on a shared $HOME the recorded pid belongs to another machine. + it.effect("names the other machine whose server owns a shared T3 home", () => + Effect.gen(function* () { + const baseDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3 pair host test-")); + const state = yield* makePersistedServerRuntimeState({ + config: { host: "127.0.0.1", devUrl: undefined }, + port: 1, + }); + NodeFS.mkdirSync(NodePath.join(baseDir, "userdata"), { recursive: true }); + // The recorded PID may be dead here, or belong to an unrelated local process. + for (const pid of [4_194_305, process.pid]) { + NodeFS.writeFileSync( + NodePath.join(baseDir, "userdata", "server-runtime.json"), + JSON.stringify({ ...state, pid, hostname: "host-a" }), + ); + + const error = yield* provideCliTestLayers( + runCli(["pair", "--base-dir", baseDir]).pipe(Effect.flip), + ); + + const rendered = String( + typeof error === "object" && error !== null && "cause" in error ? error.cause : error, + ); + assert.notInclude(rendered, "No running T3 Code server found."); + assert.include(rendered, "host-a"); + } + }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), + ); + // regression: #10516 minting opens the target database with this checkout's // migrations, so a worktree must never fall back to the live install. it.effect("does not fall back to the shared home from a linked worktree", () => diff --git a/apps/server/src/cli/pair.ts b/apps/server/src/cli/pair.ts index 904182ad29c6b..075fb959f18db 100644 --- a/apps/server/src/cli/pair.ts +++ b/apps/server/src/cli/pair.ts @@ -36,6 +36,7 @@ import { resolveBaseDir } from "../os-jank.ts"; import { type PersistedServerRuntimeState, isProcessAlive, + otherMachineOf, readPersistedServerRuntimeState, } from "../serverRuntimeState.ts"; import { @@ -82,6 +83,18 @@ export class NoRunningServerError extends Schema.TaggedError()( + "ServerOnOtherMachineError", + { hostname: Schema.String, statePath: Schema.String }, +) { + override get message(): string { + return [ + `The T3 Code server recorded in ${this.statePath} runs on ${this.hostname}, which shares this T3 home.`, + `Run \`t3 pair\` on ${this.hostname}, or start a server on this machine with a separate T3 home (--base-dir).`, + ].join("\n"); + } +} + export class IncompatibleServerProtocolError extends Schema.TaggedError()( "IncompatibleServerProtocolError", { serverVersion: Schema.String, serverProtocolVersion: Schema.Number }, @@ -267,6 +280,8 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* ( } const checkedStatePaths: Array = []; + const localHostname = yield* HostProcess.Hostname; + let otherMachine: ServerOnOtherMachineError | undefined; for (const baseDir of new Set(bases)) { for (const variant of ["userdata", "dev"] as const) { const derivedPaths = yield* ServerConfig.deriveServerPaths( @@ -281,11 +296,15 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* ( continue; } // A dead server's port may be reused; do not mint tokens in its old database for another server. - if (!isProcessAlive(state.value.pid)) { - continue; - } - const probed = yield* probeEnvironmentDescriptor(state.value.origin); - if (probed._tag !== "descriptor") { + // A live PID that answers is local even under a changed hostname (macOS follows the network). + const probed = isProcessAlive(state.value.pid) + ? yield* probeEnvironmentDescriptor(state.value.origin) + : undefined; + if (probed?._tag !== "descriptor") { + const hostname = otherMachineOf(state.value, localHostname); + if (hostname !== undefined) { + otherMachine ??= new ServerOnOtherMachineError({ hostname, statePath }); + } continue; } return { @@ -296,6 +315,9 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* ( } satisfies DiscoveredPairTarget; } } + if (otherMachine !== undefined) { + return yield* otherMachine; + } const baseDirArgs = hasExplicitBaseDir ? ["--base-dir", explicitBaseDir] : []; return yield* new NoRunningServerError({ checkedStatePaths, diff --git a/apps/server/src/cli/service.ts b/apps/server/src/cli/service.ts index ce43154116b8e..4a7f4ddb477e1 100644 --- a/apps/server/src/cli/service.ts +++ b/apps/server/src/cli/service.ts @@ -97,6 +97,9 @@ export const reconcileService = Effect.fn("cli.service.reconcile")(function* (op return yield* new BootService.BootServiceOtherBaseDirError({ installedBaseDir: status.otherBaseDir, baseDir, + ...(status.otherBaseDirHostname === undefined + ? {} + : { ownerHostname: status.otherBaseDirHostname }), }); } if (status.installed && status.current) { diff --git a/apps/server/src/cli/up.ts b/apps/server/src/cli/up.ts index 9bd0409f6ef9f..5dbf36233f375 100644 --- a/apps/server/src/cli/up.ts +++ b/apps/server/src/cli/up.ts @@ -218,6 +218,9 @@ const setUp = Effect.fn("cli.up")(function* (flags: { return yield* new BootService.BootServiceOtherBaseDirError({ installedBaseDir: before.service.otherBaseDir, baseDir, + ...(before.service.otherBaseDirHostname === undefined + ? {} + : { ownerHostname: before.service.otherBaseDirHostname }), }); } if (before.connect === null && Option.getOrElse(flags.connect, () => false)) { diff --git a/apps/server/src/cloud/bootService.test.ts b/apps/server/src/cloud/bootService.test.ts index 814d470cad410..ed8744a5295d3 100644 --- a/apps/server/src/cloud/bootService.test.ts +++ b/apps/server/src/cloud/bootService.test.ts @@ -698,6 +698,29 @@ it.layer(NodeServices.layer)("boot service install", (it) => { expect(yield* fs.readFileString(plan.unitPath)).toBe(unit); expect(commands).toEqual([]); expect((yield* other.status).otherBaseDir).toBe(baseDir); + expect(refused.message).toContain("t3 service uninstall"); + + // A shared $HOME: the served home's server recorded another machine. + yield* fs.makeDirectory(path.join(baseDir, "userdata"), { recursive: true }); + yield* fs.writeFileString( + path.join(baseDir, "userdata", "server-runtime.json"), + JSON.stringify({ + version: 1, + pid: 4_194_305, + hostname: "host-a", + port: 3773, + origin: "http://127.0.0.1:3773", + startedAt: "2026-01-01T00:00:00.000Z", + }), + ); + const shared = yield* other.install().pipe(Effect.flip); + expect(shared).toMatchObject({ + _tag: "BootServiceOtherBaseDirError", + ownerHostname: "host-a", + }); + expect(shared.message).toContain("runs on host-a"); + expect(shared.message).not.toContain("t3 service uninstall"); + expect((yield* other.status).otherBaseDirHostname).toBe("host-a"); }), ); diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.ts index 88d477974ae0a..837360529b8b8 100644 --- a/apps/server/src/cloud/bootService.ts +++ b/apps/server/src/cloud/bootService.ts @@ -16,6 +16,7 @@ import * as Schema from "effect/Schema"; import { CLI_RELEASE_BASE_URL_ENV } from "@t3tools/shared/cliRelease"; import * as ProcessRunner from "../processRunner.ts"; +import { otherMachineOf, readPersistedServerRuntimeState } from "../serverRuntimeState.ts"; import { ensurePinnedRuntimeInstalled, launcherOwnsVersionsDir, @@ -523,9 +524,14 @@ export class BootServiceOtherBaseDirError extends Schema.TaggedError; readonly unitPath: string; readonly logPath: string; @@ -655,6 +663,14 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { const logPath = path.join(input.logsDir, BOOT_SERVICE_LOG_FILE); const statePath = path.join(input.baseDir, "runtime", SERVICE_STATE_FILE); const restartPendingPath = path.join(input.baseDir, "runtime", SERVICE_RESTART_PENDING_FILE); + const localHostname = yield* HostProcess.Hostname; + const hostnameOfOtherMachineServing = (baseDir: string) => + readPersistedServerRuntimeState(path.join(baseDir, "userdata", "server-runtime.json")).pipe( + Effect.map((state) => + Option.isSome(state) ? otherMachineOf(state.value, localHostname) : undefined, + ), + Effect.provideService(FileSystem.FileSystem, fs), + ); const otherBaseDirOf = (unit: string) => { const installedBaseDir = bootServiceBaseDirOf(unit); return installedBaseDir !== undefined && @@ -811,7 +827,12 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { ? otherBaseDirOf(existingUnit.value) : undefined; if (installedBaseDir !== undefined) { - return yield* new BootServiceOtherBaseDirError({ installedBaseDir, baseDir: input.baseDir }); + const ownerHostname = yield* hostnameOfOtherMachineServing(installedBaseDir); + return yield* new BootServiceOtherBaseDirError({ + installedBaseDir, + baseDir: input.baseDir, + ...(ownerHostname === undefined ? {} : { ownerHostname }), + }); } yield* fs .makeDirectory(input.logsDir, { recursive: true }) @@ -1081,6 +1102,8 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { : undefined; const installedBaseDir = bootServiceBaseDirOf(unit); const otherBaseDir = otherBaseDirOf(unit); + const otherBaseDirHostname = + otherBaseDir === undefined ? undefined : yield* hostnameOfOtherMachineServing(otherBaseDir); const normalizeUnit = (contents: string) => detectedManager.kind === "launchd" ? contents.replace(/(PATH<\/key>\n\s*)[^<]*(<\/string>)/, "$1$2") @@ -1094,6 +1117,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { ...(installedVersion === undefined ? {} : { installedVersion }), ...(installedBaseDir === undefined ? {} : { installedBaseDir }), ...(otherBaseDir === undefined ? {} : { otherBaseDir }), + ...(otherBaseDirHostname === undefined ? {} : { otherBaseDirHostname }), problems, current: problems.length === 0 && diff --git a/apps/server/src/serverOwnership.ts b/apps/server/src/serverOwnership.ts index 565f8111849e0..0139771acffec 100644 --- a/apps/server/src/serverOwnership.ts +++ b/apps/server/src/serverOwnership.ts @@ -31,6 +31,7 @@ import * as ProcessRunner from "./processRunner.ts"; import { clearPersistedServerRuntimeState, isProcessAlive, + otherMachineOf, persistServerRuntimeState, readPersistedServerRuntimeState, type PersistedServerRuntimeState, @@ -38,13 +39,17 @@ import { export class ServerAlreadyRunningError extends Schema.TaggedError()( "ServerAlreadyRunningError", - { stateDir: Schema.String }, + { stateDir: Schema.String, hostname: Schema.optional(Schema.String) }, ) { // Distinct exit codes let supervisors stop restarting an already-owned server. override readonly [Runtime.errorExitCode] = SERVER_EXIT_CODE_STATE_DIR_OWNED; override get message(): string { - return `A T3 Code server already owns ${this.stateDir}. Stop that server, or use a separate T3 home and pair with it, then retry. No server was stopped. server-owner.sqlite is the ownership lock; do not delete it while a server is running.`; + const owner = + this.hostname === undefined + ? `A T3 Code server already owns ${this.stateDir}. Stop that server` + : `A T3 Code server on ${this.hostname} already owns ${this.stateDir}, which this machine shares. Stop it on ${this.hostname}`; + return `${owner}, or use a separate T3 home and pair with it, then retry. No server was stopped. server-owner.sqlite is the ownership lock; do not delete it while a server is running.`; } } @@ -227,6 +232,18 @@ export const acquireServerOwnership = Effect.fn("acquireServerOwnership")(functi Effect.ignore({ log: true }), Effect.ensuring(Effect.sync(() => db.close())), ), + ).pipe( + Effect.catchTags({ + ServerAlreadyRunningError: Effect.fn("acquireServerOwnership.nameOwner")(function* (error) { + const owner = yield* readPersistedServerRuntimeState(statePath); + const hostname = Option.isSome(owner) + ? otherMachineOf(owner.value, yield* HostProcess.Hostname) + : undefined; + return yield* hostname === undefined + ? error + : new ServerAlreadyRunningError({ stateDir, hostname }); + }), + }), ); const previous = yield* readPersistedServerRuntimeState(statePath).pipe( diff --git a/apps/server/src/serverRuntimeState.test.ts b/apps/server/src/serverRuntimeState.test.ts index 5a14cf139577e..bdd4a395ff0fc 100644 --- a/apps/server/src/serverRuntimeState.test.ts +++ b/apps/server/src/serverRuntimeState.test.ts @@ -188,7 +188,13 @@ describe("server ownership", () => { ); assert.equal(refused._tag, "ServerAlreadyRunningError"); assert.include(refused.message, "do not delete it while a server is running"); + assert.notInclude(refused.message, "which this machine shares"); assert.equal(yield* fs.readFileString(statePath), before); + // A shared $HOME: the owner recorded another machine; its PID may exist here too. + yield* owner.publish({ ...state, hostname: "host-a" }); + const remote = yield* ServerOwnership.acquireServerOwnership(statePath).pipe(Effect.flip); + assert.include(remote.message, "server on host-a already owns"); + assert.include(remote.message, "Stop it on host-a"); }), ); assert.isFalse(yield* fs.exists(statePath)); diff --git a/apps/server/src/serverRuntimeState.ts b/apps/server/src/serverRuntimeState.ts index 49d9a48f991bc..c80352292328f 100644 --- a/apps/server/src/serverRuntimeState.ts +++ b/apps/server/src/serverRuntimeState.ts @@ -1,3 +1,4 @@ +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; @@ -12,6 +13,8 @@ import { formatHostForUrl, isWildcardHost } from "./startupAccess.ts"; export const PersistedServerRuntimeState = Schema.Struct({ version: Schema.Literal(1), pid: Schema.Int, + // A shared $HOME (NFS) can hold a server from another machine, where `pid` means nothing here. + hostname: Schema.optional(Schema.String), ownerId: Schema.optional(Schema.String), host: Schema.optional(Schema.String), privateNetworkOnly: Schema.optional(Schema.Boolean), @@ -70,9 +73,10 @@ export const makePersistedServerRuntimeState = (input: { readonly serviceManaged?: boolean; readonly managedPolicy?: PersistedServerRuntimeState["managedPolicy"]; }): Effect.Effect => - Effect.map(DateTime.now, (now) => ({ + Effect.map(Effect.all([DateTime.now, HostProcess.Hostname]), ([now, hostname]) => ({ version: 1, pid: process.pid, + hostname, ...(input.config.host ? { host: input.config.host } : {}), ...(input.config.privateNetworkOnly ? { privateNetworkOnly: true } : {}), port: input.port, @@ -128,6 +132,13 @@ export const clearPersistedServerRuntimeState = Effect.fn( ); }); +/** The machine a recorded server runs on, when that is not this one. */ +export const otherMachineOf = ( + state: Pick, + localHostname: string, +): string | undefined => + state.hostname !== undefined && state.hostname !== localHostname ? state.hostname : undefined; + /** Signal 0 only probes existence; EPERM still means alive. */ export const isProcessAlive = (pid: number): boolean => { if (!Number.isInteger(pid) || pid <= 0) return false; diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index 274b86573ed3c..3d427f59d74bc 100644 --- a/apps/server/src/serviceLauncher.test.ts +++ b/apps/server/src/serviceLauncher.test.ts @@ -234,11 +234,17 @@ it.layer(NodeServices.layer)("service state persistence", (it) => { activeVersion: "1.0.0", }), ); + // A shared $HOME: the owner recorded another machine. + yield* fs.makeDirectory(path.join(root, "userdata"), { recursive: true }); + yield* fs.writeFileString( + path.join(root, "userdata", "server-runtime.json"), + JSON.stringify({ hostname: "host-a" }), + ); const refused = 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("server on host-a owns this T3 home")) refused.resolve(); return true; }), ), diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.ts index ee05c63297843..c0896077129a3 100644 --- a/apps/server/src/serviceLauncher.ts +++ b/apps/server/src/serviceLauncher.ts @@ -5,6 +5,7 @@ import * as NodeChildProcess from "node:child_process"; import * as NodeCrypto from "node:crypto"; import * as NodeFS from "node:fs"; import * as NodeFSP from "node:fs/promises"; +import * as NodeOS from "node:os"; import * as NodePath from "node:path"; import * as NodeUtil from "node:util"; @@ -48,6 +49,20 @@ interface ManagedChild { readonly process: NodeChildProcess.ChildProcess; } +// Mirrors serverRuntimeState's otherMachineOf: on a shared $HOME the owner may run elsewhere. +const ownerOnOtherMachine = async (baseDir: string): Promise => { + try { + const record = JSON.parse( + await NodeFSP.readFile(NodePath.join(baseDir, "userdata", "server-runtime.json"), "utf8"), + ) as { readonly hostname?: unknown }; + return typeof record.hostname === "string" && record.hostname !== NodeOS.hostname() + ? record.hostname + : undefined; + } catch { + return undefined; + } +}; + const runtimePaths = (baseDir: string, version: string) => { const versionDir = NodePath.join(baseDir, "runtime", "versions", version); // oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone launcher has no Effect runtime. @@ -800,8 +815,11 @@ export class Launcher { } // Signal listeners alone do not keep Node alive. this.#timer = setInterval(() => {}, 2_147_483_647); + const hostname = await ownerOnOtherMachine(this.#baseDir); process.stderr.write( - "[service-launcher] Another T3 Code server owns this T3 home. Stop it, then restart this service.\n", + hostname === undefined + ? "[service-launcher] Another T3 Code server owns this T3 home. Stop it, then restart this service.\n" + : `[service-launcher] A T3 Code server on ${hostname} owns this T3 home, which this machine shares. Stop it on ${hostname}, then restart this service.\n`, ); }