From 9f45841c7ddfa2150a1cb5df123b0de0227a52bf Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sun, 6 Dec 2026 15:42:31 +0300 Subject: [PATCH] Windows: port the process helper and fix a stdin deadlock - Tests: build VoidProcessHelper on Windows with the fork/poll-based modes compiled out, binary stdio, and a Windows fd listing; enable the ProcessStream suite there. - Process (Windows): communicate_blocking could block forever when the stdin write completed synchronously: the wait only armed pending operations, so the stdin pipe was never closed and the child never saw EOF. Complete ready operations before waiting, and report real EOF only when a read actually reached end of stream. --- src/System/ProcessWindows.cpp | 138 +++++++++++++--------- tests/CMakeLists.txt | 6 +- tests/helpers/ProcessHelper.cpp | 30 +++++ tests/standalone/System/ProcessStream.cpp | 1 - 4 files changed, 115 insertions(+), 60 deletions(-) diff --git a/src/System/ProcessWindows.cpp b/src/System/ProcessWindows.cpp index f15f7dd..b03d161 100644 --- a/src/System/ProcessWindows.cpp +++ b/src/System/ProcessWindows.cpp @@ -699,7 +699,9 @@ namespace } if (got == 0) { - return IoStatus::eof; + // Nothing ready is not EOF: a synchronous completion with zero + // bytes sets eof, a not-yet-completed read has neither. + return stream.eof ? IoStatus::eof : IoStatus::none; } bytes = got; return IoStatus::data; @@ -2270,6 +2272,67 @@ Void::Result::Result const bool has_timeout = timeout.count() > 0; const auto deadline = std::chrono::steady_clock::now() + timeout; + // One step of stdin progress: complete the write (sync or pending) and + // close the pipe as soon as the payload is delivered. + auto advance_stdin = [&] { + if (stdin_done) + { + return; + } + const std::size_t wrote = complete_write(state.in); + stdin_offset += wrote; + if (state.in.broken) + { + state.in.close(); + stdin_done = true; + } + else if (stdin_offset >= stdin_data.size()) + { + state.in.close(); + stdin_done = true; + } + }; + + auto capture = [&](WinStream& stream, bool is_out, bool& done) { + DWORD got = 0; + const IoStatus status = complete_read(stream, got); + if (status == IoStatus::eof) + { + done = true; + stream.close(); + return; + } + if (status == IoStatus::error) + { + done = true; + io_failure = win_error( + map_process_error(stream.error), + "Process::communicate_blocking: read failed", + stream.error); + stream.close(); + return; + } + if (status != IoStatus::data || got == 0) + { + return; + } + if (!handle_read_chunk( + reinterpret_cast(stream.read_buf.data()), + got, + is_out)) + { + aborted = true; + } + if (callbacks != nullptr) + { + emit_lines_from_offset( + is_out ? res.out : res.err, + is_out ? out_emitted : err_emitted, + is_out ? callbacks->on_stdout : callbacks->on_stderr, + false); + } + }; + while (!stdin_done || !out_done || !err_done) { if (stop.stop_requested()) @@ -2301,6 +2364,23 @@ Void::Result::Result issue_read(state.err, state.err.read_buf.size()); } + // Complete operations that finished synchronously before blocking on + // the still-pending ones: a completed stdin write must close the pipe + // even when every read is still pending, or the child never sees EOF. + advance_stdin(); + if (!out_done) + { + capture(state.out, true, out_done); + } + if (!err_done) + { + capture(state.err, false, err_done); + } + if (aborted || io_failure.has_value()) + { + break; + } + HANDLE events[3]{}; int count = 0; if (!stdin_done && state.in.write_pending) @@ -2346,61 +2426,7 @@ Void::Result::Result : static_cast(wait_ms)); } - if (!stdin_done) - { - const std::size_t wrote = complete_write(state.in); - stdin_offset += wrote; - if (state.in.broken) - { - state.in.close(); - stdin_done = true; - } - else if (stdin_offset >= stdin_data.size()) - { - state.in.close(); - stdin_done = true; - } - } - - auto capture = [&](WinStream& stream, bool is_out, bool& done) { - DWORD got = 0; - const IoStatus status = complete_read(stream, got); - if (status == IoStatus::eof) - { - done = true; - stream.close(); - return; - } - if (status == IoStatus::error) - { - done = true; - io_failure = win_error( - map_process_error(stream.error), - "Process::communicate_blocking: read failed", - stream.error); - stream.close(); - return; - } - if (status != IoStatus::data || got == 0) - { - return; - } - if (!handle_read_chunk( - reinterpret_cast(stream.read_buf.data()), - got, - is_out)) - { - aborted = true; - } - if (callbacks != nullptr) - { - emit_lines_from_offset( - is_out ? res.out : res.err, - is_out ? out_emitted : err_emitted, - is_out ? callbacks->on_stdout : callbacks->on_stderr, - false); - } - }; + advance_stdin(); if (!out_done) { capture(state.out, true, out_done); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0ea8a1c..516e9aa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -232,9 +232,9 @@ endif() # ── Test-support executables & libraries ────────────────────────────────────── # These are NOT test files; they are helper executables/libs referenced by tests. -# The process helper is POSIX-only (poll.h, fork/wait); Windows tests that -# need it are guarded or not built. -if(TARGET VoidTests AND (TARGET Void::System OR TARGET Void::IPC) AND NOT WIN32) +# The process helper builds everywhere; the fork/poll-based helper modes are +# POSIX-only and are compiled out on Windows. +if(TARGET VoidTests AND (TARGET Void::System OR TARGET Void::IPC)) add_executable(VoidProcessHelper "${CMAKE_CURRENT_SOURCE_DIR}/helpers/ProcessHelper.cpp") set_target_properties(VoidProcessHelper PROPERTIES diff --git a/tests/helpers/ProcessHelper.cpp b/tests/helpers/ProcessHelper.cpp index 486fc3a..1244948 100644 --- a/tests/helpers/ProcessHelper.cpp +++ b/tests/helpers/ProcessHelper.cpp @@ -1,5 +1,7 @@ #include +#if !defined(_WIN32) #include +#endif #include #include @@ -17,6 +19,17 @@ #include #include +#if defined(_WIN32) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#include +#endif + extern char** environ; namespace @@ -194,6 +207,7 @@ int mode_ignore_sigterm(int argc, char** argv) return 0; } +#if !defined(_WIN32) int mode_grandchild_ignore_sigterm(int argc, char** argv) { if (argc < 3) { @@ -358,6 +372,7 @@ int mode_simultaneous_io(int argc, char** argv) } return (in_read > 0 || total == 0) ? 0 : 0; } +#endif // !_WIN32 int mode_flood(int argc, char** argv) { @@ -415,9 +430,16 @@ int mode_check_fds() { std::vector open_fds; for (int fd = 3; fd <= 64; ++fd) { +#if defined(_WIN32) + const auto handle = ::_get_osfhandle(fd); + if (handle != -1) { + open_fds.push_back(fd); + } +#else if (::fcntl(fd, F_GETFD) != -1 || errno != EBADF) { open_fds.push_back(fd); } +#endif } if (open_fds.empty()) { std::cout << "clean\n"; @@ -536,6 +558,12 @@ int mode_ipc_pipe_echo() int main(int argc, char** argv) { std::ios::sync_with_stdio(false); +#if defined(_WIN32) + // Helpers speak over binary pipes: no CRLF translation or Ctrl-Z EOF. + (void)_setmode(_fileno(stdin), _O_BINARY); + (void)_setmode(_fileno(stdout), _O_BINARY); + (void)_setmode(_fileno(stderr), _O_BINARY); +#endif if (argc < 2) { return usage_error("missing mode"); } @@ -561,6 +589,7 @@ int main(int argc, char** argv) if (mode == "ignore-sigterm") { return mode_ignore_sigterm(argc, argv); } +#if !defined(_WIN32) if (mode == "grandchild-ignore-sigterm") { return mode_grandchild_ignore_sigterm(argc, argv); } @@ -570,6 +599,7 @@ int main(int argc, char** argv) if (mode == "simultaneous-io") { return mode_simultaneous_io(argc, argv); } +#endif if (mode == "flood") { return mode_flood(argc, argv); } diff --git a/tests/standalone/System/ProcessStream.cpp b/tests/standalone/System/ProcessStream.cpp index 9a0ad1c..acf7b30 100644 --- a/tests/standalone/System/ProcessStream.cpp +++ b/tests/standalone/System/ProcessStream.cpp @@ -1,4 +1,3 @@ -// @requires-posix: streaming tests drive the POSIX process helper. #include #include #include