From 53ce82f8f614d4b2c674bbf9ab0d5e405a80a928 Mon Sep 17 00:00:00 2001 From: ahmadgamal15-art Date: Mon, 3 Aug 2026 11:26:20 -0400 Subject: [PATCH] fix(mcp): set stdio to binary mode on Windows On Windows, stdin/stdout default to text mode, which translates LF to CRLF on write and CRLF to LF on read. The MCP framing layer counts bytes for Content-Length, so any translated byte makes the declared length disagree with the actual payload. The subsequent fread() then waits for bytes that never arrive and the server hangs with no error. The symptom is a native MCP server that connects, handshakes, and then stops responding on the first message containing a newline -- which in practice is every real request. It reproduces on Windows only, and it looks like a client timeout rather than a server bug, so it is easy to misattribute. Fix: call _setmode(_O_BINARY) on both descriptors at the top of cbm_mcp_server_run, guarded by #ifdef _WIN32. No behaviour change on POSIX. Verified on Windows 11 (26200): built and running continuously since 2026-07-30 with no further hangs. Note for maintainers: _O_BINARY is documented as coming from , which this file includes only in the #else (non-Windows) branch. It resolves today via transitive inclusion through , so the build is fine as-is -- but an explicit #include in the _WIN32 branch would make that independent of Windows SDK header layout. Left out of this patch to keep the change minimal; happy to add it if preferred. Signed-off-by: ahmadgamal15-art --- src/mcp/mcp.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c index 78706924c..60fc55f97 100644 --- a/src/mcp/mcp.c +++ b/src/mcp/mcp.c @@ -11177,6 +11177,13 @@ static int poll_for_input_unix(cbm_mcp_server_t *srv, int fd, FILE *in) { int cbm_mcp_server_run(cbm_mcp_server_t *srv, FILE *in, FILE *out) { int fd = cbm_fileno(in); +#ifdef _WIN32 + /* Ensure stdio is in binary mode to prevent CRLF translation from corrupting + * Content-Length byte counts and causing fread() to hang. */ + _setmode(cbm_fileno(in), _O_BINARY); + _setmode(cbm_fileno(out), _O_BINARY); +#endif + for (;;) { /* Poll with idle timeout so we can evict unused stores between requests. *