Skip to content

Commit 25eab69

Browse files
committed
fix(client): settle stdio send() from the write callback
A backpressured `StdioClientTransport.send()` waited for a `'drain'` event, but a pipe destroyed by the server process exiting never drains, so the promise stayed pending for the lifetime of the process and the `'drain'` listener leaked. Settle from the `write()` callback instead, which Node invokes on flush or on failure, so the send rejects with the underlying write error. This is what `StdioServerTransport.send()` already does for its own stdout.
1 parent 1e1392e commit 25eab69

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@modelcontextprotocol/client': patch
3+
---
4+
5+
Fix `StdioClientTransport.send()` never settling when a backpressured write is still in flight and the pipe to the server dies. `send()` waited for a `'drain'` event, but a destroyed stream never drains, so the returned promise stayed pending and the `'drain'` listener was never removed. It now settles from the `write()` callback, which Node invokes on flush or on failure, so the send rejects with the underlying write error instead. This is what `StdioServerTransport.send()` already does for its own stdout.
6+
7+
The visible symptom was `await client.notification(...)`, which never returned: the notification path awaits the transport send with no timeout, and the connection-closed teardown settles pending responses but not pending sends. Requests were already rescued by that teardown, so `callTool()` and friends now report the actual write error (`EPIPE`, `EOF`) rather than a generic connection-closed error.
8+
9+
Reaching this needs a write the pipe will not accept in one go, which starts anywhere from tens to hundreds of KB depending on the platform pipe buffer and the Node version, sizes that base64 image payloads and file contents in tool arguments reach routinely.

packages/client/src/client/stdio.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,18 @@ export class StdioClientTransport implements Transport {
313313
}
314314

315315
send(message: JSONRPCMessage): Promise<void> {
316-
return new Promise(resolve => {
316+
return new Promise((resolve, reject) => {
317317
if (!this._process?.stdin) {
318318
throw new SdkError(SdkErrorCode.NotConnected, 'Not connected');
319319
}
320320

321321
const json = serializeMessage(message);
322-
if (this._process.stdin.write(json)) {
322+
// The write callback runs once the chunk is flushed or the write fails,
323+
// so a backpressured send still settles when the server exits and
324+
// destroys the pipe. Waiting on 'drain' alone parks forever there,
325+
// because a destroyed stream never drains.
326+
if (this._process.stdin.write(json, error => (error ? reject(error) : resolve()))) {
323327
resolve();
324-
} else {
325-
this._process.stdin.once('drain', resolve);
326328
}
327329
});
328330
}

packages/client/test/client/stdio.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,41 @@ test('_dispose releases the parent-side pipe handles even when a helper process
150150
expect(proc.stdout?.destroyed).toBe(true);
151151
expect(proc.stdin?.destroyed).toBe(true);
152152
}, 10_000);
153+
154+
test('send() rejects instead of hanging when the server exits before a large write flushes', async () => {
155+
// A server that never reads stdin and then exits: the write stays buffered,
156+
// and the exit destroys the pipe, so the stream can never flush or drain.
157+
const transport = new StdioClientTransport({
158+
command: process.execPath,
159+
args: ['-e', 'setTimeout(() => process.exit(0), 200)']
160+
});
161+
transport.onerror = () => {};
162+
await transport.start();
163+
const stdin = (transport as unknown as { _process: import('node:child_process').ChildProcess })._process.stdin!;
164+
165+
// 8 MB of params, far past stdin's 16 KB high water mark, so write() returns
166+
// false and the send has to wait for the stream instead of resolving at once.
167+
const pending = transport.send({
168+
jsonrpc: '2.0',
169+
id: 1,
170+
method: 'tools/call',
171+
params: { name: 'echo', arguments: { blob: 'x'.repeat(8 * 1024 * 1024) } }
172+
});
173+
174+
// Bounded so the unfixed behaviour reports as 'hung' rather than as a suite
175+
// timeout. The rejection reason is platform specific (EPIPE, EOF), so only
176+
// the fact that it settles is asserted.
177+
const outcome = await Promise.race([
178+
pending.then(
179+
() => 'resolved' as const,
180+
() => 'rejected' as const
181+
),
182+
new Promise<'hung'>(resolve => {
183+
setTimeout(() => resolve('hung'), 3000).unref();
184+
})
185+
]);
186+
187+
expect(outcome).toBe('rejected');
188+
expect(stdin.listenerCount('drain')).toBe(0);
189+
await transport.close();
190+
}, 15_000);

0 commit comments

Comments
 (0)