Skip to content

Commit 5ca5f32

Browse files
fix(mcp): address QA round 1 findings
- 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>
1 parent 16552be commit 5ca5f32

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

scripts/test_mcp_rapid_init.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,28 @@ def main():
6565

6666
output = stdout_data.decode("utf-8", errors="replace")
6767

68+
# Expect exactly 2 JSON responses: id:1 (initialize) and id:2 (tools/list).
69+
# notifications/initialized has no id and produces no response.
70+
lines = [ln.strip() for ln in output.splitlines() if ln.strip()]
71+
import json as _json
72+
json_lines = []
73+
for ln in lines:
74+
try:
75+
json_lines.append(_json.loads(ln))
76+
except _json.JSONDecodeError:
77+
pass
78+
79+
ids = {obj.get("id") for obj in json_lines if "id" in obj}
80+
if 1 not in ids:
81+
print("FAIL: missing initialize response (id:1) in server output")
82+
print(f"Server output was:\n{output!r}")
83+
sys.exit(1)
84+
if 2 not in ids:
85+
print("FAIL: missing tools/list response (id:2) in server output")
86+
print(f"Server output was:\n{output!r}")
87+
sys.exit(1)
6888
if "tools" not in output:
69-
print("FAIL: tools/list response not found in server output")
89+
print("FAIL: tools/list response body missing 'tools' key")
7090
print(f"Server output was:\n{output!r}")
7191
sys.exit(1)
7292

src/mcp/mcp.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <sys/unistd.h>
2929
#include <sys/poll.h>
3030
#include <poll.h>
31+
#include <fcntl.h>
3132
#endif
3233
#include <yyjson/yyjson.h>
3334
#include <stdint.h> // int64_t
@@ -2383,10 +2384,14 @@ int cbm_mcp_server_run(cbm_mcp_server_t *srv, FILE *in, FILE *out) {
23832384
* empty kernel fd and block for STORE_IDLE_TIMEOUT_S seconds even
23842385
* though the next messages are already in the FILE* buffer.
23852386
*
2386-
* Fix (Unix): use a two-phase approach —
2387+
* Fix (Unix): use a three-phase approach —
23872388
* Phase 1: non-blocking poll (timeout=0) to check the kernel fd.
23882389
* Phase 2: if Phase 1 returns 0, peek the FILE* buffer via fgetc/
23892390
* ungetc to detect data buffered by a prior getline() call.
2391+
* The fd is temporarily set O_NONBLOCK so fgetc() returns
2392+
* immediately (EAGAIN → EOF + ferror) instead of blocking
2393+
* when the FILE* buffer is empty, which would otherwise
2394+
* bypass the Phase 3 idle eviction timeout.
23902395
* Phase 3: only if both phases confirm no data, do blocking poll. */
23912396
#ifdef _WIN32
23922397
/* Windows: WaitForSingleObject on stdin handle */
@@ -2414,14 +2419,26 @@ int cbm_mcp_server_run(cbm_mcp_server_t *srv, FILE *in, FILE *out) {
24142419
if (pr == 0) {
24152420
/* Raw fd appears empty. Check whether libc has already buffered
24162421
* data from a previous over-read by peeking one byte via fgetc.
2417-
* If successful, push it back and proceed to getline without
2418-
* blocking. If not (EOF or EAGAIN), do the blocking poll. */
2422+
* IMPORTANT: temporarily set O_NONBLOCK so fgetc() returns
2423+
* immediately when the FILE* buffer AND kernel fd are both empty
2424+
* (EAGAIN → EOF + ferror). Without this, fgetc() on a blocking fd
2425+
* would block indefinitely, preventing the Phase 3 idle eviction
2426+
* timeout from ever firing. */
2427+
int saved_flags = fcntl(fd, F_GETFL);
2428+
if (saved_flags >= 0) {
2429+
(void)fcntl(fd, F_SETFL, saved_flags | O_NONBLOCK);
2430+
}
24192431
int c = fgetc(in);
2432+
if (saved_flags >= 0) {
2433+
(void)fcntl(fd, F_SETFL, saved_flags); /* restore blocking */
2434+
}
24202435
if (c == EOF) {
24212436
if (feof(in)) {
24222437
break; /* true EOF */
24232438
}
2424-
/* No buffered data and fd is empty — do blocking poll */
2439+
/* No buffered data (EAGAIN from non-blocking read) — clear the
2440+
* ferror indicator set by EAGAIN, then do the blocking poll. */
2441+
clearerr(in);
24252442
pr = poll(&pfd, 1, STORE_IDLE_TIMEOUT_S * 1000);
24262443
if (pr < 0) {
24272444
break;

0 commit comments

Comments
 (0)