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).
This commit is contained in:
faumaray committed 2026-12-06 16:29:54 +03:00
1 parent 9f45841c7d
commit 3c6f458deb
2 files changed
+115 -23

No files matched your search

+62 -23
View File
@@ -15,10 +15,51 @@
#if !defined(_WIN32)
#include <fcntl.h>
#include <unistd.h>
#else
#include <fcntl.h>
#include <io.h>
#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<unsigned int>(size));
}
long fd_read(int fd, void* data, std::size_t size)
{
return ::_read(fd, data, static_cast<unsigned int>(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<ssize_t>(frames.size()));
::close(input[1]);
Void::Testing::require(fd_write(input[1], frames.data(), frames.size()) ==
static_cast<long>(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<std::size_t>(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<std::int32_t>(
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]{};