Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/port-owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ export const HOLDER_PROBE_TIMEOUT_MS = 2_000;
/** Wildcard bind hosts are probed through loopback instead. */
export function holderProbeOrigin(host: string, port: number): string {
const h = host === "0.0.0.0" || host === "::" ? "127.0.0.1" : host;
return `http://${h}:${port}`;
const authority = h.includes(":") && !h.startsWith("[") ? `[${h}]` : h;
return `http://${authority}:${port}`;
}

/** Pure hint composition for the terminal EADDRINUSE log line — the probe
Expand Down
34 changes: 34 additions & 0 deletions tests/port-owner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import http from "node:http";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { Agent, fetch as directFetch } from "undici";
import { findForeignOwner, holderHintFragment, holderProbeOrigin, mayClobberPointer, probeHolderForHint, probeOwnerInfo, resolvePortClaim, type HealthFetcher } from "../src/port-owner.ts";
import { atomicWriteInstanceFile, isPidAlive, readProxyInstanceFile, type ProxyInstanceFile } from "../src/instance.ts";
import { rmrf } from "./tmp-rm.ts";
Expand Down Expand Up @@ -266,6 +267,39 @@ test("holderProbeOrigin: wildcard binds probe through loopback (#4762)", () => {
assert.equal(holderProbeOrigin("::", 18789), "http://127.0.0.1:18789");
});

test("holderProbeOrigin: IPv6 literals remain valid URL authorities", () => {
assert.equal(holderProbeOrigin("::1", 18789), "http://[::1]:18789");
assert.equal(holderProbeOrigin("2001:db8::1", 18789), "http://[2001:db8::1]:18789");
assert.equal(holderProbeOrigin("[::1]", 18789), "http://[::1]:18789");
assert.equal(holderProbeOrigin("localhost", 18789), "http://localhost:18789");
});

test("probeHolderForHint: a healthy IPv6 listener is not diagnosed as wedged", async () => {
const server = http.createServer((req, res) => {
assert.equal(req.url, "/__bili/health");
res.setHeader("content-type", "application/json");
res.end(JSON.stringify({ ok: true, pid: FOREIGN_PID, instanceId: "ipv6-holder" }));
});
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(0, "::1", resolve);
});
const port = (server.address() as { port: number }).port;
const dispatcher = new Agent();
try {
const hint = await probeHolderForHint({
origin: holderProbeOrigin("::1", port),
fetchImpl: (url, init) => directFetch(url, { signal: init?.signal, dispatcher }),
});
assert.match(hint, /answered \/__bili\/health \(pid 4242424\)/);
assert.doesNotMatch(hint, /wedged or dead|kill it/);
} finally {
await dispatcher.close();
server.closeAllConnections();
await new Promise<void>((resolve) => server.close(() => resolve()));
}
});

test("holderHintFragment: serving vs wedged/dead wording, with and without a pid (#4762)", () => {
const serving = holderHintFragment(true, 4242);
assert.match(serving, /answered \/__bili\/health \(pid 4242\)/);
Expand Down
Loading