Skip to content

Commit 74db528

Browse files
committed
fix: resolve memory leak in parser buffer management
Fix buffer shrinking: create new appropriately-sized buffer instead of just adjusting pointers when partial messages remain. This allows garbage collection of large processed buffers that were previously kept alive due to cursor-based buffer management. Resolves memory leak where large buffers would remain in memory indefinitely even when only a few bytes of data were still needed.
1 parent 65bc3d4 commit 74db528

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

packages/pg-protocol/src/parser.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,16 @@ export class Parser {
112112
this.bufferLength = 0
113113
this.bufferOffset = 0
114114
} else {
115-
// Adjust the cursors of remainingBuffer
116-
this.bufferLength = bufferFullLength - offset
117-
this.bufferOffset = offset
115+
// A partial message remains.
116+
// Create a new, smaller buffer and copy only the remaining data into it.
117+
// This breaks the reference to the original, potentially huge buffer.
118+
const remainingLength = bufferFullLength - offset
119+
const newBuffer = Buffer.allocUnsafe(remainingLength)
120+
this.buffer.copy(newBuffer, 0, offset, offset + remainingLength)
121+
122+
this.buffer = newBuffer
123+
this.bufferOffset = 0
124+
this.bufferLength = remainingLength
118125
}
119126
}
120127

0 commit comments

Comments
 (0)