From 85b13d0393133c2b40b1589263d027feb09efa6d Mon Sep 17 00:00:00 2001 From: John Siirola Date: Wed, 16 Jan 2019 06:35:20 -0700 Subject: [PATCH] Fix UnboundLocalError (subprocess died before reader threads started). This resolves an exception that is due to a timing error where a subprocess can die before the O/I reader threads are launched. The cleanup routines need to verify that the threads were created before joining them. --- pyutilib/subprocess/processmngr.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pyutilib/subprocess/processmngr.py b/pyutilib/subprocess/processmngr.py index dff05aa5..8171206f 100644 --- a/pyutilib/subprocess/processmngr.py +++ b/pyutilib/subprocess/processmngr.py @@ -558,6 +558,7 @@ def run_command(cmd, simpleCase = False out_th = [] + th = None GlobalData.signal_handler_busy = False if simpleCase: # @@ -687,10 +688,16 @@ def run_command(cmd, # for p in out_th: os.close(p[2]) - th.join() + if th is not None: + # Note, there is a condition where the subprocess can die + # very quickly (raising an OSError) before the reader + # threads have a chance to be set up. Testing for None + # avoids joining a thread that doesn't exist. + th.join() for p in out_th: os.close(p[1]) - del th + if th is not None: + del th if outfile is not None: stdout_arg.close() elif tmpfile is not None and not ignore_output: