From b55a5260251143ea97804dd69d9f57e030706d85 Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:56:13 +0000 Subject: [PATCH 1/3] fix(server): keep Tailscale Serve on when the startup route read is slow Startup read the current Serve routes with a 3.5 s timeout and, on any failure, left Serve off. A slow tailscaled (the laptop) therefore stranded the host off the tailnet on every restart. The read now allows 8 s per attempt, retries timeouts and nonzero exits with exponential backoff for about 30 s, and on a final failure logs a warning and applies Serve anyway. Model: claude-sonnet-5-5 Task: fix Agent: t3code Serve retry lane (CTO) Harness: t3/claude-code Model-family: Claude Model-generation: 5.5 Skills-SHA: unknown (source unavailable) Co-Authored-By: Claude Sonnet 5.5 --- apps/server/src/server.ts | 36 ++++------ packages/tailscale/src/tailscale.test.ts | 89 +++++++++++++++++++++++- packages/tailscale/src/tailscale.ts | 60 ++++++++++++++-- 3 files changed, 156 insertions(+), 29 deletions(-) diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 1a7895b0cd954..bdb55ff1113b8 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -233,6 +233,7 @@ import * as RelayClient from "@t3tools/shared/relayClient"; import { disableTailscaleServe, ensureTailscaleServe, + ensureTailscaleServeAtStartup, readTailscaleServeRootHolder, readTailscaleServeRoutesTo, readTailscaleStatus, @@ -1014,31 +1015,24 @@ const layerMakeServer = Layer.unwrap( localHost: localHost.replace(/^\[|\]$/gu, ""), servePort: config.tailscaleServePort, }; - const holder = yield* tailscaleServeRootHolder({ - host: route.localHost, - port: localPort, - }).pipe(Effect.orElseSucceed(() => "unreadable" as const)); - if (holder === "other" || holder === "unreadable") { - yield* Effect.logWarning( - holder === "other" - ? "Tailscale Serve left off; its port already serves another server or app" - : "Tailscale Serve left off; its current routes could not be read", - { localPort, servePort: config.tailscaleServePort }, - ); - return route; - } - return yield* ensureTailscaleServe({ + return yield* ensureTailscaleServeAtStartup({ localPort, servePort: config.tailscaleServePort, - localHost, + localHost: route.localHost, + serveHost: localHost, }).pipe( - Effect.as(route), - Effect.tap(() => - Effect.logInfo("Tailscale Serve configured", { - localPort, - servePort: config.tailscaleServePort, - }), + Effect.flatMap((outcome) => + outcome === "occupied" + ? Effect.logWarning( + "Tailscale Serve left off; its port already serves another server or app", + { localPort, servePort: config.tailscaleServePort }, + ) + : Effect.logInfo("Tailscale Serve configured", { + localPort, + servePort: config.tailscaleServePort, + }), ), + Effect.as(route), Effect.catch((cause) => Effect.logWarning( cause._tag === "TailscaleCommandExitError" && diff --git a/packages/tailscale/src/tailscale.test.ts b/packages/tailscale/src/tailscale.test.ts index 095323e037ace..cfd03b4d39db6 100644 --- a/packages/tailscale/src/tailscale.test.ts +++ b/packages/tailscale/src/tailscale.test.ts @@ -11,9 +11,11 @@ import { HttpClient, HttpClientResponse } from "effect/http"; import * as TestClock from "effect/testing/TestClock"; import { ChildProcessSpawner } from "effect/process"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as NetService from "@t3tools/shared/Net"; import { ensureTailscaleServe, + ensureTailscaleServeAtStartup, parseTailscaleExitNode, parseTailscaleServeRootHolders, parseTailscaleServeRoutesTo, @@ -59,10 +61,13 @@ function assertCarriesNoSecret(error: object, secret: string): void { } const tailscaleStatusJson = `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":["100.100.100.100","fd7a:115c:a1e0::1","192.168.1.20"]}}`; -function mockHandle(result: { stdout?: string; stderr?: string; code?: number }) { +function mockHandle(result: { stdout?: string; stderr?: string; code?: number; hang?: boolean }) { return ChildProcessSpawner.makeHandle({ pid: ChildProcessSpawner.ProcessId(1), - exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.code ?? 0)), + exitCode: + result.hang === true + ? Effect.never + : Effect.succeed(ChildProcessSpawner.ExitCode(result.code ?? 0)), isRunning: Effect.succeed(false), kill: () => Effect.void, unref: Effect.succeed(Effect.void), @@ -88,7 +93,7 @@ function layerMockSpawner( handler: ( command: string, args: ReadonlyArray, - ) => { stdout?: string; stderr?: string; code?: number }, + ) => { stdout?: string; stderr?: string; code?: number; hang?: boolean }, ) { return layerSpawner( ChildProcessSpawner.make((command) => { @@ -495,4 +500,82 @@ describe("tailscale", () => { assert.deepEqual(killGraces, [500, 500, 500, 500]); }); }); + + describe("ensureTailscaleServeAtStartup", () => { + const startupInput = { + localPort: 45_678, + servePort: 443, + localHost: "127.0.0.1", + serveHost: "127.0.0.1", + }; + + function serveHarness(serveStatusResults: ReadonlyArray<{ code?: number; hang?: boolean }>) { + const state = { serveStatusReads: 0, serveEnabled: false }; + const layer = Layer.merge( + layerMockSpawner((_command, args) => { + if (args[0] === "status") { + return { stdout: tailscaleStatusJson }; + } + if (args[0] === "serve" && args[1] === "status") { + const result = serveStatusResults[ + Math.min(state.serveStatusReads, serveStatusResults.length - 1) + ] ?? { code: 1 }; + state.serveStatusReads += 1; + return result.hang === true || (result.code ?? 0) !== 0 ? result : { stdout: "null" }; + } + if (args[0] === "serve" && args[1] === "--bg") { + state.serveEnabled = true; + } + return { stdout: "" }; + }), + NetService.layer, + ); + return { state, layer }; + } + + it.effect("enables Serve after the first route read hangs past its timeout", () => { + const { state, layer } = serveHarness([{ hang: true }, {}]); + return Effect.gen(function* () { + const fiber = yield* ensureTailscaleServeAtStartup(startupInput).pipe( + Effect.provide(layer), + Effect.forkChild, + ); + yield* TestClock.adjust(Duration.seconds(10)); + const outcome = yield* Fiber.join(fiber); + assert.strictEqual(outcome, "configured"); + assert.strictEqual(state.serveStatusReads, 2); + assert.isTrue(state.serveEnabled); + }); + }); + + it.effect("enables Serve after several failed route reads", () => { + const { state, layer } = serveHarness([{ code: 1 }, { code: 1 }, { code: 1 }, {}]); + return Effect.gen(function* () { + const fiber = yield* ensureTailscaleServeAtStartup(startupInput).pipe( + Effect.provide(layer), + Effect.forkChild, + ); + yield* TestClock.adjust(Duration.seconds(10)); + const outcome = yield* Fiber.join(fiber); + assert.strictEqual(outcome, "configured"); + assert.strictEqual(state.serveStatusReads, 4); + assert.isTrue(state.serveEnabled); + }); + }); + + it.effect("enables Serve anyway when every route read fails", () => { + const { state, layer } = serveHarness([{ hang: true }]); + return Effect.gen(function* () { + const fiber = yield* ensureTailscaleServeAtStartup(startupInput).pipe( + Effect.provide(layer), + Effect.forkChild, + ); + yield* TestClock.adjust(Duration.seconds(120)); + const outcome = yield* Fiber.join(fiber); + assert.strictEqual(outcome, "configured"); + assert.isAbove(state.serveStatusReads, 2); + assert.isTrue(state.serveEnabled); + }); + }); + }); }); diff --git a/packages/tailscale/src/tailscale.ts b/packages/tailscale/src/tailscale.ts index 0456f95ab4850..eecfc4f252d1d 100644 --- a/packages/tailscale/src/tailscale.ts +++ b/packages/tailscale/src/tailscale.ts @@ -6,6 +6,7 @@ import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; +import * as Schedule from "effect/Schedule"; import * as Stream from "effect/Stream"; import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; import { ChildProcess, ChildProcessSpawner } from "effect/process"; @@ -27,6 +28,8 @@ const TAILSCALE_STATUS_TIMEOUT = Duration.millis(3_500); const TAILSCALE_KILL_GRACE = Duration.millis(500); const TAILSCALE_SERVE_TIMEOUT = Duration.seconds(10); const TAILSCALE_PROBE_TIMEOUT = Duration.millis(2_500); +const TAILSCALE_STARTUP_READ_TIMEOUT = Duration.seconds(8); +const TAILSCALE_STARTUP_READ_RETRY_WINDOW = Duration.seconds(30); const tailscaleCommandForPlatform = (platform: NodeJS.Platform): "tailscale" | "tailscale.exe" => platform === "win32" ? "tailscale.exe" : "tailscale"; @@ -244,6 +247,7 @@ export const parseTailscaleStatus = ( const readTailscaleOutput = Effect.fn("tailscale.readTailscaleOutput")(function* ( subcommand: "status" | "serve" | "debug", args: readonly string[], + timeout: Duration.Input = TAILSCALE_STATUS_TIMEOUT, ) { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const hostPlatform = yield* HostProcess.Platform; @@ -290,13 +294,13 @@ const readTailscaleOutput = Effect.fn("tailscale.readTailscaleOutput")(function* return stdout; }).pipe( Effect.scoped, - Effect.timeout(TAILSCALE_STATUS_TIMEOUT), + Effect.timeout(timeout), Effect.catchTags({ TimeoutError: (cause) => Effect.fail( new TailscaleCommandTimeoutError({ ...commandContext, - timeoutMs: Duration.toMillis(TAILSCALE_STATUS_TIMEOUT), + timeoutMs: Duration.toMillis(Duration.fromInputUnsafe(timeout)), cause, }), ), @@ -730,8 +734,9 @@ export const parseTailscaleServeRootHolders = ( const readTailscaleServeRootHolders = ( servePort: number, target: { readonly host: string; readonly port: number }, + timeout?: Duration.Input, ) => - readTailscaleOutput("serve", ["serve", "status", "--json"]).pipe( + readTailscaleOutput("serve", ["serve", "status", "--json"], timeout).pipe( Effect.flatMap((status) => { const holders = parseTailscaleServeRootHolders(status, servePort, target); return holders === undefined @@ -745,9 +750,13 @@ const readTailscaleServeRootHolders = ( * unclean exit leaves one), or another live server or app, whose route must be left alone. */ export const readTailscaleServeRootHolder = Effect.fn("tailscale.readTailscaleServeRootHolder")( - function* (servePort: number, target: { readonly host: string; readonly port: number }) { + function* ( + servePort: number, + target: { readonly host: string; readonly port: number }, + readTimeout?: Duration.Input, + ) { const net = yield* NetService.NetService; - const holders = yield* readTailscaleServeRootHolders(servePort, target); + const holders = yield* readTailscaleServeRootHolders(servePort, target, readTimeout); if (holders.length === 0) return "none" as const; if (holders.some((holder) => holder.ours)) return "ours" as const; const live = yield* Effect.forEach(holders, (holder) => @@ -759,6 +768,47 @@ export const readTailscaleServeRootHolder = Effect.fn("tailscale.readTailscaleSe }, ); +const isSlowTailscaledError = (error: { readonly _tag: string }) => + error._tag === "TailscaleCommandTimeoutError" || error._tag === "TailscaleCommandExitError"; + +export const ensureTailscaleServeAtStartup = Effect.fn("tailscale.ensureTailscaleServeAtStartup")( + function* (input: { + readonly localPort: number; + readonly servePort: number; + readonly localHost: string; + readonly serveHost: string; + }) { + const holder = yield* readTailscaleServeRootHolder( + input.servePort, + { host: input.localHost, port: input.localPort }, + TAILSCALE_STARTUP_READ_TIMEOUT, + ).pipe( + Effect.retry({ + while: isSlowTailscaledError, + schedule: Schedule.exponential(Duration.seconds(1)).pipe( + Schedule.upTo({ duration: TAILSCALE_STARTUP_READ_RETRY_WINDOW }), + ), + }), + Effect.catch((cause) => + Effect.logWarning("Tailscale Serve routes could not be read; configuring Serve anyway", { + cause, + localPort: input.localPort, + servePort: input.servePort, + }).pipe(Effect.as("unreadable" as const)), + ), + ); + if (holder === "other") { + return "occupied" as const; + } + yield* ensureTailscaleServe({ + localPort: input.localPort, + servePort: input.servePort, + localHost: input.serveHost, + }); + return "configured" as const; + }, +); + export const probeTailscaleHttpsEndpoint = Effect.fn("tailscale.probeTailscaleHttpsEndpoint")( function* (input: { readonly baseUrl: string; From 54955831ced1c25c3a2f8f14ff4fbb8b5f2a4fae Mon Sep 17 00:00:00 2001 From: Kameron Smith Date: Fri, 20 Nov 2026 05:34:17 -0500 Subject: [PATCH 2/3] fix(server): let shutdown interrupt the Serve startup retry and never apply over unparseable routes The startup Serve read and its retry window ran inside an uninterruptible acquire, so a stop requested during a slow tailscaled waited about 40 s and was force-killed before cleanup. The acquire is now interruptible. A status read that exits 0 with output the parser cannot decode now leaves Serve off instead of applying it blind; only timeouts and nonzero exits fall back to applying Serve. Linear: https://linear.app/closura/issue/CLO-1006 Model: claude-opus-5-5 Task: fix Agent: CTO standing thread Harness: t3/claude-code Model-family: Claude Model-generation: 5.5 Skills-SHA: unknown (source unavailable) Co-Authored-By: Claude Opus 5.5 --- apps/server/src/server.ts | 5 ++++- packages/tailscale/src/tailscale.test.ts | 20 ++++++++++++++++++-- packages/tailscale/src/tailscale.ts | 19 ++++++++++++++----- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index bdb55ff1113b8..a4f27a469c530 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -1027,7 +1027,9 @@ const layerMakeServer = Layer.unwrap( "Tailscale Serve left off; its port already serves another server or app", { localPort, servePort: config.tailscaleServePort }, ) - : Effect.logInfo("Tailscale Serve configured", { + : outcome === "unknown" + ? Effect.void + : Effect.logInfo("Tailscale Serve configured", { localPort, servePort: config.tailscaleServePort, }), @@ -1093,6 +1095,7 @@ const layerMakeServer = Layer.unwrap( ), ) : Effect.void, + { interruptible: true }, ).pipe( // Startup is not the only time the route can go stale, so whoever finds it on a dead // port takes it back. diff --git a/packages/tailscale/src/tailscale.test.ts b/packages/tailscale/src/tailscale.test.ts index cfd03b4d39db6..3ae33dc62a84a 100644 --- a/packages/tailscale/src/tailscale.test.ts +++ b/packages/tailscale/src/tailscale.test.ts @@ -509,7 +509,9 @@ describe("tailscale", () => { serveHost: "127.0.0.1", }; - function serveHarness(serveStatusResults: ReadonlyArray<{ code?: number; hang?: boolean }>) { + function serveHarness( + serveStatusResults: ReadonlyArray<{ code?: number; hang?: boolean; stdout?: string }>, + ) { const state = { serveStatusReads: 0, serveEnabled: false }; const layer = Layer.merge( layerMockSpawner((_command, args) => { @@ -521,7 +523,9 @@ describe("tailscale", () => { Math.min(state.serveStatusReads, serveStatusResults.length - 1) ] ?? { code: 1 }; state.serveStatusReads += 1; - return result.hang === true || (result.code ?? 0) !== 0 ? result : { stdout: "null" }; + return result.hang === true || (result.code ?? 0) !== 0 + ? result + : { stdout: result.stdout ?? "null" }; } if (args[0] === "serve" && args[1] === "--bg") { state.serveEnabled = true; @@ -577,5 +581,17 @@ describe("tailscale", () => { assert.isTrue(state.serveEnabled); }); }); + + it.effect("leaves Serve off when the route read succeeds with output it cannot parse", () => { + const { state, layer } = serveHarness([{ stdout: "not json" }]); + return Effect.gen(function* () { + const outcome = yield* ensureTailscaleServeAtStartup(startupInput).pipe( + Effect.provide(layer), + ); + assert.strictEqual(outcome, "unknown"); + assert.strictEqual(state.serveStatusReads, 1); + assert.isFalse(state.serveEnabled); + }); + }); }); }); diff --git a/packages/tailscale/src/tailscale.ts b/packages/tailscale/src/tailscale.ts index eecfc4f252d1d..51b234d2efb45 100644 --- a/packages/tailscale/src/tailscale.ts +++ b/packages/tailscale/src/tailscale.ts @@ -790,16 +790,25 @@ export const ensureTailscaleServeAtStartup = Effect.fn("tailscale.ensureTailscal ), }), Effect.catch((cause) => - Effect.logWarning("Tailscale Serve routes could not be read; configuring Serve anyway", { - cause, - localPort: input.localPort, - servePort: input.servePort, - }).pipe(Effect.as("unreadable" as const)), + isSlowTailscaledError(cause) + ? Effect.logWarning("Tailscale Serve routes could not be read; configuring Serve anyway", { + cause, + localPort: input.localPort, + servePort: input.servePort, + }).pipe(Effect.as("unreadable" as const)) + : Effect.logWarning("Tailscale Serve routes could not be understood; leaving Serve off", { + cause, + localPort: input.localPort, + servePort: input.servePort, + }).pipe(Effect.as("unknown" as const)), ), ); if (holder === "other") { return "occupied" as const; } + if (holder === "unknown") { + return "unknown" as const; + } yield* ensureTailscaleServe({ localPort: input.localPort, servePort: input.servePort, From ac81ee36a193ece57a6c0fc45cb4aa9925664c07 Mon Sep 17 00:00:00 2001 From: Kameron Smith Date: Fri, 20 Nov 2026 05:37:44 -0500 Subject: [PATCH 3/3] style(server): format the Serve startup outcome handling Re-indents the nested ternary for the formatter and shortens the two startup warnings so they fit the line width; the "leaving Serve off" warning now says the routes could not be checked, which also covers spawn failures. Linear: https://linear.app/closura/issue/CLO-1006 Model: claude-opus-5-5 Task: fix Agent: CTO standing thread Harness: t3/claude-code Model-family: Claude Model-generation: 5.5 Skills-SHA: unknown (source unavailable) Co-Authored-By: Claude Opus 5.5 --- apps/server/src/server.ts | 6 +++--- packages/tailscale/src/tailscale.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index a4f27a469c530..02d2e5a3fbedb 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -1030,9 +1030,9 @@ const layerMakeServer = Layer.unwrap( : outcome === "unknown" ? Effect.void : Effect.logInfo("Tailscale Serve configured", { - localPort, - servePort: config.tailscaleServePort, - }), + localPort, + servePort: config.tailscaleServePort, + }), ), Effect.as(route), Effect.catch((cause) => diff --git a/packages/tailscale/src/tailscale.ts b/packages/tailscale/src/tailscale.ts index 51b234d2efb45..5f991da4492af 100644 --- a/packages/tailscale/src/tailscale.ts +++ b/packages/tailscale/src/tailscale.ts @@ -791,12 +791,12 @@ export const ensureTailscaleServeAtStartup = Effect.fn("tailscale.ensureTailscal }), Effect.catch((cause) => isSlowTailscaledError(cause) - ? Effect.logWarning("Tailscale Serve routes could not be read; configuring Serve anyway", { + ? Effect.logWarning("Tailscale Serve routes could not be read; applying Serve anyway", { cause, localPort: input.localPort, servePort: input.servePort, }).pipe(Effect.as("unreadable" as const)) - : Effect.logWarning("Tailscale Serve routes could not be understood; leaving Serve off", { + : Effect.logWarning("Tailscale Serve routes could not be checked; leaving Serve off", { cause, localPort: input.localPort, servePort: input.servePort,