Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/serializer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const Transform = require('readable-stream').Transform

// A malformed stream can make the same packet fail to parse for every frame it sends -- thousands
// of identical stacks a minute. Log the first occurrence of each in full, then only at 1, 2, 4, 8,
// ... so a persistent fault stays O(log n) instead of unbounded. Keyed per parser instance.
function logThrottled (counts, key, full) {
const n = (counts.get(key) || 0) + 1
counts.set(key, n)
if (n === 1) console.log(full)
else if ((n & (n - 1)) === 0) console.log(`${key} (repeated ${n} times)`)
}

class Serializer extends Transform {
constructor (proto, mainType) {
super({ writableObjectMode: true })
Expand Down Expand Up @@ -62,6 +72,7 @@ class FullPacketParser extends Transform {
this.proto = proto
this.mainType = mainType
this.noErrorLogging = noErrorLogging
this.errorCounts = new Map()
}

parsePacketBuffer (buffer) {
Expand All @@ -73,13 +84,14 @@ class FullPacketParser extends Transform {
try {
packet = this.parsePacketBuffer(chunk)
if (packet.metadata.size !== chunk.length && !this.noErrorLogging) {
console.log('Chunk size is ' + chunk.length + ' but only ' + packet.metadata.size + ' was read ; partial packet : ' +
logThrottled(this.errorCounts, 'chunk size mismatch',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Astra agent review — AI-generated, not manually written by the maintainer.

The constant key combines every size mismatch in this parser, so a different malformed packet never gets its first full diagnostic. I reproduced this with a real one-byte container schema and buffers 01 63, 01 63, 02 58, 02 58: only the first packet's parsed value/buffer is logged; the second fault is silent at occurrence 3 and becomes just chunk size mismatch (repeated 4 times) at occurrence 4. Since these mismatches are only logged and the packets are still pushed downstream, the second fault's details are lost altogether. Could the grouping distinguish different packet/schema failures and preserve a first diagnostic for each, with bounded bookkeeping and an interleaved-malformed-packet test? The partial-read key has the same risk when different generated read paths share the first stack line.

'Chunk size is ' + chunk.length + ' but only ' + packet.metadata.size + ' was read ; partial packet : ' +
JSON.stringify(packet.data) + '; buffer :' + chunk.toString('hex'))
}
} catch (e) {
if (e.partialReadError) {
if (!this.noErrorLogging) {
console.log(e.stack)
logThrottled(this.errorCounts, e.stack.split('\n')[0], e.stack)
}
return cb()
} else {
Expand Down
Loading