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:
1 parent
9f45841c7d
commit
3c6f458deb
2 files changed
+115
-23
No files matched your search
@@ -15,7 +15,14 @@
|
||||
#include <variant>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <io.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
@@ -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<void>
|
||||
wait_fd_read(int fd, std::chrono::milliseconds timeout,
|
||||
std::stop_token stop, std::string_view operation)
|
||||
{
|
||||
const HANDLE handle =
|
||||
reinterpret_cast<HANDLE>(::_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<std::size_t>
|
||||
@@ -621,6 +668,12 @@ namespace Void::MCP
|
||||
return std::optional<Message>{std::move(*message)};
|
||||
}
|
||||
std::array<std::byte, 8192> 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)
|
||||
|
||||
@@ -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]{};
|
||||
|
||||
Reference in new issue
Block a user