Skip to content

Commit 74904e3

Browse files
mr-joomclaudejuliusmarminge
authored
fix(shared): port checks no longer fail on hosts with IPv6 disabled (pingdotgg#15044)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent b92ebce commit 74904e3

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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

@@ -54,6 +55,33 @@ it.layer(NetService.layer)("NetService", (it) => {
5455
}),
5556
);
5657

58+
it.effect.each(["EAFNOSUPPORT", "EADDRNOTAVAIL"])(
59+
"keeps ports available when IPv6 binds fail with %s",
60+
(code) =>
61+
Effect.gen(function* () {
62+
const net = yield* NetService.NetService;
63+
const port = yield* net.reserveLoopbackPort();
64+
const listen = NodeNet.Server.prototype.listen;
65+
const spy = vi.spyOn(NodeNet.Server.prototype, "listen").mockImplementation(function (
66+
this: NodeNet.Server,
67+
...args
68+
) {
69+
const [options] = args;
70+
if (options?.host === "::1" || options?.host === "::") {
71+
process.nextTick(() => this.emit("error", Object.assign(new Error(code), { code })));
72+
return this;
73+
}
74+
return listen.apply(this, args);
75+
});
76+
try {
77+
assert.equal(yield* net.isPortAvailableOnLoopback(port), true);
78+
assert.equal(yield* net.canListenOnHost(port, "::"), true);
79+
} finally {
80+
spy.mockRestore();
81+
}
82+
}),
83+
);
84+
5785
it.effect("isPortAvailableOnLoopback reports false for an occupied port", () =>
5886
Effect.acquireUseRelease(
5987
openServer("127.0.0.1"),

‎packages/shared/src/Net.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ export class NetService extends Context.Service<NetService, NetServiceShape>()(
4444

4545
/** @public Service construction is part of the canonical Effect module API. */
4646
export const make = () => {
47-
/**
48-
* `EADDRNOTAVAIL` is treated as available so IPv6-absent hosts don't fail
49-
* loopback availability checks.
50-
*/
5147
const canListenOnHost = (port: number, host: string): Effect.Effect<boolean> =>
5248
Effect.callback<boolean>((resume) => {
5349
const server = NodeNet.createServer();
@@ -62,7 +58,11 @@ export const make = () => {
6258
server.unref();
6359

6460
server.once("error", (cause) => {
65-
if (isErrnoExceptionWithCode(cause) && cause.code === "EADDRNOTAVAIL") {
61+
// Missing IPv6 addresses or kernel support do not mean the port is occupied.
62+
if (
63+
isErrnoExceptionWithCode(cause) &&
64+
(cause.code === "EADDRNOTAVAIL" || cause.code === "EAFNOSUPPORT")
65+
) {
6666
settle(true);
6767
return;
6868
}

0 commit comments

Comments
 (0)