Skip to content

Commit da00365

Browse files
lzyyzznlclaude
andcommitted
fix(cli): isPortBusy now probes all 4 address families to catch IPv4-only listeners
Previously, isPortBusy (used by --force port detection) only tried a bare tryListenOnPort which binds IPv6 wildcard (::) by default, missing ports occupied by IPv4-only listeners on 127.0.0.1 or 0.0.0.0. Now probes 127.0.0.1, 0.0.0.0, ::1, :: — same multi-host approach as checkPortInUse in ports-inspect.ts. EADDRNOTAVAIL/EAFNOSUPPORT hosts are silently skipped (e.g. IPv6 not enabled). Fixes #94426 Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent 5b7cf46 commit da00365

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

src/cli/ports.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,23 @@ function killPortWithFuser(port: number, signal: "SIGTERM" | "SIGKILL"): PortPro
130130
}
131131

132132
async function isPortBusy(port: number): Promise<boolean> {
133-
try {
134-
await tryListenOnPort({ port, exclusive: true });
135-
return false;
136-
} catch (err: unknown) {
137-
const code = (err as NodeJS.ErrnoException).code;
138-
if (code === "EADDRINUSE") {
139-
return true;
133+
const hosts = ["127.0.0.1", "0.0.0.0", "::1", "::"];
134+
for (const host of hosts) {
135+
try {
136+
await tryListenOnPort({ port, host, exclusive: true });
137+
} catch (err: unknown) {
138+
const code = (err as NodeJS.ErrnoException).code;
139+
if (code === "EADDRINUSE") {
140+
return true;
141+
}
142+
if (code === "EADDRNOTAVAIL" || code === "EAFNOSUPPORT") {
143+
// Host not available on this system (e.g., IPv6 not enabled), skip
144+
continue;
145+
}
146+
throw err instanceof Error ? err : new Error(String(err));
140147
}
141-
throw err instanceof Error ? err : new Error(String(err));
142148
}
149+
return false;
143150
}
144151

145152
export function parseLsofOutput(output: string): PortProcess[] {

src/cli/program.force.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe("gateway --force helpers", () => {
236236
.mockRejectedValueOnce(busyErr)
237237
.mockRejectedValueOnce(busyErr)
238238
.mockRejectedValueOnce(busyErr)
239-
.mockResolvedValueOnce(undefined);
239+
.mockResolvedValue(undefined);
240240

241241
const promise = forceFreePortAndWait(18789, {
242242
timeoutMs: 300,

0 commit comments

Comments
 (0)