mongodb/node-mongodb-native · error · MongoInvalidArgumentError

Argument "size" must be a non-negative number

Error message

Argument "size" must be a non-negative number

What it means

Thrown by BufferPool.read() when its `size` argument is not a number or is negative. The BufferPool is an internal helper used by the wire-protocol message stream to slice bytes off buffered network data, so an invalid size indicates a corrupted length prefix or a programming fault inside the driver rather than user input. It surfaces as a MongoInvalidArgumentError.

Source

Thrown at src/utils.ts:840

      return NumberUtils.getInt32LE(firstBuffer, 0);
    }

    // Unlikely case: an int32 is split across buffers.
    // Use read and put the returned buffer back on top
    const top4Bytes = this.read(4);
    const value = NumberUtils.getInt32LE(top4Bytes, 0);

    // Put it back.
    this.totalByteLength += 4;
    this.buffers.unshift(top4Bytes);

    return value;
  }

  /** Reads the requested number of bytes, optionally consuming them */
  read(size: number): Uint8Array {
    if (typeof size !== 'number' || size < 0) {
      throw new MongoInvalidArgumentError('Argument "size" must be a non-negative number');
    }

    // oversized request returns empty buffer
    if (size > this.totalByteLength) {
      return ByteUtils.allocate(0);
    }

    // We know we have enough, we just don't know how it is spread across chunks
    // TODO(NODE-4732): alloc API should change based on raw option
    const result = ByteUtils.allocateUnsafe(size);

    for (let bytesRead = 0; bytesRead < size; ) {
      const buffer = this.buffers.shift();
      if (buffer == null) {
        break;
      }
      const bytesRemaining = size - bytesRead;
      const bytesReadable = Math.min(bytesRemaining, buffer.byteLength);

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Verify the endpoint is a real MongoDB server (not a proxy returning non-MongoDB bytes); connect directly to a mongod/mongos to isolate.
  2. Upgrade the driver to the latest patch release in case the regression is fixed; if reproducible on latest, file a driver bug with the full stack trace and server version.
  3. If a proxy/load balancer sits in front, bypass it or configure it to operate at L4 (raw TCP) rather than inspecting/rewriting the stream.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await collection.findOne(filter);
} catch (err) {
  if (err instanceof MongoInvalidArgumentError && /non-negative number/.test(err.message)) {
    // internal wire-protocol fault; reconnect against a verified MongoDB endpoint
    throw new Error('Wire-protocol parse error; verify the endpoint is a MongoDB server', { cause: err });
  }
  throw err;
}

Prevention

When it happens

Trigger: The driver computing a negative or non-numeric byte count when parsing a server response, typically because the wire-protocol length field was malformed or a buffer was advanced incorrectly. Not reachable through normal public API calls.

Common situations: Wire-level corruption from a misbehaving proxy or load balancer rewriting MongoDB traffic; a malformed server response on a non-MongoDB endpoint mistakenly targeted by the driver; an internal regression after a BSON/wire-protocol change.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/31e7bb5461623773.json. Report an issue: GitHub.