Skip to content

Commit 53ce82f

Browse files
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 <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>
1 parent ee9833d commit 53ce82f

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11177,6 +11177,13 @@ static int poll_for_input_unix(cbm_mcp_server_t *srv, int fd, FILE *in) {
1117711177
int cbm_mcp_server_run(cbm_mcp_server_t *srv, FILE *in, FILE *out) {
1117811178
int fd = cbm_fileno(in);
1117911179

11180+
#ifdef _WIN32
11181+
/* Ensure stdio is in binary mode to prevent CRLF translation from corrupting
11182+
* Content-Length byte counts and causing fread() to hang. */
11183+
_setmode(cbm_fileno(in), _O_BINARY);
11184+
_setmode(cbm_fileno(out), _O_BINARY);
11185+
#endif
11186+
1118011187
for (;;) {
1118111188
/* Poll with idle timeout so we can evict unused stores between requests.
1118211189
*

0 commit comments

Comments
 (0)