Skip to content

Commit 935a294

Browse files
mr-joomclaude
andcommitted
fix(shared): port checks no longer fail on hosts with IPv6 disabled
On a kernel booted with `ipv6.disable=1`, binding `::` or `::1` fails with EAFNOSUPPORT. canListenOnHost only treated EADDRNOTAVAIL as "available", so every port looked taken: the desktop app scanned 3773-65535 and quit with DesktopBackendPortUnavailableError. Fixes #15038 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c5a929e commit 935a294

2 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎packages/shared/src/Net.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,61 @@ import * as NodeNet from "node:net";
22

33
import { assert, describe, it } from "@effect/vitest";
44
import * as Effect from "effect/Effect";
5+
import { vi } from "vite-plus/test";
56

67
import * as NetService from "./Net.ts";
78

9+
const listenInterceptor = vi.hoisted(() => ({
10+
failHost: null as string | null,
11+
errorCode: "EAFNOSUPPORT",
12+
}));
13+
14+
// Fails `listen({ host })` for one host with a chosen errno, e.g. EAFNOSUPPORT
15+
// for `::` on a kernel booted with `ipv6.disable=1`, which CI hosts are not.
16+
vi.mock("node:net", async (importOriginal) => {
17+
const actual = await importOriginal<typeof import("node:net")>();
18+
return {
19+
...actual,
20+
createServer: (...args: Parameters<typeof actual.createServer>) => {
21+
const server = actual.createServer(...args);
22+
const listen = server.listen.bind(server) as (...a: Array<unknown>) => NodeNet.Server;
23+
server.listen = ((...listenArgs: Array<unknown>) => {
24+
const [options] = listenArgs;
25+
if (
26+
listenInterceptor.failHost !== null &&
27+
typeof options === "object" &&
28+
options !== null &&
29+
(options as NodeNet.ListenOptions).host === listenInterceptor.failHost
30+
) {
31+
const error = new Error(`listen ${listenInterceptor.errorCode}`);
32+
Object.assign(error, { code: listenInterceptor.errorCode });
33+
process.nextTick(() => server.emit("error", error));
34+
return server;
35+
}
36+
return listen(...listenArgs);
37+
}) as typeof server.listen;
38+
return server;
39+
},
40+
};
41+
});
42+
43+
const withListenError = <A, E, R>(
44+
host: string,
45+
errorCode: string,
46+
effect: Effect.Effect<A, E, R>,
47+
): Effect.Effect<A, E, R> =>
48+
Effect.sync(() => {
49+
listenInterceptor.failHost = host;
50+
listenInterceptor.errorCode = errorCode;
51+
}).pipe(
52+
Effect.andThen(effect),
53+
Effect.ensuring(
54+
Effect.sync(() => {
55+
listenInterceptor.failHost = null;
56+
}),
57+
),
58+
);
59+
860
const closeServer = (server: NodeNet.Server) =>
961
Effect.sync(() => {
1062
try {
@@ -56,6 +108,30 @@ it.layer(NetService.layer)("NetService", (it) => {
56108
}),
57109
);
58110

111+
it.effect("canListenOnHost treats a host without IPv6 support as available", () =>
112+
Effect.gen(function* () {
113+
const net = yield* NetService.NetService;
114+
const port = yield* net.reserveLoopbackPort();
115+
116+
const available = yield* withListenError(
117+
"::",
118+
"EAFNOSUPPORT",
119+
net.canListenOnHost(port, "::"),
120+
);
121+
assert.equal(available, true);
122+
}),
123+
);
124+
125+
it.effect("canListenOnHost reports other bind errors as unavailable", () =>
126+
Effect.gen(function* () {
127+
const net = yield* NetService.NetService;
128+
const port = yield* net.reserveLoopbackPort();
129+
130+
const available = yield* withListenError("::", "EACCES", net.canListenOnHost(port, "::"));
131+
assert.equal(available, false);
132+
}),
133+
);
134+
59135
it.effect("isPortAvailableOnLoopback reports false for an occupied port", () =>
60136
Effect.acquireUseRelease(
61137
openServer("127.0.0.1"),

‎packages/shared/src/Net.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ export class NetService extends Context.Service<NetService, NetServiceShape>()(
6767
export const make = () => {
6868
/**
6969
* Returns true when a TCP server can bind to {host, port}.
70-
* `EADDRNOTAVAIL` is treated as available so IPv6-absent hosts don't fail
71-
* loopback availability checks.
70+
* `EADDRNOTAVAIL` (no such address, e.g. no `::1`) and `EAFNOSUPPORT` (no
71+
* IPv6 in the kernel, e.g. `ipv6.disable=1`) are treated as available so
72+
* IPv6-absent hosts don't fail availability checks.
7273
*/
7374
const canListenOnHost = (port: number, host: string): Effect.Effect<boolean> =>
7475
Effect.callback<boolean>((resume) => {
@@ -84,7 +85,10 @@ export const make = () => {
8485
server.unref();
8586

8687
server.once("error", (cause) => {
87-
if (isErrnoExceptionWithCode(cause) && cause.code === "EADDRNOTAVAIL") {
88+
if (
89+
isErrnoExceptionWithCode(cause) &&
90+
(cause.code === "EADDRNOTAVAIL" || cause.code === "EAFNOSUPPORT")
91+
) {
8892
settle(true);
8993
return;
9094
}

0 commit comments

Comments
 (0)