Skip to content

Commit c565d1d

Browse files
authored
Stderr: overhaul error piping from child (#25)
We were not reading stderr from subprocesses (which could cause a hang). Signed-off-by: John Parent <john.parent@kitware.com>
1 parent b6d26aa commit c565d1d

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

Makefile

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,26 @@ test_relocate_dll: build_and_check_test_sample
147147
.\tester.exe
148148
cd ../..
149149

150-
test_pipe_overflow: build_and_check_test_sample
151-
echo "--------------------"
152-
echo " Pipe overflow test"
153-
echo "--------------------"
150+
test_pipe_out_overflow: build_and_check_test_sample
151+
@echo \n
152+
@echo ---------------------------
153+
@echo Pipe stdout overflow test
154+
@echo ---------------------------
154155
set SPACK_CC_TMP=%SPACK_CC%
155156
set SPACK_CC=$(MAKEDIR)\test\lots-of-output.bat
156157
cl /c /EHsc "test\src file\calc.cxx"
157158
set SPACK_CC=%SPACK_CC_TMP%
158159

160+
test_pipe_error_overflow: build_and_check_test_sample
161+
@echo \n
162+
@echo ---------------------------
163+
@echo Pipe stderr overflow test
164+
@echo ---------------------------
165+
set SPACK_CC_TMP=%SPACK_CC%
166+
set SPACK_CC=$(MAKEDIR)\test\lots-of-error.bat
167+
cl /c /EHsc "test\src file\calc.cxx"
168+
set SPACK_CC=%SPACK_CC_TMP%
169+
159170
build_zerowrite_test: test\writezero.obj
160171
link $(LFLAGS) $** $(API_LIBS) /out:writezero.exe
161172

@@ -247,7 +258,7 @@ test_def_file_name_override:
247258
test_and_cleanup: test clean-test
248259

249260

250-
test: test_wrapper test_relocate_exe test_relocate_dll test_def_file_name_override test_exe_with_exports test_long_paths test_pipe_overflow
261+
test: test_wrapper test_relocate_exe test_relocate_dll test_def_file_name_override test_exe_with_exports test_long_paths test_pipe_out_overflow test_pipe_error_overflow
251262

252263

253264
clean : clean-test clean-cl

src/execute.cxx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ enum : std::uint16_t { InvalidExitCode = 999 };
3131
ExecuteCommand::ExecuteCommand(std::string command)
3232
: ChildStdOut_Rd(nullptr),
3333
ChildStdOut_Wd(nullptr),
34+
ChildStdErr_Rd(nullptr),
35+
ChildStdErr_Wd(nullptr),
3436
base_command(std::move(command)) {
3537
this->CreateChildPipes();
3638
this->SetupExecute();
@@ -39,6 +41,8 @@ ExecuteCommand::ExecuteCommand(std::string command)
3941
ExecuteCommand::ExecuteCommand(std::string arg, const StrList& args)
4042
: ChildStdOut_Rd(nullptr),
4143
ChildStdOut_Wd(nullptr),
44+
ChildStdErr_Rd(nullptr),
45+
ChildStdErr_Wd(nullptr),
4246
base_command(std::move(arg)) {
4347
for (const auto& argp : args) {
4448
this->command_args.push_back(argp);
@@ -51,6 +55,8 @@ ExecuteCommand& ExecuteCommand::operator=(
5155
ExecuteCommand&& execute_command) noexcept {
5256
this->ChildStdOut_Rd = std::move(execute_command.ChildStdOut_Rd);
5357
this->ChildStdOut_Wd = std::move(execute_command.ChildStdOut_Wd);
58+
this->ChildStdErr_Rd = std::move(execute_command.ChildStdErr_Rd);
59+
this->ChildStdErr_Wd = std::move(execute_command.ChildStdErr_Wd);
5460
this->procInfo = std::move(execute_command.procInfo);
5561
this->startInfo = std::move(execute_command.startInfo);
5662
this->saAttr = std::move(execute_command.saAttr);
@@ -59,6 +65,7 @@ ExecuteCommand& ExecuteCommand::operator=(
5965
this->base_command = std::move(execute_command.base_command);
6066
this->command_args = std::move(execute_command.command_args);
6167
this->child_out_future = std::move(execute_command.child_out_future);
68+
this->child_err_future = std::move(execute_command.child_err_future);
6269
this->exit_code_future = std::move(exit_code_future);
6370
return *this;
6471
}
@@ -76,7 +83,7 @@ void ExecuteCommand::SetupExecute() {
7683
// This structure specifies the STDIN and STDOUT handles for redirection.
7784
ZeroMemory(&si_start_info, sizeof(STARTUPINFOW));
7885
si_start_info.cb = sizeof(STARTUPINFOW);
79-
si_start_info.hStdError = this->ChildStdOut_Wd;
86+
si_start_info.hStdError = this->ChildStdErr_Wd;
8087
si_start_info.hStdOutput = this->ChildStdOut_Wd;
8188
si_start_info.dwFlags |= STARTF_USESTDHANDLES;
8289
this->procInfo = pi_proc_info;
@@ -162,6 +169,7 @@ bool ExecuteCommand::ExecuteToolChainChild() {
162169
// determine when child proc is done
163170
free(nc_command_line);
164171
CloseHandle(this->ChildStdOut_Wd);
172+
CloseHandle(this->ChildStdErr_Wd);
165173
return true;
166174
}
167175

test/lots-of-error.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo OFF
2+
3+
for /l %%x in (1, 1, 1250) do echo Test boilerplate output to fill stderr line number %%x 1>&2

0 commit comments

Comments
 (0)