fix(mcp): set stdio to binary mode on Windows - #1424
Conversation
|
Thanks for opening this — it has been seen, and it is queued. This note is automated, but it is not a brush-off: it exists so you know where your PR stands instead of having to guess from silence. Current review status: working through a backlog. What that means for this PR, concretely:
Things that will genuinely speed it up whenever review does happen:
If this fixes a bug, a reproduction we can run is worth more than a description of the symptom. Thanks for contributing, and sorry in advance for the wait. |
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 <fcntl.h>, which this file includes only in the #else (non-Windows) branch. It resolves today via transitive inclusion through <windows.h>, so the build is fine as-is -- but an explicit #include <fcntl.h> 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 <ahmad.gamal15@gmail.com>
03c1bbe to
53ce82f
Compare
|
Thank you — a textbook fix for a genuinely nasty failure mode: CRLF translation silently desyncing Content-Length framing presents as a client timeout, so it's been easy to misattribute for anyone hitting it. Placement at the top of the server loop before any framing I/O is exactly right, and we verified nothing else in the codebase sets binary mode on this path. Your note about _O_BINARY resolving via transitive include is appreciated — we'll fold the explicit <fcntl.h> include into a follow-up cleanup rather than hold this up. Windows users owe you one! |
Problem
On Windows,
stdin/stdoutdefault to text mode, which translatesLF->CRLFon write andCRLF->LFon read. The MCP framing layer counts bytes forContent-Length, so any translated byte makes the declared length disagree with the actual payload. The subsequentfread()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 presents as 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 ofcbm_mcp_server_run, guarded by#ifdef _WIN32. No behaviour change on POSIX.7 lines, one file.
Verification
Built and running continuously on Windows 11 (build 26200) since 2026-07-30 with no further hangs, driving a ~70k-symbol index through the Claude Code MCP client.
Note for maintainers
_O_BINARYis documented as coming from<fcntl.h>, which this file includes only in the#else(non-Windows) branch —<io.h>is what the_WIN32branch gets. It resolves today via transitive inclusion through<windows.h>, so the build is fine as-is, but an explicit#include <fcntl.h>in the_WIN32branch would make that independent of Windows SDK header layout.I left it out to keep the change minimal — happy to add it if you'd prefer.