mongodb/node-mongodb-native · error · MongoDecompressionError

Message body and message header must be the same length

Error message

Message body and message header must be the same length

What it means

Thrown after decompression succeeds when the length of the decompressed body does not equal the length declared in the compressed message header (read at offset headerSize+4). The driver treats this as a decompression failure because the payload cannot be safely interpreted as a complete OP_MSG/OP_REPLY. It is a MongoDecompressionError. Root causes are almost always data corruption during transit or a bug/version mismatch in the compression library (snappy, zlib, or zstd).

Solutions

  1. Turn off compression (drop the 'compressors' option) to confirm the link is otherwise healthy.
  2. Reinstall optional compression dependencies (snappy, @mongodb-js/zstd) ensuring no native build errors; check they are the versions the driver expects.
  3. Bypass any proxy/VPN/tunnel between client and server and connect directly to mongod.
  4. Upgrade both the driver and server to matching supported versions.
  5. File a driver bug with the compressed frame bytes if the failure reproduces on a direct unproxied connection.

Example fix

// before
const client = new MongoClient(uri, { compressors: ['snappy'] });

// after
const client = new MongoClient(uri);
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await db.command({ ping: 1 });
} catch (error) {
  if (error instanceof MongoDecompressionError && /same length/.test(error.message)) {
  }
}

Prevention

When it happens

Trigger: Fires at src/cmap/wire_protocol/compression.ts:212 inside decompressResponse, after `await decompress(compressorID, compressedBuffer)`, when `messageBody.length !== header.length`. Requires an OP_COMPRESSED response, a valid recognized compressorID, successful library decompression, but a resulting byte count that disagrees with the header's declared uncompressed length.

Common situations: A transparent proxy truncates or re-encodes frames; an outdated or buggy snappy/zstd native binding (especially optional deps that failed to install cleanly and fell back); mismatched compression level between client and server; network-level packet corruption that still decompresses to a wrong-sized buffer.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/fcdab595acb57579. Report an issue: GitHub.

Appendix: source

Thrown at src/cmap/wire_protocol/compression.ts:212

    const ResponseType = messageHeader.opCode === OP_MSG ? OpMsgResponse : OpReply;
    const messageBody = message.subarray(MESSAGE_HEADER_SIZE);
    return new ResponseType(message, messageHeader, messageBody);
  }

  const header: MessageHeader = {
    ...messageHeader,
    fromCompressed: true,
    opCode: readInt32LE(message, MESSAGE_HEADER_SIZE),
    length: readInt32LE(message, MESSAGE_HEADER_SIZE + 4)
  };
  const compressorID = message[MESSAGE_HEADER_SIZE + 8];
  const compressedBuffer = message.slice(MESSAGE_HEADER_SIZE + 9);

  // recalculate based on wrapped opcode
  const ResponseType = header.opCode === OP_MSG ? OpMsgResponse : OpReply;
  const messageBody = await decompress(compressorID, compressedBuffer);
  if (messageBody.length !== header.length) {
    throw new MongoDecompressionError('Message body and message header must be the same length');
  }
  return new ResponseType(message, header, messageBody);
}

View on GitHub (pinned to dce7939f86)