Skip to content

fix(mcp): set stdio to binary mode on Windows - #1424

Merged
DeusData merged 1 commit into
DeusData:mainfrom
ahmadgamal15-art:fix/windows-stdio-binary-mode
Aug 4, 2026
Merged

fix(mcp): set stdio to binary mode on Windows#1424
DeusData merged 1 commit into
DeusData:mainfrom
ahmadgamal15-art:fix/windows-stdio-binary-mode

Conversation

@ahmadgamal15-art

Copy link
Copy Markdown

Problem

On Windows, stdin/stdout default to text mode, which translates LF -> CRLF on write and CRLF -> 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 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 of cbm_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_BINARY is documented as coming from <fcntl.h>, which this file includes only in the #else (non-Windows) branch — <io.h> is what the _WIN32 branch 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 _WIN32 branch 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.

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

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. 0.9.1-rc.1 is out, so the release freeze that held reviews is over — but it left a large queue of open pull requests behind it, and we are reading through them oldest-first. The background is in discussion #1144.

What that means for this PR, concretely:

  • It will not be closed for inactivity. No stale bot touches pull requests here.
  • It may still sit a while before a human reads it. That is on us, not on you.
  • Older PRs are read first, so a recent one is not being skipped — it is behind a queue.

Things that will genuinely speed it up whenever review does happen:

  • Keep it rebased on main — the tree is moving quickly right now, and a conflicting branch cannot be reviewed as the diff you intended.
  • Get CI green, or say which failures you believe are pre-existing.
  • Keep the change to one claim. Bundled features and refactors get split before they get merged, which costs you a round trip.
  • Every commit needs a sign-off (git commit -s) — CI enforces DCO.

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>
@ahmadgamal15-art
ahmadgamal15-art force-pushed the fix/windows-stdio-binary-mode branch from 03c1bbe to 53ce82f Compare August 3, 2026 16:28
@DeusData

DeusData commented Aug 4, 2026

Copy link
Copy Markdown
Owner

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!

@DeusData
DeusData merged commit 2cf4235 into DeusData:main Aug 4, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants