{"record":{"id":"a21db3cebc7de9de","repo":"denoland/deno","slug":"invalid-port-out-of-range-maybeport","errorCode":null,"errorMessage":"Invalid port (out of range): ${maybePort}","messagePattern":"Invalid port \\(out of range\\): (.+?)","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/net/01_net.js","lineNumber":657,"sourceCode":"  // must always specify a port to connect to, so a missing port is left as-is\n  // and rejected below.\n  if (isServer && (maybePort === null || maybePort === undefined)) {\n    maybePort = 0;\n  }\n  if (typeof maybePort !== \"number\" && typeof maybePort !== \"string\") {\n    throw new TypeError(`Invalid port (expected number): ${maybePort}`);\n  }\n  if (maybePort === \"\") throw new TypeError(\"Invalid port: ''\");\n  const port = Number(maybePort);\n  if (!NumberIsInteger(port)) {\n    if (NumberIsNaN(port) && !NumberIsNaN(maybePort)) {\n      throw new TypeError(`Invalid port: '${maybePort}'`);\n    } else {\n      throw new TypeError(`Invalid port: ${maybePort}`);\n    }\n  } else if (port < (isServer ? 0 : 1) || port > 65535) {\n    // Servers may bind to port 0 (OS-assigned), clients may not connect to it.\n    throw new RangeError(`Invalid port (out of range): ${maybePort}`);\n  }\n  return port;\n}\n\nfunction createListenDatagram(udpOpFn, unixOpFn) {\n  return function listenDatagram(args) {\n    switch (args.transport) {\n      case \"udp\": {\n        const port = validatePort(args.port, true);\n        const { 0: rid, 1: addr } = udpOpFn(\n          {\n            hostname: args.hostname ?? \"0.0.0.0\",\n            port,\n          },\n          args.reuseAddress ?? false,\n          args.loopback ?? false,\n        );\n        addr.transport = \"udp\";","sourceCodeStart":639,"sourceCodeEnd":675,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/net/01_net.js#L639-L675","documentation":"RangeError from validatePort when the port is a valid integer but outside the allowed range. Clients (Deno.connect, Deno.connectTls) must use 1-65535; servers (Deno.listen, Deno.listenTls, listenDatagram) may additionally bind to 0, which asks the OS for an ephemeral port, per the comment 'Servers may bind to port 0 (OS-assigned), clients may not connect to it'. Nothing is dialed or bound when this throws.","triggerScenarios":"Deno.connect({ port: 0 }) (port 0 is only legal for servers); Deno.listen({ port: 65536 }) (off-by-one at the top of the range); negative ports from arithmetic such as port: -1.","commonSituations":"Retry or port-scan loops doing port + 1 past 65535; expecting client-side port 0 to mean OS-assigned like it does for listen; config values parsed with a sign or scale error (e.g. 70000).","solutions":["Clamp and validate: 1-65535 for connect calls, 0-65535 for listen calls","Bound any loop that increments a port: stop at 65535","For an OS-assigned port use Deno.listen({ port: 0 }) and read the real port from listener.addr.port"],"exampleFix":"// before\nfor (let port = 65530; port <= 65540; port++) {\n  await tryPort(port); // 65536..65540 throw RangeError\n}\n\n// after\nfor (let port = 65530; port <= 65535; port++) {\n  await tryPort(port);\n}","handlingStrategy":"validation","validationCode":"function assertPortInRange(port: number, isServer: boolean): void {\n  const min = isServer ? 0 : 1;\n  if (port < min || port > 65535) {\n    throw new RangeError(`port ${port} outside ${min}-65535`);\n  }\n}","typeGuard":"function isUsablePort(port: number, isServer: boolean): port is number {\n  return Number.isInteger(port) && port >= (isServer ? 0 : 1) && port <= 65535;\n}","tryCatchPattern":"try {\n  await Deno.connect({ hostname, port });\n} catch (e) {\n  if (e instanceof RangeError && e.message.includes('out of range')) {\n    return null; // skip this candidate port\n  }\n  throw e;\n}","preventionTips":["Cap port-increment retry loops at 65535","Remember port 0 is only meaningful for listeners (OS-assigned); clients must use 1-65535","Read the assigned ephemeral port from listener.addr.port after listening on 0"],"tags":["deno","network","port","rangeerror"],"backgroundTag":"port-out-of-range","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}