Skip to content

Commit f2626f0

Browse files
committed
fix(mcp): exit promptly on stdin EOF over a FIFO (macOS poll deficiency)
The stdio server's input loop blocked in one poll() for the full idle timeout (STORE_IDLE_TIMEOUT_S = 60s). On macOS, poll() and select() do NOT report POLLIN/POLLHUP when a FIFO's last writer closes — only read() returns 0 there (verified with a standalone probe). So when a client closed the server's stdin over a named FIFO, the server did not observe the EOF until the 60s poll timed out and the loop's Phase-2 read() then returned 0 — i.e. it appeared to hang for up to a minute after the client went away. Bound the blocking poll to MCP_TIMEOUT_MS (~1s) instead of the full idle timeout so the existing read()-based EOF check runs within ~1s. Idle-store eviction (threshold STORE_IDLE_TIMEOUT_S) is idempotent, so checking it on each short tick is harmless. Real MCP clients connect stdin via a pipe, which macOS poll() does handle, so they were unaffected; FIFO-based transports (and the smoke clean-eof-exit invariant) hung. Reproduce-first: scripts/smoke-invariants.sh clean-eof-exit was RED (>12s EOF hang) and is now GREEN (exit 0 within bound); the full battery is 30/0. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 2a4fac6 commit f2626f0

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5563,8 +5563,9 @@ static int poll_for_input_unix(cbm_mcp_server_t *srv, int fd, FILE *in) {
55635563
/* Phase 2: peek FILE* buffer */
55645564
int saved_flags = fcntl(fd, F_GETFL);
55655565
if (saved_flags < 0) {
5566-
/* fcntl failed — fall through to blocking poll */
5567-
pr = poll(&pfd, SKIP_ONE, STORE_IDLE_TIMEOUT_S * MCP_TIMEOUT_MS);
5566+
/* fcntl failed — fall through to a short blocking poll (see the Phase-3
5567+
* note below on why the interval is bounded, not the full idle timeout) */
5568+
pr = poll(&pfd, SKIP_ONE, MCP_TIMEOUT_MS);
55685569
if (pr < 0) {
55695570
return CBM_NOT_FOUND;
55705571
}
@@ -5584,8 +5585,15 @@ static int poll_for_input_unix(cbm_mcp_server_t *srv, int fd, FILE *in) {
55845585
return CBM_NOT_FOUND; /* true EOF */
55855586
}
55865587
clearerr(in);
5587-
/* Phase 3: blocking poll */
5588-
pr = poll(&pfd, SKIP_ONE, STORE_IDLE_TIMEOUT_S * MCP_TIMEOUT_MS);
5588+
/* Phase 3: blocking poll, bounded to a SHORT interval (not the full idle
5589+
* timeout). macOS poll()/select() do NOT report POLLIN/POLLHUP when a
5590+
* FIFO's last writer closes — only read() returns 0 there (verified). A
5591+
* 60s poll would therefore leave the server blocked up to a full idle
5592+
* timeout after stdin EOF (a client that closes the pipe would appear to
5593+
* hang). Waking every MCP_TIMEOUT_MS lets the Phase-2 read() above detect
5594+
* the EOF within ~1s. Idle-store eviction (threshold STORE_IDLE_TIMEOUT_S)
5595+
* is idempotent, so checking it on each short tick is harmless. */
5596+
pr = poll(&pfd, SKIP_ONE, MCP_TIMEOUT_MS);
55895597
if (pr < 0) {
55905598
return CBM_NOT_FOUND;
55915599
}

0 commit comments

Comments
 (0)