The best way to control Claude Code, Codex and more from any computer or phone
fix(server): Tailscale Serve no longer takes a live server's route and repairs one left on a dead port (#35042)
Julius Marminge committed
Nov 12, 2026 at 02:30 UTC
becc7d3510ce82c4b58a8002a2b358a45f579d1f
3 files changed
+161
-7
apps/server/src/server.ts
+72
-6
@@ -217,6 +217,7 @@ import * as RelayClient from "@t3tools/shared/relayClient";
217
import {
218
disableTailscaleServe,
219
ensureTailscaleServe,
220
+ readTailscaleServeRootHolders,
221
readTailscaleServeRoutesTo,
222
readTailscaleStatus,
223
} from "@t3tools/tailscale";
@@ -904,6 +905,22 @@ const layerMakeServer = Layer.unwrap(
905
);
906
}),
907
);
908
+ // Who holds the Serve port's root: nobody, this server, only dead loopback ports (an unclean
909
+ // exit leaves one), or another live server or app, whose route is left alone.
910
+ const tailscaleServeRootHolder = Effect.fn("server.tailscaleServeRootHolder")(
911
+ function* (target: { readonly host: string; readonly port: number }) {
912
+ const net = yield* NetService.NetService;
913
+ const holders = yield* readTailscaleServeRootHolders(config.tailscaleServePort, target);
914
+ if (holders.length === 0) return "none" as const;
915
+ if (holders.some((holder) => holder.ours)) return "ours" as const;
916
+ const live = yield* Effect.forEach(holders, (holder) =>
917
+ holder.ours || holder.loopback === undefined
918
+ ? Effect.succeed(true)
919
+ : net.hasListenerOnHost(holder.loopback.port, holder.loopback.host),
920
+ );
921
+ return live.includes(true) ? ("other" as const) : ("dead" as const);
922
+ },
923
+ );
924
const layerTailscaleServe = config.tailscaleServeEnabled
925
? Layer.effectDiscard(
926
Effect.acquireRelease(
@@ -918,16 +935,31 @@ const layerMakeServer = Layer.unwrap(
935
936
const localPort = address.port;
937
const localHost = loopbackUrlHostForAddress(address);
938
+ const route = {
939
+ localPort,
940
+ urlHost: localHost,
941
+ localHost: localHost.replace(/^\[|\]$/gu, ""),
942
+ servePort: config.tailscaleServePort,
943
+ };
944
+ const holder = yield* tailscaleServeRootHolder({
945
+ host: route.localHost,
946
+ port: localPort,
947
+ }).pipe(Effect.orElseSucceed(() => "unreadable" as const));
948
+ if (holder === "other" || holder === "unreadable") {
949
+ yield* Effect.logWarning(
950
+ holder === "other"
951
+ ? "Tailscale Serve left off; its port already serves another server or app"
952
+ : "Tailscale Serve left off; its current routes could not be read",
953
+ { localPort, servePort: config.tailscaleServePort },
954
+ );
955
+ return route;
956
+ }
957
return yield* ensureTailscaleServe({
958
localPort,
959
servePort: config.tailscaleServePort,
960
localHost,
961
}).pipe(
926
- Effect.as({
927
- localPort,
928
- localHost: localHost.replace(/^\[|\]$/gu, ""),
929
- servePort: config.tailscaleServePort,
930
- }),
962
+ Effect.as(route),
963
Effect.tap(() =>
964
Effect.logInfo("Tailscale Serve configured", {
965
localPort,
@@ -992,8 +1024,42 @@ const layerMakeServer = Layer.unwrap(
1024
),
1025
)
1026
: Effect.void,
1027
+ ).pipe(
1028
+ // Startup is not the only time the route can go stale, so whoever finds it on a dead
1029
+ // port takes it back.
1030
+ Effect.tap((configured) =>
1031
+ configured === null
1032
+ ? Effect.void
1033
+ : Effect.sleep("30 seconds").pipe(
1034
+ Effect.andThen(
1035
+ tailscaleServeRootHolder({
1036
+ host: configured.localHost,
1037
+ port: configured.localPort,
1038
+ }),
1039
+ ),
1040
+ Effect.flatMap((holder) =>
1041
+ holder === "dead"
1042
+ ? ensureTailscaleServe({
1043
+ localPort: configured.localPort,
1044
+ servePort: configured.servePort,
1045
+ localHost: configured.urlHost,
1046
+ }).pipe(
1047
+ Effect.andThen(
1048
+ Effect.logInfo("Tailscale Serve repaired", {
1049
+ localPort: configured.localPort,
1050
+ servePort: configured.servePort,
1051
+ }),
1052
+ ),
1053
+ )
1054
+ : Effect.void,
1055
+ ),
1056
+ Effect.ignore,
1057
+ Effect.forever,
1058
+ Effect.forkScoped,
1059
+ ),
1060
+ ),
1061
),
996
- )
1062
+ ).pipe(Layer.provide(NetService.layer))
1063
: Layer.empty;
1064
const layerCloudDesiredLinkReconcile = Layer.effectDiscard(
1065
Effect.gen(function* () {
packages/tailscale/src/tailscale.test.ts
+34
@@ -15,6 +15,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
15
import {
16
ensureTailscaleServe,
17
parseTailscaleExitNode,
18
+ parseTailscaleServeRootHolders,
19
parseTailscaleServeRoutesTo,
20
parseTailscaleSshEnabled,
21
parseTailscaleStatus,
@@ -217,6 +218,39 @@ describe("tailscale", () => {
218
assert.isUndefined(parseTailscaleServeRoutesTo("not json", { host: "127.0.0.1", port: 3773 }));
219
});
220
221
+ it("finds what holds a Serve port's root", () => {
222
+ const target = { host: "127.0.0.1", port: 3773 };
223
+ const status = JSON.stringify({
224
+ TCP: { "8443": { TCPForward: "127.0.0.1:22" } },
225
+ Web: {
226
+ "box.tailnet.ts.net:443": {
227
+ Handlers: { "/": { Proxy: "http://127.0.0.1:3773" }, "/x": { Proxy: "9000" } },
228
+ },
229
+ "old.tailnet.ts.net:443": { Handlers: { "/": { Proxy: "http://[::1]:4000" } } },
230
+ "box.tailnet.ts.net:10000": { Handlers: { "/": { Proxy: "http://127.0.0.1:4001" } } },
231
+ "box.tailnet.ts.net:10001": { Handlers: { "/": { Proxy: "http://10.0.0.5:4002" } } },
232
+ "box.tailnet.ts.net:10002": { Handlers: { "/": { Text: "hi" } } },
233
+ },
234
+ Services: {
235
+ "svc:t3": {
236
+ Web: { "t3.tailnet.ts.net:10000": { Handlers: { "/": { Proxy: "4003" } } } },
237
+ },
238
+ },
239
+ });
240
+ assert.deepEqual(parseTailscaleServeRootHolders(status, 443, target), [
241
+ { ours: true },
242
+ { ours: false, loopback: { host: "::1", port: 4000 } },
243
+ ]);
244
+ assert.deepEqual(parseTailscaleServeRootHolders(status, 10000, target), [
245
+ { ours: false, loopback: { host: "127.0.0.1", port: 4001 } },
246
+ ]);
247
+ assert.deepEqual(parseTailscaleServeRootHolders(status, 10001, target), [{ ours: false }]);
248
+ assert.deepEqual(parseTailscaleServeRootHolders(status, 10002, target), [{ ours: false }]);
249
+ assert.deepEqual(parseTailscaleServeRootHolders(status, 8443, target), [{ ours: false }]);
250
+ assert.deepEqual(parseTailscaleServeRootHolders("null", 443, target), []);
251
+ assert.isUndefined(parseTailscaleServeRootHolders("not json", 443, target));
252
+ });
253
+
254
it.effect("parses status facts", () =>
255
Effect.gen(function* () {
256
const status = yield* parseTailscaleStatus(tailscaleStatusJson);
packages/tailscale/src/tailscale.ts
+55
-1
@@ -1,6 +1,6 @@
1
import { ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
2
import * as HostProcess from "@t3tools/shared/HostProcess";
3
-import { normalizeHostname } from "@t3tools/shared/hostClassification";
3
+import { isLocalLoopbackHost, normalizeHostname } from "@t3tools/shared/hostClassification";
4
import * as Duration from "effect/Duration";
5
import * as Effect from "effect/Effect";
6
import * as Option from "effect/Option";
@@ -656,6 +656,60 @@ export const readTailscaleServeRoutesTo = (target: {
656
}),
657
);
658
659
+/** A handler at the root of a Serve port: this server's, or another with the loopback port it dials. */
660
+export type TailscaleServeRootHolder =
661
+ | { readonly ours: true }
662
+ | { readonly ours: false; readonly loopback?: { readonly host: string; readonly port: number } };
663
+
664
+/**
665
+ * What answers at the root of `servePort` outside any Tailscale Service, where
666
+ * `ensureTailscaleServe` publishes, or undefined when the status cannot be read.
667
+ */
668
+export const parseTailscaleServeRootHolders = (
669
+ rawServeStatusJson: string,
670
+ servePort: number,
671
+ target: { readonly host: string; readonly port: number },
672
+): ReadonlyArray<TailscaleServeRootHolder> | undefined => {
673
+ const status = decodeTailscaleServeStatusJson(rawServeStatusJson);
674
+ if (Option.isNone(status)) return undefined;
675
+ const config = status.value ?? undefined;
676
+ const tcp: ReadonlyArray<TailscaleServeRootHolder> =
677
+ config?.TCP?.[String(servePort)]?.TCPForward === undefined ? [] : [{ ours: false }];
678
+ const web = Object.entries(config?.Web ?? {}).flatMap(
679
+ ([hostPort, site]): ReadonlyArray<TailscaleServeRootHolder> => {
680
+ const root = site.Handlers?.["/"];
681
+ if (Number(hostPort.slice(hostPort.lastIndexOf(":") + 1)) !== servePort || root === undefined)
682
+ return [];
683
+ if (root.Proxy === undefined) return [{ ours: false }];
684
+ const url = expandProxy(root.Proxy);
685
+ if (reachesTarget(url, target) !== undefined) return [{ ours: true }];
686
+ const dialed = dialedHostPort(url);
687
+ return [
688
+ dialed !== undefined && isLocalLoopbackHost(dialed.host)
689
+ ? {
690
+ ours: false,
691
+ loopback: { host: normalizeHostname(dialed.host), port: Number(dialed.port) },
692
+ }
693
+ : { ours: false },
694
+ ];
695
+ },
696
+ );
697
+ return [...tcp, ...web];
698
+};
699
+
700
+export const readTailscaleServeRootHolders = (
701
+ servePort: number,
702
+ target: { readonly host: string; readonly port: number },
703
+) =>
704
+ readTailscaleOutput("serve", ["serve", "status", "--json"]).pipe(
705
+ Effect.flatMap((status) => {
706
+ const holders = parseTailscaleServeRootHolders(status, servePort, target);
707
+ return holders === undefined
708
+ ? Effect.fail(new TailscaleStatusParseError({ cause: "unreadable serve status" }))
709
+ : Effect.succeed(holders);
710
+ }),
711
+ );
712
+
713
export const probeTailscaleHttpsEndpoint = Effect.fn("tailscale.probeTailscaleHttpsEndpoint")(
714
function* (input: { readonly baseUrl: string; readonly timeout?: Duration.Input }) {
715
const client = yield* HttpClient.HttpClient;