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)
|
||||
{
|
||||
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<CommunicateResult, Void::Result::Error>
|
||||
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<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)
|
||||
{
|
||||
if (stop.stop_requested())
|
||||
@@ -2301,6 +2364,23 @@ Void::Result::Result<CommunicateResult, Void::Result::Error>
|
||||
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<CommunicateResult, Void::Result::Error>
|
||||
: static_cast<DWORD>(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<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);
|
||||
}
|
||||
};
|
||||
advance_stdin();
|
||||
if (!out_done)
|
||||
{
|
||||
capture(state.out, true, out_done);
|
||||
|
||||
Reference in new issue
Block a user