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 by decompressResponse() after decompressing an OP_COMPRESSED message when the decompressed body length does not equal the original (uncompressed) length declared in the compressed message header. The mismatch means the decompression produced the wrong number of bytes, indicating data corruption, a truncated/modified message, or a compressor library bug. Surfaced as MongoDecompressionError.

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 3366c21a63)

Solutions

  1. Disable compression to rule out a native compressor library bug.
  2. Eliminate any intermediary (proxy, LB, MITM) that could alter compressed message bytes.
  3. Reinstall/rebuild native compression dependencies (`@mongodb-js/zstd`, snappy) if using them.
  4. If it persists on a clean network path with current libraries, report a driver bug with the corrupted message bytes.
Defensive patterns

Strategy: retry

Try / catch

let lastErr;
for (let attempt = 0; attempt < 3; attempt++) {
  try {
    return await collection.find({}).toArray();
  } catch (err) {
    lastErr = err;
    if (err instanceof MongoDecompressionError && /same length/.test(err.message)) {
      // likely transient corruption; retry with backoff
      await new Promise(r => setTimeout(r, 100 * 2 ** attempt));
      continue;
    }
    throw err;
  }
}
throw lastErr;

Prevention

When it happens

Trigger: After decompress(compressorID, compressedBuffer) returns messageBody, the check `messageBody.length !== header.length` fails. The header.length was read from the OP_COMPRESSED wrapper (the original message size). Occurs on any compressed response where bytes were lost/corrupted in transit or the compressor produced incorrect output.

Common situations: Network corruption not caught at the TCP layer (rare), a proxy/load balancer truncating or rewriting compressed payloads, a faulty zstd/zlib/snappy native build producing wrong-length output, or memory corruption in the buffer pipeline.

Related errors


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