denoland/deno · error · TypeError
ERR_SOCKET_BAD_BUFFER_SIZE
ERR_SOCKET_BAD_BUFFER_SIZE
Error message
Buffer size must be a positive integer
What it means
Backing validation for dgram Socket.setRecvBufferSize/setSendBufferSize (via the internal bufferSize() helper): the size is coerced with 'size >>> 0' and must round-trip exactly, i.e. be a uint32. Negative numbers, fractional values, NaN, Infinity, and anything above 0xFFFFFFFF fail the check and throw ERR_SOCKET_BAD_BUFFER_SIZE.
Source
Thrown at ext/node/polyfills/dgram.ts:1403
function replaceHandle(self: Socket, newHandle: UDP) {
const state = self[kStateSymbol];
const oldHandle = state.handle!;
// Set up the handle that we got from primary.
newHandle.lookup = oldHandle.lookup;
newHandle.bind = oldHandle.bind;
newHandle.send = oldHandle.send;
newHandle[ownerSymbol] = self;
// Replace the existing handle by the handle we got from primary.
oldHandle.close();
state.handle = newHandle;
}
function bufferSize(self: Socket, size: number, buffer: boolean): number {
if (size >>> 0 !== size) {
throw new ERR_SOCKET_BAD_BUFFER_SIZE();
}
const ctx = {};
const ret = self[kStateSymbol].handle!.bufferSize(size, buffer, ctx);
if (ret === undefined) {
throw new ERR_SOCKET_BUFFER_SIZE(ctx as NodeSystemErrorCtx);
}
return ret;
}
function socketCloseNT(self: Socket) {
self.emit("close");
}
function healthCheck(socket: Socket) {
if (!socket[kStateSymbol].handle) {View on GitHub (pinned to 89f33cbef2)
Solutions
- Sanitize to a non-negative integer before calling: Math.max(0, Math.floor(Number(size))) and verify !Number.isNaN
- Use Number.isInteger(size) && size >= 0 && size <= 0xFFFFFFFF as a guard for values from untrusted sources
- Pick a sane default (e.g. 4096..262144) when the configured value fails validation
Example fix
// before const size = parseInt(process.env.SO_RCVBUF); // NaN if unset/malformed sock.setRecvBufferSize(size); // throws ERR_SOCKET_BAD_BUFFER_SIZE // after const size = Number.parseInt(process.env.SO_RCVBUF, 10); sock.setRecvBufferSize(Number.isInteger(size) && size > 0 ? size : 65536);
Defensive patterns
Strategy: validation
Validate before calling
function safeBufferSize(v, fallback = 65536) {
const n = Math.floor(Number(v));
return Number.isInteger(n) && n >= 0 && n <= 0xFFFFFFFF ? n : fallback;
}
sock.setRecvBufferSize(safeBufferSize(config.recvBuf)); Type guard
function isU32(v) { return typeof v === 'number' && Number.isInteger(v) && v >= 0 && v <= 0xFFFFFFFF; } Prevention
- Never feed parseInt results directly to buffer-size setters
- Round fractional computed sizes with Math.floor
- Keep configured sizes within a small known set of tested values
When it happens
Trigger: socket.setRecvBufferSize(65536.5); socket.setSendBufferSize(-1); socket.setRecvBufferSize(NaN) (e.g. a failed parseInt); setRecvBufferSize(2 ** 33); passing a string like '65536'.
Common situations: Reading buffer sizes from config/env without numeric validation; computing sizes with division or scaling that yields fractions; parseInt returning NaN on malformed input.
Related errors
- ERR_INVALID_ARG_TYPE
- ERR_SOCKET_BUFFER_SIZE
- ERR_SOCKET_DGRAM_NOT_RUNNING
- resolve hook must return { shortCircuit: true } or call next
- load hook must return { shortCircuit: true } or call nextLoa
AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16).
Data as JSON: /api/errors/af76ea75c24d086f.
Report an issue: GitHub.