Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop trying other ports given EADDRNOTAVAIL. #5115

Merged
merged 2 commits into from
Oct 12, 2022
Merged
Changes from 1 commit
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
34 changes: 18 additions & 16 deletions src/emulator/portUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { IPV4_UNSPECIFIED, IPV6_UNSPECIFIED, Resolver } from "./dns";
import { Emulators, ListenSpec } from "./types";
import { Constants } from "./constants";
import { EmulatorLogger } from "./emulatorLogger";
import { logger } from "../logger";

// See:
// - https://stackoverflow.com/questions/4313403/why-do-browsers-block-some-ports
Expand Down Expand Up @@ -128,22 +127,10 @@ export async function checkListenable(
dummyServer.once("error", (err) => {
dummyServer.removeAllListeners();
const e = err as Error & { code?: string };
if (
e.code === "EADDRINUSE" ||
e.code === "EACCES" ||
// Where the address is not bindable (not just the port), e.g. in Docker:
// https://github.com/firebase/firebase-tools/issues/4741#issuecomment-1275318134
e.code === "EADDRNOTAVAIL" ||
e.code === "EINVAL"
) {
if (e.code === "EADDRINUSE" || e.code === "EACCES") {
resolve(false);
} else {
// Other unknown issues -- we'll log a warning and return unavailable.
logger.warn(
`portUtils: Error when trying to check port ${addr.port} (on ${addr.address}): ${e.code}`
);
logger.warn(e);
resolve(false);
reject(e);
}
});
dummyServer.once("listening", () => {
Expand Down Expand Up @@ -276,7 +263,22 @@ export async function resolveHostAndAssignPorts(
const addr = addrs[i];
const listen = listenSpec(addr, p);
// This must be done one by one since the addresses may overlap.
if (await checkListenable(listen)) {
let listenable: boolean;
try {
listenable = await checkListenable(listen);
} catch (err) {
emuLogger.logLabeled(
"WARN",
name,
`Error when trying to check port ${p} on ${addr.address}: ${err}`
);
// Even if portFixed is false, don't try other ports since the
// address may be entirely unavailable on all ports (e.g. no IPv6).
// https://github.com/firebase/firebase-tools/issues/4741#issuecomment-1275318134
unavailable.push(addr.address);
continue;
}
if (listenable) {
available.push(listen);
} else {
if (!portFixed) {
Expand Down