mxc icon indicating copy to clipboard operation
mxc copied to clipboard

Node SDK on Windows: writes to standardInput wait behind idle stdout/stderr reads on libuv's thread pool

Open vinnividivicci opened this issue 3 weeks ago • 1 comments

Relevant area(s)

Windows

Brief description of your issue

On Windows, @microsoft/mxc-sdk 1.0.1 wraps each container's stdio handle in fs.createReadStream / fs.createWriteStream with windowsHandle (dist/bindings/native-stdio.js). Those streams do their blocking reads and writes on libuv's thread pool, which has 4 threads by default.

Each output stream that is waiting for data holds one pool thread. Once four are waiting, a write to a container's standardInput is queued until one of those reads returns. A common case is stdout and stderr of two containers started from one Node process.

A request/response child, such as an ACP or MCP server over stdio, therefore deadlocks: it only writes after it reads the request, and the request never arrives. On Linux and macOS the SDK uses net.Socket instead, and MXC's own wxc-exec.exe with ordinary Node pipes shows no delay.

Steps to reproduce

Windows 11 26H2 (build 26300.9711), processcontainer at tier base-container, Node.js 26.13.0, @microsoft/mxc-sdk 1.0.1.

// repro.mjs. Run: node repro.mjs
//   default thread pool    -> "two" is never echoed
//   UV_THREADPOOL_SIZE=16  -> echoed at once
import { spawn } from '@microsoft/mxc-sdk/v1';

const node = `"${process.execPath}"`;
const ui = { disable: false, clipboard: 'none', allowInputInjection: false }; // node.exe needs UI on
const base = { containment: { type: 'processcontainer' }, network: { egress: { default: 'deny' } }, ui, timeoutMs: 60000 };

// A second container that never writes anything, like a proxy or an idle helper.
const idle = await spawn({ ...base, command: `${node} -e "setInterval(() => {}, 1000)"` });
idle.standardOutput.resume();
idle.standardError.resume();

// The child echoes every stdin line to stdout at once.
const echo = "process.stdin.on('data', d => process.stdout.write('echo ' + d));";
const child = await spawn({ ...base, command: `${node} -e "${echo}"` });
child.standardError.resume();
const t0 = Date.now();
child.standardOutput.on('data', d => console.log(`+${Date.now() - t0} ms child: ${String(d).trim()}`));

await new Promise(r => setTimeout(r, 1500));
console.log(`+${Date.now() - t0} ms host writes "two" (UV_THREADPOOL_SIZE=${process.env.UV_THREADPOOL_SIZE ?? 'default'})`);
child.standardInput.write('two\n');
await new Promise(r => setTimeout(r, 10000));
console.log(`+${Date.now() - t0} ms done waiting`);
child.kill(); idle.kill();
await Promise.allSettled([child.wait(), idle.wait()]);

Expected behavior

The line written to standardInput reaches the child at once, and echo two is printed within a few milliseconds, however many other stdio streams are open.

Actual behavior

  • Default thread pool: +1508 ms host writes "two", then nothing until +11513 ms done waiting. The line never reaches the child.
  • With UV_THREADPOOL_SIZE=16: +1513 ms host writes "two", then +1515 ms child: echo two.
  • With a child that also prints a line every 2 s: each write arrives at the child's next print.
  • With only one container (two streams), the stall doesn't occur.
  • It is not tied to a particular child. An ACP adapter (codex-acp) and codex app-server both stopped answering after initialize for the same reason.

Suggested fix: use overlapped pipe I/O on Windows too (net.Socket / a uv_pipe_t, as on Unix). Failing that, document the dependence on UV_THREADPOOL_SIZE.


Generated by Claude Code

vinnividivicci avatar Nov 08 '26 19:11 vinnividivicci

Same root cause hit from the other side: it is not only standardInput writes that queue. With libuv's default four threads, two idle contained processes (four pending output reads) left every fs call of the host process waiting, with no error: in our case keychain reads, config reads and the next prompt all stalled.

What we do meanwhile: start the host process with UV_THREADPOOL_SIZE=16 and refuse a contained process past floor((pool − max(2, pool/4)) / 2) live ones (six with 16 threads), so the host keeps a quarter of the pool. The pool is not free on Windows: a pool of 64 measured about 484 MB more private memory in the host process (about 8 MB per thread). Reading the pipes with overlapped I/O (or net.Socket over a named pipe, as on the other platforms) instead of fs.createReadStream would remove the limit.

mrestrepo95 avatar Nov 19 '26 13:11 mrestrepo95