fix: use non-blocking stdout writes in stdio_server to prevent event loop deadlock#2070
Open
retospect wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: use non-blocking stdout writes in stdio_server to prevent event loop deadlock#2070retospect wants to merge 1 commit intomodelcontextprotocol:mainfrom
retospect wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
…loop deadlock When a tool returns a response larger than the OS pipe buffer (64 KB on macOS), stdout_writer blocks the entire event loop on write() because anyio.wrap_file delegates to a synchronous write on a blocking fd. Fix: set stdout fd to non-blocking mode and write in 4 KB chunks via os.write(), catching BlockingIOError (EAGAIN) and yielding to the event loop before retrying. Custom stdout overrides use the original path. Closes modelcontextprotocol#547
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: use non-blocking stdout writes in stdio_server to prevent event loop deadlock
Problem
When an MCP server tool returns a response larger than the OS pipe buffer (64 KB on macOS),
stdout_writerblocks the entire event loop on theawait stdout.write()call. This happens becauseanyio.wrap_filedelegates to a synchronouswrite()on a blocking fd — if the pipe buffer is full (client hasn't read yet), the write syscall blocks, and no other async tasks can run.In practice this manifests as:
list_paperswith 500+ entries returning ~74 KB of JSON)Reported in #547.
Root cause
anyio.wrap_file(TextIOWrapper(sys.stdout.buffer))wraps the synchronous file in a thread worker, but the underlyingwrite()still blocks when the kernel pipe buffer is full. Since MCP stdio transport is a single pipe between server and client, the client must read before the server can write more — but the server can't process the client's next read request because the event loop is blocked on the write.Fix
For the default stdout path (no custom override):
os.set_blocking(fd, False))os.write(), catchingBlockingIOError(EAGAIN) and yielding to the event loop withawait anyio.sleep(0.005)before retryingThis ensures the event loop never blocks on a pipe-full condition. The 4 KB chunk size is well below the 64 KB macOS pipe buffer, so most writes complete in a single syscall. When the buffer fills, the coroutine yields and retries after the client drains some data.
Custom stdout overrides (the
stdoutparameter) use the originalanyio.wrap_filepath unchanged.Testing
Tested in production with an MCP server managing 500+ research papers, where
list_papersregularly returns 60-80 KB responses. Before this fix, the server would hang ~1 in 3 calls. After the fix, zero hangs over weeks of use.Closes #547