]> arthur.barton.de Git - bup.git/commitdiff
main.py: don't leak a file descriptor.
authorAvery Pennarun <apenwarr@gmail.com>
Sun, 21 Mar 2010 04:34:21 +0000 (00:34 -0400)
committerAvery Pennarun <apenwarr@gmail.com>
Sun, 21 Mar 2010 05:50:22 +0000 (01:50 -0400)
subprocess.Popen() is a little weird about when it closes the file
descriptors you give it.  In this case, we have to dup() it because if
stderr=2 (the default) and stdout=2 (because fix_stderr), it'll close fd 2.
But if we dup it first, it *won't* close the dup, because stdout!=stderr.
So we have to dup it, but then we have to close it ourselves.

This was apparently harmless (it just resulted in an extra fd#3 getting
passed around to subprocesses as a clone of fd#2) but it was still wrong.

main.py

diff --git a/main.py b/main.py
index 941242ef4bbb9f6962c930346f916a1ec8bcfd01..2a5cbae2f969d6c53e8c86ba4be59a94caf9f1bf 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -97,9 +97,11 @@ def force_tty():
 
 if fix_stdout or fix_stderr:
     realf = fix_stderr and 2 or 1
+    drealf = os.dup(realf)  # Popen goes crazy with stdout=2
     n = subprocess.Popen([subpath('newliner')],
-                         stdin=subprocess.PIPE, stdout=os.dup(realf),
+                         stdin=subprocess.PIPE, stdout=drealf,
                          close_fds=True, preexec_fn=force_tty)
+    os.close(drealf)
     outf = fix_stdout and n.stdin.fileno() or None
     errf = fix_stderr and n.stdin.fileno() or None
 else: