{"record":{"id":"f3afba85de7da502","repo":"nodejs/node","slug":"und-err-max-origins-reached","errorCode":"UND_ERR_MAX_ORIGINS_REACHED","errorMessage":"Maximum allowed origins reached","messagePattern":"Maximum allowed origins reached","errorType":"exception","errorClass":"MaxOriginsReachedError","httpStatus":null,"severity":"error","filePath":"deps/undici/src/lib/dispatcher/agent.js","lineNumber":86,"sourceCode":"    for (const dispatcher of this[kClients].values()) {\n      ret += dispatcher[kRunning]\n    }\n    return ret\n  }\n\n  [kDispatch] (opts, handler) {\n    let origin\n    if (opts.origin && (typeof opts.origin === 'string' || opts.origin instanceof URL)) {\n      origin = String(opts.origin)\n    } else {\n      throw new InvalidArgumentError('opts.origin must be a non-empty string or URL.')\n    }\n\n    const allowH2 = opts.allowH2 ?? this[kOptions].allowH2\n    const key = allowH2 === false ? `${origin}#http1-only` : origin\n\n    if (this[kOrigins].size >= this[kOptions].maxOrigins && !this[kOrigins].has(origin)) {\n      throw new MaxOriginsReachedError()\n    }\n\n    let dispatcher = this[kClients].get(key)\n    if (!dispatcher) {\n      dispatcher = this[kFactory](opts.origin, allowH2 === false\n        ? { ...this[kOptions], allowH2: false }\n        : this[kOptions])\n\n      const closeClientIfUnused = () => {\n        if (this[kClients].get(key) !== dispatcher) {\n          return\n        }\n\n        if (dispatcher[kConnected] > 0 || dispatcher[kBusy]) {\n          return\n        }\n\n        this[kClients].delete(key)","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/undici/src/lib/dispatcher/agent.js#L68-L104","documentation":"Thrown inside Agent's [kDispatch] as a MaxOriginsReachedError (code UND_ERR_MAX_ORIGINS_REACHED). The check is: this[kOrigins].size >= this[kOptions].maxOrigins AND the current origin is not already tracked. The Agent refuses to silently evict or grow beyond the configured cap, so a brand-new origin past the limit is rejected outright.","triggerScenarios":"new Agent({ maxOrigins: 3 }) then dispatching to four DISTINCT origins (the fourth, never-seen-before origin triggers the throw); a crawler hitting many hosts through a capped Agent.","commonSituations":"A polyglot client pointed at many microservices through one low-capped Agent; raising traffic to include more origins than the configured cap; mis-tuning maxOrigins down for memory and then seeing new hosts fail.","solutions":["Raise maxOrigins to cover the expected origin cardinality (or omit it for Infinity).","Pool repeated origins so they count once — make sure the origin string is normalized (trailing slash, lowercase host).","Catch MaxOriginsReachedError and fall back to a fresh Client/Pool for overflow origins."],"exampleFix":"// before\nconst agent = new Agent({ maxOrigins: 2 })\nawait agent.request({ origin: 'https://a.com', method: 'GET', path: '/' })\nawait agent.request({ origin: 'https://b.com', method: 'GET', path: '/' })\nawait agent.request({ origin: 'https://c.com', method: 'GET', path: '/' }) // throws\n// after\nconst agent = new Agent({ maxOrigins: Infinity })","handlingStrategy":"fallback","validationCode":"function boundedDispatch(agent, opts, handler, overflowPool) {\n  try {\n    return agent.dispatch(opts, handler)\n  } catch (e) {\n    if (e.code === 'UND_ERR_MAX_ORIGINS_REACHED') return overflowPool.dispatch(opts, handler)\n    throw e\n  }\n}","typeGuard":"const isMaxOriginsError = (e) => e?.code === 'UND_ERR_MAX_ORIGINS_REACHED'","tryCatchPattern":"try {\n  await agent.request({ origin, method, path })\n} catch (e) {\n  if (e.code === 'UND_ERR_MAX_ORIGINS_REACHED') {\n    // raise maxOrigins or route via a fallback Client\n  } else throw e\n}","preventionTips":["Size maxOrigins to your real origin cardinality plus headroom.","Normalize origin strings (lowercase host, strip trailing slash) so duplicates count once."],"tags":["undici","agent","maxorigins","dispatch","limits"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}