fix(process): release drained bridge reply backing storage (#12180)

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandShuaixinHuang authored and GitHub committed 2026-11-19 04:00:58 +00:00
1 parent 3493d6d252
commit fe784e7f03
2 files changed
+28 -1

No files matched your search

+14
View File
@@ -130,6 +130,20 @@ describe("UTF-8 NDJSON line buffer", () => {
});
});
it("releases a drained large frame when only a partial next frame remains", () => {
const bytes = 1024 * 1024;
const lines = new Utf8NdjsonLineBuffer(() => bytes);
expect(texts(lines.push(Buffer.alloc(bytes, 0x78)))).toEqual([]);
const [first] = texts(lines.push(Buffer.from("\npartial")));
expect(first?.length).toBe(bytes);
expect(lines.retainedTail()).toEqual({
pendingBytes: 7,
backingBytes: 7,
});
expect(texts(lines.push(Buffer.from(" line\n")))).toEqual(["partial line"]);
});
it("discards an unterminated tail once it passes the ceiling", () => {
const lines = new Utf8NdjsonLineBuffer(() => 16);
expect([...lines.push(Buffer.alloc(16, 0x78))]).toEqual([]);
+14 -1
View File
@@ -101,7 +101,20 @@ export class Utf8NdjsonLineBuffer {
if (!line.ok) return;
}
const refused = this.#refuseTail(maximumBytes);
if (refused !== undefined) yield err(refused);
if (refused !== undefined) {
yield err(refused);
return;
}
// A short tail is a view into the drained frames. Copy it once after
// draining, when doing so releases more bytes than it copies.
if (
this.#buffer.buffer.byteLength >
Math.max(Buffer.poolSize, 2 * this.#buffer.length)
) {
const tail = Buffer.allocUnsafeSlow(this.#buffer.length);
this.#buffer.copy(tail);
this.#buffer = tail;
}
}
/** Validate the unfinished tail at EOF without treating it as a complete response. */