Skip to content

Commit f6c08a1

Browse files
authored
fix(core): remove buffer slice to prevent OOM on large output streams (google-gemini#25094)
1 parent c1fd602 commit f6c08a1

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

packages/core/src/services/shellExecutionService.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,22 @@ describe('ShellExecutionService child_process fallback', () => {
16101610
'exit',
16111611
]);
16121612
});
1613+
1614+
it('should correctly measure sniffedBytes with >20 small chunks to prevent OOM (regression #22170)', async () => {
1615+
mockIsBinary.mockReturnValue(false);
1616+
1617+
await simulateExecution('cat lots_of_chunks', (cp) => {
1618+
for (let i = 0; i < 25; i++) {
1619+
cp.stdout?.emit('data', Buffer.alloc(10, 'a'));
1620+
}
1621+
cp.emit('exit', 0, null);
1622+
cp.emit('close', 0, null);
1623+
});
1624+
1625+
const lastCallBuffer =
1626+
mockIsBinary.mock.calls[mockIsBinary.mock.calls.length - 1][0];
1627+
expect(lastCallBuffer.length).toBe(250);
1628+
});
16131629
});
16141630

16151631
describe('Platform-Specific Behavior', () => {

packages/core/src/services/shellExecutionService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ export class ShellExecutionService {
630630
}
631631

632632
if (isStreamingRawContent && sniffedBytes < MAX_SNIFF_SIZE) {
633-
const sniffBuffer = Buffer.concat(state.sniffChunks.slice(0, 20));
633+
const sniffBuffer = Buffer.concat(state.sniffChunks);
634634
sniffedBytes = sniffBuffer.length;
635635

636636
if (isBinary(sniffBuffer)) {
@@ -1094,7 +1094,7 @@ export class ShellExecutionService {
10941094
}
10951095

10961096
if (isStreamingRawContent && sniffedBytes < MAX_SNIFF_SIZE) {
1097-
const sniffBuffer = Buffer.concat(sniffChunks.slice(0, 20));
1097+
const sniffBuffer = Buffer.concat(sniffChunks);
10981098
sniffedBytes = sniffBuffer.length;
10991099

11001100
if (isBinary(sniffBuffer)) {

0 commit comments

Comments
 (0)