Skip to content
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/client/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ export class StdioClientTransport implements Transport {

if (this._stderrStream && this._process.stderr) {
this._process.stderr.pipe(this._stderrStream);
// Auto-resume so the pipe never blocks the child process.
// Without this, a server that logs to stderr will deadlock if
// nobody attaches a reader — the pipe fills, the child blocks
// on write(2), and the session silently hangs.
this._stderrStream.resume();
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions test/client/stdio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,34 @@ test('should fire onerror and close when ReadBuffer overflows', async () => {
expect(error.message).toMatch(/ReadBuffer exceeded maximum size/);
await closed;
});

test('stderr pipe should not deadlock when nobody reads it', async () => {
// Spawn a server that writes a lot to stderr.
// Without auto-resume, the pipe fills, the child blocks on write(2),
// and the session hangs silently.
const client = new StdioClientTransport({
command: 'node',
args: ['-e', `
process.stderr.write('log line '.repeat(5000) + '\\n');
process.stdout.write(JSON.stringify({jsonrpc:'2.0', id:1, result:{}}) + '\\n');
`],
stderr: 'pipe'
});

// Do NOT attach a reader to client.stderr — this is the bug scenario.
const started = Date.now();
await Promise.race([
client.start().then(() => {
return new Promise<void>((resolve) => {
client.onmessage = () => resolve();
});
}),
new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error('Session hung - likely stderr deadlock')), 5000)
)
]);

// Should complete quickly, not hang
expect(Date.now() - started).toBeLessThan(5000);
await client.close();
});
Loading