Skip to content

Commit 271fb8c

Browse files
test(server): reproduce #26775, connects re-read a failing tailscale status per service
Fixes nothing yet: these tests fail on main. Both RemoteOpenTargets and DirectEndpoints spawn their own `tailscale status` per connect, and a later connect waits on a fresh read after a failed one has already settled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7b87466 commit 271fb8c

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

‎apps/server/src/environment/DirectEndpoints.test.ts‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { it as effectIt } from "@effect/vitest";
3+
import * as HostProcess from "@t3tools/shared/HostProcess";
4+
import * as NetService from "@t3tools/shared/Net";
35
import type * as NodeOS from "node:os";
46
import * as Effect from "effect/Effect";
7+
import * as Fiber from "effect/Fiber";
58
import * as Layer from "effect/Layer";
9+
import * as Option from "effect/Option";
610
import * as Sink from "effect/Sink";
711
import * as Stream from "effect/Stream";
812
import * as HttpClient from "effect/http/HttpClient";
913
import * as HttpClientResponse from "effect/http/HttpClientResponse";
14+
import * as ChildProcess from "effect/process/ChildProcess";
1015
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
16+
import * as TestClock from "effect/testing/TestClock";
1117
import { describe, expect, it } from "vite-plus/test";
1218

1319
import * as ServerConfig from "../config.ts";
1420
import * as DirectEndpoints from "./DirectEndpoints.ts";
1521
import { resolveBoundEndpoints } from "./DirectEndpoints.ts";
22+
import * as RemoteOpenTargets from "./RemoteOpenTargets.ts";
1623

1724
const INTERFACES: ReturnType<typeof NodeOS.networkInterfaces> = {
1825
lo0: [
@@ -239,3 +246,93 @@ describe("DirectEndpoints Tailscale Serve", () => {
239246
}),
240247
);
241248
});
249+
250+
// A CLI that cannot reach tailscaled: `tailscale status` never answers, so each read waits out its timeout.
251+
const layerTailscaleHung = (onStatusSpawn: () => void) =>
252+
Layer.succeed(
253+
ChildProcessSpawner.ChildProcessSpawner,
254+
ChildProcessSpawner.make((command) => {
255+
if (ChildProcess.isStandardCommand(command) && command.args[0] === "status") onStatusSpawn();
256+
return Effect.succeed(
257+
ChildProcessSpawner.makeHandle({
258+
pid: ChildProcessSpawner.ProcessId(1),
259+
exitCode: Effect.never,
260+
isRunning: Effect.succeed(true),
261+
kill: () => Effect.void,
262+
unref: Effect.succeed(Effect.void),
263+
stdin: Sink.drain,
264+
stdout: Stream.empty,
265+
stderr: Stream.empty,
266+
all: Stream.empty,
267+
getInputFd: () => Sink.drain,
268+
getOutputFd: () => Stream.empty,
269+
}),
270+
);
271+
}),
272+
);
273+
274+
const layerSshdListening = Layer.succeed(NetService.NetService, {
275+
canListenOnHost: () => Effect.succeed(true),
276+
isPortAvailableOnLoopback: () => Effect.succeed(true),
277+
hasListenerOnHost: () => Effect.succeed(true),
278+
reserveLoopbackPort: () => Effect.succeed(40_000),
279+
findAvailablePort: (preferred) => Effect.succeed(preferred),
280+
});
281+
282+
const MDNS_ONLY = [{ kind: "mdns", host: "bb-1.local", username: "t3-user" }];
283+
284+
// What ws.loadServerConfig resolves for every client that connects.
285+
const connectWithTailscaleHung = (onStatusSpawn: () => void) =>
286+
Effect.gen(function* () {
287+
const remoteOpenTargets = yield* RemoteOpenTargets.RemoteOpenTargets;
288+
const directEndpoints = yield* DirectEndpoints.DirectEndpoints;
289+
return Effect.all([remoteOpenTargets.resolveTargets(), directEndpoints.resolve()], {
290+
concurrency: "unbounded",
291+
}).pipe(
292+
Effect.provideService(HostProcess.Hostname, "bb-1"),
293+
Effect.provideService(HostProcess.Username, "t3-user"),
294+
);
295+
}).pipe(
296+
Effect.provide(
297+
Layer.mergeAll(DirectEndpoints.layer, RemoteOpenTargets.layer).pipe(
298+
Layer.provide(
299+
Layer.mergeAll(
300+
layerServeConfig,
301+
layerTailscaleHung(onStatusSpawn),
302+
layerServeProbe(200),
303+
layerSshdListening,
304+
),
305+
),
306+
),
307+
),
308+
);
309+
310+
describe("Tailscale status shared by DirectEndpoints and RemoteOpenTargets", () => {
311+
effectIt.effect("reads `tailscale status` once per connect, not once per service", () =>
312+
Effect.gen(function* () {
313+
let statusReads = 0;
314+
const connect = yield* connectWithTailscaleHung(() => statusReads++);
315+
const first = yield* Effect.forkChild(connect);
316+
yield* TestClock.adjust("4 seconds");
317+
expect(yield* Fiber.join(first)).toEqual([MDNS_ONLY, []]);
318+
expect(statusReads, "tailscale status spawns for one connect").toBe(1);
319+
}),
320+
);
321+
322+
effectIt.effect(
323+
"answers a later connect from the settled failed read without waiting again",
324+
() =>
325+
Effect.gen(function* () {
326+
const connect = yield* connectWithTailscaleHung(() => {});
327+
const first = yield* Effect.forkChild(connect);
328+
yield* TestClock.adjust("4 seconds");
329+
expect(yield* Fiber.join(first)).toEqual([MDNS_ONLY, []]);
330+
331+
const later = yield* connect.pipe(Effect.timeoutOption("1 second"), Effect.forkChild);
332+
yield* TestClock.adjust("1 second");
333+
expect(yield* Fiber.join(later), "later connect still waiting on tailscale status").toEqual(
334+
Option.some([MDNS_ONLY, []]),
335+
);
336+
}),
337+
);
338+
});

0 commit comments

Comments
 (0)