From 3c6f458debf570edc3d2486191148ea7ffdb84a3 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sun, 6 Dec 2026 16:29:54 +0300 Subject: [PATCH] MCP: make stdio reads cancellable and time-bounded on Windows Windows anonymous pipes have no non-blocking mode, so read_message could block forever ignoring the stop token and the I/O deadline. Wait for readable data with PeekNamedPipe (honouring cancellation and the timeout) before the blocking read. The stdio transport tests now use CRT pipe helpers on Windows; the framing test runs everywhere and the cancellation test covers the cancellable read on both platforms (the non-blocking-write half stays POSIX-only). --- src/MCP/Transport.cpp | 53 +++++++++++++++++++ tests/standalone/MCP/Transport.cpp | 85 ++++++++++++++++++++++-------- 2 files changed, 115 insertions(+), 23 deletions(-) diff --git a/src/MCP/Transport.cpp b/src/MCP/Transport.cpp index 7441140..cbdfec4 100644 --- a/src/MCP/Transport.cpp +++ b/src/MCP/Transport.cpp @@ -15,7 +15,14 @@ #include #if defined(_WIN32) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif #include +#include #else #include #include @@ -270,6 +277,46 @@ namespace Void::MCP } } } +#else + // Anonymous pipes on Windows have no non-blocking mode; wait for + // readable data with PeekNamedPipe so a stop request or the I/O + // deadline is honoured before the blocking read. + [[nodiscard]] Void::Result::Result + wait_fd_read(int fd, std::chrono::milliseconds timeout, + std::stop_token stop, std::string_view operation) + { + const HANDLE handle = + reinterpret_cast(::_get_osfhandle(fd)); + if (handle == INVALID_HANDLE_VALUE) { + return {}; + } + const auto deadline = std::chrono::steady_clock::now() + timeout; + for (;;) { + if (stop.stop_requested()) { + return std::unexpected{make_mcp_error( + McpErrorCode::cancelled, + std::format("MCP: stdio {} cancelled", operation))}; + } + DWORD available = 0; + if (::PeekNamedPipe(handle, nullptr, 0, nullptr, &available, + nullptr) != 0) { + if (available > 0) { + return {}; + } + } else { + // Broken pipe: the read reports EOF. Anything else is not + // a pipe, so fall back to the blocking read. + return {}; + } + if (timeout.count() > 0 && + std::chrono::steady_clock::now() >= deadline) { + return std::unexpected{make_mcp_error( + McpErrorCode::timeout, + std::format("MCP: stdio {} timed out", operation))}; + } + ::Sleep(10); + } + } #endif [[nodiscard]] Void::Result::Result @@ -621,6 +668,12 @@ namespace Void::MCP return std::optional{std::move(*message)}; } std::array buffer{}; +#if defined(_WIN32) + auto waited = wait_fd_read(impl.read_fd, impl.io_timeout, stop, "read"); + if (!waited.has_value()) { + return std::unexpected{std::move(waited.error())}; + } +#endif auto count = fd_read(impl.read_fd, buffer); if (!count.has_value()) { #if !defined(_WIN32) diff --git a/tests/standalone/MCP/Transport.cpp b/tests/standalone/MCP/Transport.cpp index dff54ff..a843ea4 100644 --- a/tests/standalone/MCP/Transport.cpp +++ b/tests/standalone/MCP/Transport.cpp @@ -15,10 +15,51 @@ #if !defined(_WIN32) #include #include +#else +#include +#include #endif namespace { + // POSIX-style pipe helpers over the CRT on Windows, so the stdio transport + // tests exercise the same code path on both platforms. +#if defined(_WIN32) + int make_pipe(int fds[2]) + { + return ::_pipe(fds, 65536, _O_BINARY); + } + long fd_write(int fd, const void* data, std::size_t size) + { + return ::_write(fd, data, static_cast(size)); + } + long fd_read(int fd, void* data, std::size_t size) + { + return ::_read(fd, data, static_cast(size)); + } + void fd_close(int fd) + { + ::_close(fd); + } +#else + int make_pipe(int fds[2]) + { + return ::pipe(fds); + } + ssize_t fd_write(int fd, const void* data, std::size_t size) + { + return ::write(fd, data, size); + } + ssize_t fd_read(int fd, void* data, std::size_t size) + { + return ::read(fd, data, size); + } + void fd_close(int fd) + { + ::close(fd); + } +#endif + using namespace std::chrono_literals; using Void::MCP::HttpRequest; using Void::MCP::HttpResponse; @@ -294,19 +335,16 @@ Void::Testing::Test mcp_line_framer_bounds_and_crlf() Void::Testing::Test mcp_stdio_framing_dispatch_and_parse_error() { -#ifdef _WIN32 - Void::Testing::skip("POSIX pipe-based stdio transport"); -#else int input[2]{}; int output[2]{}; - Void::Testing::require(::pipe(input) == 0); - Void::Testing::require(::pipe(output) == 0); + Void::Testing::require(make_pipe(input) == 0); + Void::Testing::require(make_pipe(output) == 0); const std::string frames = encode_request("tools/list") + "\n" + "not json\n"; - Void::Testing::require(::write(input[1], frames.data(), frames.size()) == - static_cast(frames.size())); - ::close(input[1]); + Void::Testing::require(fd_write(input[1], frames.data(), frames.size()) == + static_cast(frames.size())); + fd_close(input[1]); bool notified = false; Void::MCP::StdioTransport transport{ @@ -318,15 +356,15 @@ Void::Testing::Test mcp_stdio_framing_dispatch_and_parse_error() {}, 2s}; auto run = transport.run(); Void::Testing::require(run.has_value()); - ::close(output[1]); + fd_close(output[1]); std::string received; char buffer[4096]; - ssize_t count = 0; - while ((count = ::read(output[0], buffer, sizeof(buffer))) > 0) { + long count = 0; + while ((count = fd_read(output[0], buffer, sizeof(buffer))) > 0) { received.append(buffer, static_cast(count)); } - ::close(output[0]); + fd_close(output[0]); Void::MCP::LineFramer framer; Void::Testing::require(framer.append(received).has_value()); @@ -346,24 +384,24 @@ Void::Testing::Test mcp_stdio_framing_dispatch_and_parse_error() Void::Testing::require(error_response->error.has_value()); Void::Testing::expect_eq(error_response->error->code, std::int32_t{-32700}); -#endif } Void::Testing::Test mcp_stdio_cancellation_and_slow_reader() { -#ifdef _WIN32 - Void::Testing::skip("POSIX pipe-based stdio transport"); -#else - // Cancellation while waiting on an empty, non-blocking input pipe. + // Cancellation while waiting on an empty input pipe. { int input[2]{}; int output[2]{}; - Void::Testing::require(::pipe(input) == 0); - Void::Testing::require(::pipe(output) == 0); + Void::Testing::require(make_pipe(input) == 0); + Void::Testing::require(make_pipe(output) == 0); +#if !defined(_WIN32) + // The POSIX path only waits after a non-blocking read reports EAGAIN; + // Windows has no pipe O_NONBLOCK and waits before reading instead. const int flags = ::fcntl(input[0], F_GETFL, 0); Void::Testing::require(flags >= 0); Void::Testing::require( ::fcntl(input[0], F_SETFL, flags | O_NONBLOCK) == 0); +#endif Void::MCP::StdioTransport transport{ input[0], output[1], echo_handler, {}, {}, 2s}; @@ -382,12 +420,13 @@ Void::Testing::Test mcp_stdio_cancellation_and_slow_reader() static_cast( Void::MCP::McpErrorCode::cancelled)); Void::Testing::expect(elapsed < 1s); - ::close(input[0]); - ::close(input[1]); - ::close(output[0]); - ::close(output[1]); + fd_close(input[0]); + fd_close(input[1]); + fd_close(output[0]); + fd_close(output[1]); } +#if !defined(_WIN32) // Slow reader: a full non-blocking pipe makes the write time out. { int input[2]{};