{"record":{"id":"f142959c5ac7a744","repo":"nodejs/node","slug":"und-err-invalid-arg-f14295","errorCode":"UND_ERR_INVALID_ARG","errorMessage":"maxCachedSessions must be a positive integer or zero","messagePattern":"maxCachedSessions must be a positive integer or zero","errorType":"validation","errorClass":"InvalidArgumentError","httpStatus":null,"severity":"error","filePath":"deps/undici/src/lib/core/connect.js","lineNumber":64,"sourceCode":"          this._sessionCache.delete(key)\n          return\n        }\n      }\n\n      const oldest = this._sessionCache.keys().next()\n      if (!oldest.done) {\n        this._sessionCache.delete(oldest.value)\n      }\n    }\n\n    this._sessionCache.set(sessionKey, new WeakRef(session))\n    this._sessionRegistry.register(session, sessionKey)\n  }\n}\n\nfunction buildConnector ({ allowH2, preferH2, useH2c, maxCachedSessions, socketPath, timeout, session: customSession, ...opts }) {\n  if (maxCachedSessions != null && (!Number.isInteger(maxCachedSessions) || maxCachedSessions < 0)) {\n    throw new InvalidArgumentError('maxCachedSessions must be a positive integer or zero')\n  }\n\n  const options = { path: socketPath, ...opts }\n  const sessionCache = new SessionCache(maxCachedSessions == null ? 100 : maxCachedSessions)\n  timeout = timeout == null ? 10e3 : timeout\n  allowH2 = allowH2 != null ? allowH2 : true\n  return function connect ({ hostname, host, protocol, port, servername, localAddress, httpSocket }, callback) {\n    let socket\n    if (protocol === 'https:') {\n      if (!tls) {\n        tls = require('node:tls')\n      }\n      servername = servername || options.servername || util.getServerName(host) || null\n\n      const sessionKey = servername || hostname\n      assert(sessionKey)\n\n      const session = customSession || sessionCache.get(sessionKey) || null","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/undici/src/lib/core/connect.js#L46-L82","documentation":"Thrown by buildConnector (the TLS/connection factory used by undici's Client and Pool) when maxCachedSessions is provided but is not an integer >= 0. maxCachedSessions controls how many resumed TLS sessions are cached per host; the guard is `maxCachedSessions != null && (!Number.isInteger(...) || value < 0)`.","triggerScenarios":"Passing maxCachedSessions as a float (1.5), a negative number, a string ('100'), NaN, Infinity, or a BigInt. Note the message says 'positive integer or zero' but 0 is accepted; only negative or non-integer values throw.","commonSituations":"Setting maxCachedSessions via env var as a string; accidentally using a fractional value; disabling caching by passing -1 instead of 0; misreading the message as 'must be > 0'.","solutions":["Pass an integer >= 0: new Client(url, { connect: { maxCachedSessions: 100 } }).","To disable session caching, pass 0 (not a negative number).","Coerce from config: Number.parseInt(process.env.TLS_SESSION_CACHE, 10).","Omit the option entirely to take the default of 100."],"exampleFix":"// before\nnew Client(url, { connect: { maxCachedSessions: process.env.TLS_CACHE } })\n// after\nconst n = Number.parseInt(process.env.TLS_CACHE, 10)\nnew Client(url, { connect: { maxCachedSessions: Number.isNaN(n) ? 100 : n } })","handlingStrategy":"validation","validationCode":"function coerceMaxCachedSessions(v) {\n  const n = Number(v)\n  if (!Number.isInteger(n) || n < 0) {\n    throw new TypeError('maxCachedSessions must be an integer >= 0')\n  }\n  return n\n}","typeGuard":"function isValidMaxCachedSessions(v) {\n  return v == null || (typeof v === 'number' && Number.isInteger(v) && v >= 0)\n}","tryCatchPattern":null,"preventionTips":["Use 0 to disable session caching, not a negative number.","Parse env vars with Number.parseInt(..., 10).","Omit the option to accept the default of 100.","Note the message: 'positive integer or zero' but 0 is valid."],"tags":["tls","connect","validation","numeric","undici"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}