INNER CODE UNIT · TypeScript
chooseListenPort
lidge-jun/opencodex · src/cli/index.ts:267
async function chooseListenPort(
requestedPort?: number,
options: { sibling?: boolean } = {},
): Promise<number> {
const config = loadConfig();
const preferred = requestedPort ?? config.port ?? 10100;
const hardPin = requestedPort !== undefined && requestedPort > 0;
// Only an EXPLICITLY ported listener reserves a port. The companion form (`{enabled:true}`
// with no port) deliberately shares the public port on 127.0.0.1, so treating it as a
// reservation — via `effectiveLoopbackListenerPort` — would refuse every start on a
// one-port hub and hop the public listener onto an ephemeral port (#4236).
const reservedLoopbackPort = config.unauthenticatedLoopbackListener?.enabled
? config.unauthenticatedLoopbackListener.port
: undefined;
// Before the reclaim path, not after (#1102). Asking for the port the loopback listener is
// configured to bind is a configuration mistake, and reclaim would spend up to 60 seconds
// waiting for a socket to free before reporting "port is busy" — the wrong diagnosis for a
// collision the config can state outright.