Can you do it all on the current thread by something like the following pseudo code?
while !eof(stdin) || !eof(stderr) {
if !eof(stdin) read(stdin)
if !eof(stderr) read(stderr)
}
close(stdin)
close(stderr)
wait_for_process_termination_and_get_exit_code()
I suppose there’s a risk that the process could generate some more output after we’ve exited the while loop, which would cause the waitFor to block.
If we put the code to empty stdin and stderr on another thread, I assume that it must loop until after the main thread successfully gets the exit code, at which point the main thread must signal it to exit (needs a semaphore). That does mean that stdin and stderr can’t be explicitly closed, prior to the waitFor. Does the waitFor close the streams automatically?