denoland/deno · error · NodeTypeError
ERR_SOCKET_BAD_BUFFER_SIZE
ERR_SOCKET_BAD_BUFFER_SIZE
Error message
Buffer size must be a positive integer
What it means
The recvBufferSize/sendBufferSize setters route through bufferSize(), which requires size to be a non-negative 32-bit integer (size >>> 0 === size). Negative numbers, fractions, NaN, Infinity, or values above 0xFFFFFFFF fail the check and throw ERR_SOCKET_BAD_BUFFER_SIZE before any socket option is applied.
Solutions
- Coerce and validate before assigning: const n = Number(raw); if (Number.isInteger(n) && n >= 0) sock.recvBufferSize = n
- Cap at 0xFFFFFFFF (max unsigned 32-bit)
- Read back the getter to confirm what the OS actually applied
Example fix
// before
socket.recvBufferSize = process.env.UDP_RCVBUF; // string like '4096'
// after
const size = Number(process.env.UDP_RCVBUF);
if (Number.isInteger(size) && size >= 0) {
socket.recvBufferSize = size;
} Defensive patterns
Strategy: validation
Validate before calling
function setBufSize(socket, prop, raw) {
const n = Number(raw);
if (!Number.isInteger(n) || n < 0 || n > 0xFFFFFFFF) return false;
socket[prop] = n;
return true;
}
if (!setBufSize(socket, 'recvBufferSize', process.env.UDP_RCVBUF)) {
console.warn('ignoring invalid UDP_RCVBUF:', process.env.UDP_RCVBUF);
} Type guard
const isValidBufSize = (v) => Number.isInteger(Number(v)) && Number(v) >= 0 && Number(v) <= 0xFFFFFFFF;
Prevention
- Convert env/config values with Number() before assigning buffer sizes
- Remember the check is a 32-bit unsigned range: no negatives, fractions, NaN or >4 GiB
- Read back the getter to confirm the OS accepted the value
When it happens
Trigger: socket.recvBufferSize = 1024 * 1.5; assigning a string parsed from an env var without Number(); assigning NaN from undefined arithmetic; sizes above 4294967295.
Common situations: Buffer sizes read from config/env as strings; percentage multipliers producing fractions; test values like -1 used to probe validation.
Related errors
- ERR_INVALID_ARG_TYPE
- ERR_MISSING_ARGS
- ERR_SOCKET_BUFFER_SIZE
- ERR_BUFFER_OUT_OF_BOUNDS
- ERR_INVALID_FD_TYPE
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/af76ea75c24d086f.
Report an issue: GitHub.
Appendix: 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 9ad36f7a2c)