nestjs/nest · error · CorruptedPacketLengthException
Corrupted length value "${rawContentLength}" supplied in a p
Error message
Corrupted length value "${rawContentLength}" supplied in a packet What it means
Thrown as CorruptedPacketLengthException from JsonSocket.handleData() when the bytes before the '#' delimiter cannot be parsed by parseInt() as an integer (parseInt yields NaN). The NestJS TCP framing protocol is '<length>#<json-body>'; if the length prefix is non-numeric (e.g. garbage, a partial previous frame, or a non-NestJS client speaking a different protocol), the socket cannot determine the message boundary and rejects the frame, tearing the connection down via TcpSocket.onData().
Source
Thrown at packages/microservices/helpers/json-socket.ts:60
throw new MaxPacketLengthExceededException(bufferLength);
}
if (this.contentLength === null) {
const i = this.buffer.indexOf(this.delimiter);
/**
* Check if the buffer has the delimiter (#),
* if not, the end of the buffer string might be in the middle of a content length string
*/
if (i === -1) {
break;
}
const rawContentLength = this.buffer.substring(0, i);
this.contentLength = parseInt(rawContentLength, 10);
if (isNaN(this.contentLength)) {
this.contentLength = null;
this.buffer = '';
throw new CorruptedPacketLengthException(rawContentLength);
}
this.buffer = this.buffer.substring(i + 1);
}
if (this.contentLength !== null) {
const length = this.buffer.length;
if (length === this.contentLength) {
this.handleMessage(this.buffer);
// handleMessage resets contentLength and buffer; next iteration will break
} else if (length > this.contentLength) {
const message = this.buffer.substring(0, this.contentLength);
const rest = this.buffer.substring(this.contentLength);
this.handleMessage(message); // resets this.buffer to ''
this.buffer = rest; // restore remaining data for next iteration
continue;
} else {
// Incomplete message — wait for more data
break;View on GitHub (pinned to 6ec0e2783d)
Solutions
- Ensure both endpoints speak the NestJS TCP framing protocol ('<length>#<json>') — use NestJS ClientTCP against a NestJS ServerTCP.
- Make sure no proxy/LB rewrites the byte stream; use raw L4 pass-through for the TCP transport.
- Confirm the serializer on the sender produces the standard framed format and the deserializer on the receiver matches.
- Drop and reconnect: a corrupted frame is unrecoverable on that socket; the connection is closed by design.
Example fix
// before
// plain net.Socket client writing raw JSON to a NestJS TCP server -> CorruptedPacketLengthException
const sock = net.connect(3001, '127.0.0.1');
sock.write(JSON.stringify({ pattern: 'x', data: 1 }));
// after
const client = new ClientTCP({ host: '127.0.0.1', port: 3001 });
await client.connect();
client.send('x', 1).subscribe(); Defensive patterns
Strategy: validation
Validate before calling
// Validate that the peer speaks the NestJS TCP framing protocol before bulk traffic.
// (No public pre-flight; instead only connect NestJS ClientTCP to NestJS ServerTCP.)
function assertNestTcpPeer(server: { transport?: string }) {
if (server.transport && server.transport !== 'TCP') {
throw new Error('Expected a NestJS TCP server for framed communication.');
}
} Type guard
const isCorruptedPacketLength = (e: unknown): boolean => /Corrupted length value/.test((e as Error)?.message ?? '');
Try / catch
// Connection is torn down on this error; catch at the call site and reconnect with a known-good peer.
try {
await firstValueFrom(client.send('x', 1));
} catch (e) {
if (isCorruptedPacketLength(e)) { /* peer is not NestJS TCP; switch client/fix framing */ }
else throw e;
} Prevention
- Only connect NestJS ClientTCP to a NestJS ServerTCP endpoint.
- Avoid L7 proxies that rewrite the byte stream; use L4 pass-through.
- Keep serializer/deserializer matched on both ends so framing stays '<int>#<json>'.
When it happens
Trigger: A non-NestJS client/server connects to the TCP microservice port and sends raw bytes not framed as '<int>#<json>'. A previous oversized/corrupt frame left residue in the buffer that gets parsed as a length prefix. A man-in-the-middle or proxy that mangles the framing. Mismatched serializer that writes data without the '#'-delimited length header.
Common situations: Pointing a plain TCP/HTTP client at a NestJS TCP microservice port. A load balancer or proxy rewriting the byte stream. Connecting a NestJS TCP client to a non-NestJS TCP server that uses a different framing. Leftover bytes after a MaxPacketLength reset on a connection that wasn't fully closed.
Related errors
- The packet length (${length}) exceeds the maximum allowed le
- Not initialized. Please call the "connect" method first.
- Could not parse JSON: ${err.message} Request data: ${data}
- Method is not supported for Kafka client
- The client consumer did not subscribe to the corresponding r
AI-assisted analysis of nestjs/nest@6ec0e2783d (2026-08-03).
Data as JSON: /data/errors/a3b0a6477bfba49a.json.
Report an issue: GitHub.