{"record":{"id":"29c676f1dba072f0","repo":"denoland/deno","slug":"err-socket-buffer-size","errorCode":"ERR_SOCKET_BUFFER_SIZE","errorMessage":"Could not get or set buffer size: ${context.syscall} returned ${context.code} (${context.message})","messagePattern":"Could not get or set buffer size: (.+?) returned (.+?) \\((.+?)\\)","errorType":"exception","errorClass":"SystemError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/dgram.ts","lineNumber":1410,"sourceCode":"  newHandle.bind = oldHandle.bind;\n  newHandle.send = oldHandle.send;\n  newHandle[ownerSymbol] = self;\n\n  // Replace the existing handle by the handle we got from primary.\n  oldHandle.close();\n  state.handle = newHandle;\n}\n\nfunction bufferSize(self: Socket, size: number, buffer: boolean): number {\n  if (size >>> 0 !== size) {\n    throw new ERR_SOCKET_BAD_BUFFER_SIZE();\n  }\n\n  const ctx = {};\n  const ret = self[kStateSymbol].handle!.bufferSize(size, buffer, ctx);\n\n  if (ret === undefined) {\n    throw new ERR_SOCKET_BUFFER_SIZE(ctx as NodeSystemErrorCtx);\n  }\n\n  return ret;\n}\n\nfunction socketCloseNT(self: Socket) {\n  self.emit(\"close\");\n}\n\nfunction healthCheck(socket: Socket) {\n  if (!socket[kStateSymbol].handle) {\n    // Error message from dgram_legacy.js.\n    throw new ERR_SOCKET_DGRAM_NOT_RUNNING();\n  }\n}\n\nfunction stopReceiving(socket: Socket) {\n  const state = socket[kStateSymbol];","sourceCodeStart":1392,"sourceCodeEnd":1428,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/dgram.ts#L1392-L1428","documentation":"After the uint32 check passes, bufferSize() delegates to the native handle; if the OS-level getsockopt/setsockopt syscall fails, the handle returns undefined and the error context (syscall, code/errno, message) captured in ctx is wrapped in ERR_SOCKET_BUFFER_SIZE. This is a runtime failure of the socket option itself, not an argument problem.","triggerScenarios":"socket.setRecvBufferSize(64 * 1024 * 1024) on Linux where net.core.rmem_max is far lower; setSendBufferSize with a size the kernel rejects; querying buffer sizes on a socket whose handle hit a resource limit.","commonSituations":"High-throughput UDP apps (media streaming, telemetry) requesting OS buffers larger than kernel defaults; containers with locked-down sysctls; macOS where buffer clamping differs from Linux.","solutions":["Lower the requested size (start at 1-4 MiB and binary-search the maximum the OS accepts)","On Linux raise the caps: sysctl -w net.core.rmem_max=<n> and net.core.wmem_max=<n> (needs privileges)","Wrap the call in try-catch and continue with the OS default buffer size, logging the syscall/errno from the error context"],"exampleFix":"// before\nsock.setRecvBufferSize(64 * 1024 * 1024); // may throw ERR_SOCKET_BUFFER_SIZE (setsockopt)\n\n// after\ntry {\n  sock.setRecvBufferSize(4 * 1024 * 1024);\n} catch (err) {\n  if (err.code === 'ERR_SOCKET_BUFFER_SIZE') {\n    console.warn(`buffer size rejected (${err.syscall} ${err.code}): keeping OS default`);\n  } else throw err;\n}","handlingStrategy":"try-catch","validationCode":"// Probe the maximum acceptable size once at startup, then reuse it\nfunction probeMaxBuffer(sock, start, max) {\n  let ok = 0;\n  for (let s = start; s <= max; s *= 2) {\n    try { sock.setRecvBufferSize(s); ok = s; } catch { return ok; }\n  }\n  return ok;\n}","typeGuard":null,"tryCatchPattern":"try {\n  sock.setRecvBufferSize(want);\n} catch (err) {\n  if (err?.code === 'ERR_SOCKET_BUFFER_SIZE') {\n    // err.syscall/err.code/err.message carry the failing getsockopt/setsockopt context\n    log.warn('kernel rejected buffer size, keeping default', err.syscall, err.errno);\n  } else throw err;\n}","preventionTips":["Document the OS caps your deployment needs (net.core.rmem_max / wmem_max on Linux)","Binary-search the accepted maximum instead of hardcoding a huge value","Treat buffer sizing as best-effort: degrade to defaults rather than crashing the socket setup"],"tags":["node-compat","dgram","udp","os-limits","network"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}