@@ -37,9 +37,17 @@ def run(self):
3737 si = subprocess .STARTUPINFO ()
3838 si .dwFlags |= subprocess .STARTF_USESHOWWINDOW
3939
40+ # select appropriate file handle for stderr
41+ # usually we want to redirect stderr to stdout, so erros are shown
42+ # in the output in the right place (where they actually occurred)
43+ # only if silenceErrors=True, we separate stderr from stdout and discard it
44+ stderrHandle = subprocess .STDOUT
45+ if self .silenceErrors :
46+ stderrHandle = subprocess .PIPE
47+
4048 self .process = subprocess .Popen (self .args ,
4149 stdout = subprocess .PIPE ,
42- stderr = subprocess . PIPE ,
50+ stderr = stderrHandle ,
4351 stdin = subprocess .PIPE ,
4452 env = os .environ .copy (),
4553 startupinfo = si )
@@ -52,6 +60,9 @@ def run(self):
5260 'replace' ).replace ('\r ' , '' ))
5361
5462 queryTimerEnd = time .time ()
63+ # we are done with the output, terminate the process
64+ self .process .terminate ()
65+
5566 if 'show_query' in self .options and self .options ['show_query' ]:
5667 resultInfo = "/*\n -- Executed querie(s) at {0} took {1:.3f}ms --" .format (
5768 str (time .strftime ("%Y-%m-%d %H:%M:%S" , time .localtime (queryTimerStart ))),
@@ -61,6 +72,10 @@ def run(self):
6172 resultInfo , resultLine , self .query , resultLine )
6273 return self .callback (resultString )
6374
75+ return
76+
77+ # regular mode is handled with more reliable Popen.communicate
78+ # which also terminates the process afterwards
6479 results , errors = self .process .communicate (input = self .query .encode ())
6580
6681 queryTimerEnd = time .time ()
@@ -110,6 +125,10 @@ def stop(self):
110125 if not self .process :
111126 return
112127
128+ # if poll returns None - proc still running, otherwise returns process return code
129+ if self .process .poll () is not None :
130+ return
131+
113132 try :
114133 # Windows does not provide SIGKILL, go with SIGTERM
115134 sig = getattr (signal , 'SIGKILL' , signal .SIGTERM )
0 commit comments