]> git.phdru.name Git - bookmarks_db.git/blobdiff - subproc.py
Fix(subproc.py): Carefully close file descriptors
[bookmarks_db.git] / subproc.py
old mode 100644 (file)
new mode 100755 (executable)
index d16ecc6..b5944f9
@@ -1,3 +1,5 @@
+#! /usr/bin/env python
+
 """Run a subprocess and communicate with it via stdin, stdout, and stderr.
 
 Requires that platform supports, eg, posix-style 'os.pipe' and 'os.fork'
@@ -141,7 +143,9 @@ class Subprocess:
             except os.error as error:
                 errno, msg = error
                 if errno == 10:
+                    self.pid = None
                     raise SubprocessError("Subprocess '%s' failed." % self.cmd)
+                self.pid = None
                 raise SubprocessError("Subprocess failed[%d]: %s" % (errno, msg))
             if pid == self.pid:
                 # child exited already
@@ -153,6 +157,7 @@ class Subprocess:
                     "child killed by signal %d with a return code of %d"
                     % (sig, rc))
                 if rc:
+                    self.pid = None
                     raise SubprocessError(
                           "child exited with return code %d" % rc)
                 # Child may have exited, but not in error, so we won't say
@@ -323,7 +328,11 @@ class Subprocess:
                                (sig[0], self.pid, self.cmd,
                                 hex(id(self))[2:])))
                     for i in self.pipefiles:
-                        os.close(i)
+                        try:
+                            fp = os.fdopen(i).close()
+                        except OSError:
+                            pass
+                    del self.pipefiles[:]
                     self.pid = 0
                     return None                                         # ===>
                 time.sleep(.1)
@@ -686,3 +695,6 @@ def test(p=0):
             del p
             print("\tDone.")
             return None
+
+if __name__ == '__main__':
+    test()