Index: Lib/popen2.py =================================================================== --- Lib/popen2.py (revision 43268) +++ Lib/popen2.py (working copy) @@ -20,7 +20,13 @@ def _cleanup(): for inst in _active[:]: - inst.poll() + if inst.poll() >= 0: + try: + _active.remove(inst) + except ValueError: + # This can happen if two threads create a new Popen instance. + # It's harmless that it was already removed, so ignore. + pass class Popen3: """Class representing a child process. Normally instances are created @@ -61,8 +67,14 @@ self.childerr = os.fdopen(errout, 'r', bufsize) else: self.childerr = None - _active.append(self) + def __del__(self): + # In case the child hasn't been waited on, check if it's done. + self.poll() + if self.sts < 0: + # Child is still running, keep us alive until we can wait on it. + _active.append(self) + def _run_child(self, cmd): if isinstance(cmd, basestring): cmd = ['/bin/sh', '-c', cmd] @@ -84,7 +96,6 @@ pid, sts = os.waitpid(self.pid, os.WNOHANG) if pid == self.pid: self.sts = sts - _active.remove(self) except os.error: pass return self.sts @@ -95,7 +106,6 @@ pid, sts = os.waitpid(self.pid, 0) if pid == self.pid: self.sts = sts - _active.remove(self) return self.sts @@ -117,7 +127,6 @@ self.tochild = os.fdopen(p2cwrite, 'w', bufsize) os.close(c2pwrite) self.fromchild = os.fdopen(c2pread, 'r', bufsize) - _active.append(self) if sys.platform[:3] == "win" or sys.platform == "os2emx": @@ -220,6 +229,7 @@ raise ValueError("unexpected %r on stderr" % (got,)) for inst in _active[:]: inst.wait() + _cleanup() if _active: raise ValueError("_active not empty") print "All OK" Index: Lib/test/test_popen2.py =================================================================== --- Lib/test/test_popen2.py (revision 43268) +++ Lib/test/test_popen2.py (working copy) @@ -65,6 +65,7 @@ raise ValueError("unexpected %r on stderr" % (got,)) for inst in popen2._active[:]: inst.wait() + popen2._cleanup() if popen2._active: raise ValueError("_active not empty") print "All OK"