fix(mcp): resolve poll/getline FILE* buffering mismatch causing tools/list hang#1
Open
halindrome wants to merge 5 commits intomainfrom
Open
fix(mcp): resolve poll/getline FILE* buffering mismatch causing tools/list hang#1halindrome wants to merge 5 commits intomainfrom
halindrome wants to merge 5 commits intomainfrom
Conversation
- Use O_NONBLOCK + clearerr() in Phase 2 fgetc probe to preserve the 60s idle eviction timeout when both kernel fd and FILE* buffer are empty (fgetc on a blocking fd would otherwise block indefinitely, bypassing Phase 3 poll timeout and preventing cbm_mcp_server_evict_idle) - Add #include <fcntl.h> for fcntl()/O_NONBLOCK - Fix comment: "two-phase" → "three-phase" (implementation has 3 phases) - Improve Python integration test: verify id:1 (initialize) and id:2 (tools/list) response IDs are both present, not just "tools" substring Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add explicit fallback path when fcntl(F_GETFL) fails: skip the FILE* peek and fall through directly to blocking poll so idle eviction still fires on timeout (Finding 1) - Strengthen C unit test: verify id:1 (initialize) and id:2 (tools/list) response IDs are both present, not just a substring match on "tools" (Finding 2) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Root Cause
The MCP server event loop in
src/mcp/mcp.c(cbm_mcp_server_run) mixespoll()on the raw file descriptor withgetline()on a bufferedFILE*. When Claude Code sendsinitialize+notifications/initialized+tools/listin rapid succession:poll()returnsPOLLIN— kernel fd has data.getline()call consumes all kernel data into libc's internalFILE*buffer (typically 4096 bytes in one read syscall).poll()sees an empty kernel fd and blocks forSTORE_IDLE_TIMEOUT_S= 60 seconds.Fix
Three-phase poll approach in the Unix event loop path:
poll()(timeout=0) — catches data in the kernel fd.fgetc(in)+ungetc()peek to detect data already buffered by libc from a priorgetline()over-read. If found, skip blocking poll and fall through togetline()immediately.poll()forSTORE_IDLE_TIMEOUT_S * 1000 ms.This is fully portable (POSIX
fgetc/ungetc), has no busy-loop, and preserves idle eviction semantics.Also corrects the inaccurate comment at the top of the loop that claimed the poll/getline mix "is safe in practice".
Test Evidence
Both complete well within the 5-second timeout (previously took 60 seconds).
The full test suite (2043 tests) passes including the new
mcp_server_run_rapid_messagesC unit test.Files Changed
src/mcp/mcp.c— three-phase event loop fix + corrected commenttests/test_mcp.c—mcp_server_run_rapid_messagesC unit test usingpipe()+alarm(5)scripts/test_mcp_rapid_init.py— Python integration test (spawns binary, sends 3 messages simultaneously, asserts response within 5s)