2222#include " shell_utils.hpp"
2323#include " utf8_to_utf16.hpp"
2424
25+ #define HERE (format, ...) \
26+ printf (" [%s:%d] " format " \n " , __FILE__, __LINE__, ##__VA_ARGS__)
2527
2628using std::nullptr_t;
2729
@@ -206,7 +208,7 @@ namespace subprocess {
206208 }
207209 void Popen::init (CommandLine& command, RunOptions& options) {
208210 ProcessBuilder builder;
209-
211+ HERE ( " initializing builder " );
210212 builder.cin_option = get_pipe_option (options.cin );
211213 builder.cout_option = get_pipe_option (options.cout );
212214 builder.cerr_option = get_pipe_option (options.cerr );
@@ -230,19 +232,22 @@ namespace subprocess {
230232 builder.new_process_group = options.new_process_group ;
231233 builder.env = options.env ;
232234 builder.cwd = options.cwd ;
233-
235+ HERE ( " running builder " );
234236 *this = builder.run_command (command);
235237
238+ HERE (" setting up redirects" );
236239 cin_thread = setup_redirect_stream (options.cin , cin);
237240 cout_thread = setup_redirect_stream (cout, options.cout );
238241 cerr_thread = setup_redirect_stream (cerr, options.cerr );
242+ HERE (" closing unused pipes" );
239243 // the background thread will take ownership and auto close the pipe
240244 if (cin_thread.joinable ())
241245 cin = kBadPipeValue ;
242246 if (cout_thread.joinable ())
243247 cout = kBadPipeValue ;
244248 if (cerr_thread.joinable ())
245249 cerr = kBadPipeValue ;
250+ HERE (" done start" );
246251 }
247252
248253 Popen::Popen (Popen&& other) {
@@ -586,10 +591,13 @@ namespace subprocess {
586591 }
587592
588593 CompletedProcess run (CommandLine command, RunOptions options) {
594+ HERE (" starting command" );
589595 Popen popen (command, std::move (options));
596+ HERE (" it started" );
590597 CompletedProcess completed;
591598 std::thread cout_thread;
592599 std::thread cerr_thread;
600+ HERE (" starting threads\n " );
593601 if (popen.cout != kBadPipeValue ) {
594602 cout_thread = std::thread ([&]() {
595603 try {
@@ -611,6 +619,7 @@ namespace subprocess {
611619 });
612620 }
613621
622+ HERE (" joining threads" );
614623 if (cout_thread.joinable ()) {
615624 cout_thread.join ();
616625 }
@@ -619,26 +628,33 @@ namespace subprocess {
619628 }
620629
621630 try {
631+ HERE (" waiting for timeout" );
622632 popen.wait (options.timeout );
623633 } catch (subprocess::TimeoutExpired& expired) {
634+ HERE (" caught signal" );
624635 popen.send_signal (subprocess::SigNum::PSIGTERM );
625636 /* python source code sends SIGKILL, we'll be a bit more nice.
626637 give it a bit of time to terminate. This is more practical.
627638 */
628639 try {
640+ HERE (" graceful wait" );
629641 popen.wait (1.0 /20.0 );
630642 } catch (subprocess::TimeoutExpired& expired) {
643+ HERE (" dirty kill" );
631644 popen.kill ();
632645 }
646+ HERE (" final wait" );
633647 popen.wait ();
634648 subprocess::TimeoutExpired timeout (" subprocess::run timeout reached" );
635649 timeout.cmd = command;
636650 timeout.timeout = options.timeout ;
637651 timeout.cout = std::move (completed.cout );
638652 timeout.cerr = std::move (completed.cerr );
653+ HERE (" second throw" );
639654 throw timeout;
640655 }
641656
657+ HERE (" all good" );
642658 completed.returncode = popen.returncode ;
643659 completed.args = command;
644660 if (options.check && completed.returncode != 0 ) {
@@ -647,6 +663,7 @@ namespace subprocess {
647663 error.returncode = completed.returncode ;
648664 error.cout = std::move (completed.cout );
649665 error.cerr = std::move (completed.cerr );
666+ HERE (" check bad" );
650667 throw error;
651668 }
652669 return completed;
0 commit comments