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.
This commit is contained in:
1 parent
b3c959b920
commit
9f45841c7d
4 files changed
+115
-60
No files matched your search
@@ -699,7 +699,9 @@ namespace
|
|||||||
}
|
}
|
||||||
if (got == 0)
|
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;
|
bytes = got;
|
||||||
return IoStatus::data;
|
return IoStatus::data;
|
||||||
@@ -2270,6 +2272,67 @@ Void::Result::Result<CommunicateResult, Void::Result::Error>
|
|||||||
const bool has_timeout = timeout.count() > 0;
|
const bool has_timeout = timeout.count() > 0;
|
||||||
const auto deadline = std::chrono::steady_clock::now() + timeout;
|
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<const char*>(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)
|
while (!stdin_done || !out_done || !err_done)
|
||||||
{
|
{
|
||||||
if (stop.stop_requested())
|
if (stop.stop_requested())
|
||||||
@@ -2301,6 +2364,23 @@ Void::Result::Result<CommunicateResult, Void::Result::Error>
|
|||||||
issue_read(state.err, state.err.read_buf.size());
|
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]{};
|
HANDLE events[3]{};
|
||||||
int count = 0;
|
int count = 0;
|
||||||
if (!stdin_done && state.in.write_pending)
|
if (!stdin_done && state.in.write_pending)
|
||||||
@@ -2346,61 +2426,7 @@ Void::Result::Result<CommunicateResult, Void::Result::Error>
|
|||||||
: static_cast<DWORD>(wait_ms));
|
: static_cast<DWORD>(wait_ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stdin_done)
|
advance_stdin();
|
||||||
{
|
|
||||||
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<const char*>(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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (!out_done)
|
if (!out_done)
|
||||||
{
|
{
|
||||||
capture(state.out, true, out_done);
|
capture(state.out, true, out_done);
|
||||||
|
|||||||
@@ -232,9 +232,9 @@ endif()
|
|||||||
# ── Test-support executables & libraries ──────────────────────────────────────
|
# ── Test-support executables & libraries ──────────────────────────────────────
|
||||||
# These are NOT test files; they are helper executables/libs referenced by tests.
|
# 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
|
# The process helper builds everywhere; the fork/poll-based helper modes are
|
||||||
# need it are guarded or not built.
|
# POSIX-only and are compiled out on Windows.
|
||||||
if(TARGET VoidTests AND (TARGET Void::System OR TARGET Void::IPC) AND NOT WIN32)
|
if(TARGET VoidTests AND (TARGET Void::System OR TARGET Void::IPC))
|
||||||
add_executable(VoidProcessHelper
|
add_executable(VoidProcessHelper
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/helpers/ProcessHelper.cpp")
|
"${CMAKE_CURRENT_SOURCE_DIR}/helpers/ProcessHelper.cpp")
|
||||||
set_target_properties(VoidProcessHelper PROPERTIES
|
set_target_properties(VoidProcessHelper PROPERTIES
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#if !defined(_WIN32)
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
|
#endif
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -17,6 +19,17 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#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>
|
||||||
|
#endif
|
||||||
|
|
||||||
extern char** environ;
|
extern char** environ;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
@@ -194,6 +207,7 @@ int mode_ignore_sigterm(int argc, char** argv)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(_WIN32)
|
||||||
int mode_grandchild_ignore_sigterm(int argc, char** argv)
|
int mode_grandchild_ignore_sigterm(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
@@ -358,6 +372,7 @@ int mode_simultaneous_io(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
return (in_read > 0 || total == 0) ? 0 : 0;
|
return (in_read > 0 || total == 0) ? 0 : 0;
|
||||||
}
|
}
|
||||||
|
#endif // !_WIN32
|
||||||
|
|
||||||
int mode_flood(int argc, char** argv)
|
int mode_flood(int argc, char** argv)
|
||||||
{
|
{
|
||||||
@@ -415,9 +430,16 @@ int mode_check_fds()
|
|||||||
{
|
{
|
||||||
std::vector<int> open_fds;
|
std::vector<int> open_fds;
|
||||||
for (int fd = 3; fd <= 64; ++fd) {
|
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) {
|
if (::fcntl(fd, F_GETFD) != -1 || errno != EBADF) {
|
||||||
open_fds.push_back(fd);
|
open_fds.push_back(fd);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if (open_fds.empty()) {
|
if (open_fds.empty()) {
|
||||||
std::cout << "clean\n";
|
std::cout << "clean\n";
|
||||||
@@ -536,6 +558,12 @@ int mode_ipc_pipe_echo()
|
|||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
std::ios::sync_with_stdio(false);
|
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) {
|
if (argc < 2) {
|
||||||
return usage_error("missing mode");
|
return usage_error("missing mode");
|
||||||
}
|
}
|
||||||
@@ -561,6 +589,7 @@ int main(int argc, char** argv)
|
|||||||
if (mode == "ignore-sigterm") {
|
if (mode == "ignore-sigterm") {
|
||||||
return mode_ignore_sigterm(argc, argv);
|
return mode_ignore_sigterm(argc, argv);
|
||||||
}
|
}
|
||||||
|
#if !defined(_WIN32)
|
||||||
if (mode == "grandchild-ignore-sigterm") {
|
if (mode == "grandchild-ignore-sigterm") {
|
||||||
return mode_grandchild_ignore_sigterm(argc, argv);
|
return mode_grandchild_ignore_sigterm(argc, argv);
|
||||||
}
|
}
|
||||||
@@ -570,6 +599,7 @@ int main(int argc, char** argv)
|
|||||||
if (mode == "simultaneous-io") {
|
if (mode == "simultaneous-io") {
|
||||||
return mode_simultaneous_io(argc, argv);
|
return mode_simultaneous_io(argc, argv);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
if (mode == "flood") {
|
if (mode == "flood") {
|
||||||
return mode_flood(argc, argv);
|
return mode_flood(argc, argv);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// @requires-posix: streaming tests drive the POSIX process helper.
|
|
||||||
#include <Void/System/Process.hpp>
|
#include <Void/System/Process.hpp>
|
||||||
#include <Void/Testing/Assert.hpp>
|
#include <Void/Testing/Assert.hpp>
|
||||||
#include <Void/Testing/Suite.hpp>
|
#include <Void/Testing/Suite.hpp>
|
||||||
|
|||||||
Reference in new issue
Block a user