##// END OF EJS Templates
subprocessio: don't use __del__ to close the buffers and readers. Instead use a finally block....
dan -
r799:825a2f59 default
parent child
Show More
@@ -1137,11 +1137,12 class GitRemote(RemoteBase):
1137 cmd = [settings.GIT_EXECUTABLE] + _copts + cmd
1137 cmd = [settings.GIT_EXECUTABLE] + _copts + cmd
1138 _opts = {'env': gitenv, 'shell': False}
1138 _opts = {'env': gitenv, 'shell': False}
1139
1139
1140 proc = None
1140 try:
1141 try:
1141 _opts.update(opts)
1142 _opts.update(opts)
1142 p = subprocessio.SubprocessIOChunker(cmd, **_opts)
1143 proc = subprocessio.SubprocessIOChunker(cmd, **_opts)
1143
1144
1144 return ''.join(p), ''.join(p.error)
1145 return ''.join(proc), ''.join(proc.error)
1145 except (EnvironmentError, OSError) as err:
1146 except (EnvironmentError, OSError) as err:
1146 cmd = ' '.join(cmd) # human friendly CMD
1147 cmd = ' '.join(cmd) # human friendly CMD
1147 tb_err = ("Couldn't run git command (%s).\n"
1148 tb_err = ("Couldn't run git command (%s).\n"
@@ -1153,6 +1154,9 class GitRemote(RemoteBase):
1153 return '', err
1154 return '', err
1154 else:
1155 else:
1155 raise exceptions.VcsException()(tb_err)
1156 raise exceptions.VcsException()(tb_err)
1157 finally:
1158 if proc:
1159 proc.close()
1156
1160
1157 @reraise_safe_exceptions
1161 @reraise_safe_exceptions
1158 def install_hooks(self, wire, force=False):
1162 def install_hooks(self, wire, force=False):
@@ -216,9 +216,6 class BufferedGenerator(object):
216 except (GeneratorExit, StopIteration):
216 except (GeneratorExit, StopIteration):
217 pass
217 pass
218
218
219 def __del__(self):
220 self.close()
221
222 ####################
219 ####################
223 # Threaded reader's infrastructure.
220 # Threaded reader's infrastructure.
224 ####################
221 ####################
@@ -475,26 +472,23 class SubprocessIOChunker(object):
475 self._closed = True
472 self._closed = True
476 try:
473 try:
477 self.process.terminate()
474 self.process.terminate()
478 except:
475 except Exception:
479 pass
476 pass
480 if self._close_input_fd:
477 if self._close_input_fd:
481 os.close(self._close_input_fd)
478 os.close(self._close_input_fd)
482 try:
479 try:
483 self.output.close()
480 self.output.close()
484 except:
481 except Exception:
485 pass
482 pass
486 try:
483 try:
487 self.error.close()
484 self.error.close()
488 except:
485 except Exception:
489 pass
486 pass
490 try:
487 try:
491 os.close(self.inputstream)
488 os.close(self.inputstream)
492 except:
489 except Exception:
493 pass
490 pass
494
491
495 def __del__(self):
496 self.close()
497
498
492
499 def run_command(arguments, env=None):
493 def run_command(arguments, env=None):
500 """
494 """
@@ -506,18 +500,20 def run_command(arguments, env=None):
506
500
507 cmd = arguments
501 cmd = arguments
508 log.debug('Running subprocessio command %s', cmd)
502 log.debug('Running subprocessio command %s', cmd)
503 proc = None
509 try:
504 try:
510 _opts = {'shell': False, 'fail_on_stderr': False}
505 _opts = {'shell': False, 'fail_on_stderr': False}
511 if env:
506 if env:
512 _opts.update({'env': env})
507 _opts.update({'env': env})
513 p = SubprocessIOChunker(cmd, **_opts)
508 proc = SubprocessIOChunker(cmd, **_opts)
514 stdout = ''.join(p)
509 return ''.join(proc), ''.join(proc.error)
515 stderr = ''.join(''.join(p.error))
516 except (EnvironmentError, OSError) as err:
510 except (EnvironmentError, OSError) as err:
517 cmd = ' '.join(cmd) # human friendly CMD
511 cmd = ' '.join(cmd) # human friendly CMD
518 tb_err = ("Couldn't run subprocessio command (%s).\n"
512 tb_err = ("Couldn't run subprocessio command (%s).\n"
519 "Original error was:%s\n" % (cmd, err))
513 "Original error was:%s\n" % (cmd, err))
520 log.exception(tb_err)
514 log.exception(tb_err)
521 raise Exception(tb_err)
515 raise Exception(tb_err)
516 finally:
517 if proc:
518 proc.close()
522
519
523 return stdout, stderr
General Comments 0
You need to be logged in to leave comments. Login now