summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorCharles-François Natali <neologix@free.fr>2011-08-18 17:11:29 (GMT)
committerCharles-François Natali <neologix@free.fr>2011-08-18 17:11:29 (GMT)
commit558639f0c9cc507acafd0c3842a23d03b2244652 (patch)
tree6fe7f3f1eca5ad431164b89aad298523b43f504c /Lib
parent020340f2841ec2b70b9e09921850d16019d0667e (diff)
parent134a8baed9b70b2d6a953d0c872fc2c42580e1b6 (diff)
downloadcpython-558639f0c9cc507acafd0c3842a23d03b2244652.zip
cpython-558639f0c9cc507acafd0c3842a23d03b2244652.tar.gz
cpython-558639f0c9cc507acafd0c3842a23d03b2244652.tar.bz2
Issue #12650: Fix a race condition where a subprocess.Popen could leak
resources (FD/zombie) when killed at the wrong time.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/subprocess.py7
-rw-r--r--Lib/test/test_subprocess.py58
2 files changed, 64 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index ec417c2..db64588 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -424,12 +424,16 @@ try:
except:
MAXFD = 256
+# This lists holds Popen instances for which the underlying process had not
+# exited at the time its __del__ method got called: those processes are wait()ed
+# for synchronously from _cleanup() when a new Popen object is created, to avoid
+# zombie processes.
_active = []
def _cleanup():
for inst in _active[:]:
res = inst._internal_poll(_deadstate=sys.maxsize)
- if res is not None and res >= 0:
+ if res is not None:
try:
_active.remove(inst)
except ValueError:
@@ -1272,6 +1276,7 @@ class Popen(object):
errread, errwrite,
errpipe_read, errpipe_write,
restore_signals, start_new_session, preexec_fn)
+ self._child_created = True
finally:
# be sure the FD is closed no matter what
os.close(errpipe_write)
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index 06664a8..950294d 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -1491,6 +1491,64 @@ class POSIXProcessTestCase(BaseTestCase):
finally:
p.wait()
+ def test_zombie_fast_process_del(self):
+ # Issue #12650: on Unix, if Popen.__del__() was called before the
+ # process exited, it wouldn't be added to subprocess._active, and would
+ # remain a zombie.
+ # spawn a Popen, and delete its reference before it exits
+ p = subprocess.Popen([sys.executable, "-c",
+ 'import sys, time;'
+ 'time.sleep(0.2)'],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ ident = id(p)
+ pid = p.pid
+ del p
+ # check that p is in the active processes list
+ self.assertIn(ident, [id(o) for o in subprocess._active])
+
+ # sleep a little to let the process exit, and create a new Popen: this
+ # should trigger the wait() of p
+ time.sleep(1)
+ with self.assertRaises(EnvironmentError) as c:
+ with subprocess.Popen(['nonexisting_i_hope'],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE) as proc:
+ pass
+ # p should have been wait()ed on, and removed from the _active list
+ self.assertRaises(OSError, os.waitpid, pid, 0)
+ self.assertNotIn(ident, [id(o) for o in subprocess._active])
+
+ def test_leak_fast_process_del_killed(self):
+ # Issue #12650: on Unix, if Popen.__del__() was called before the
+ # process exited, and the process got killed by a signal, it would never
+ # be removed from subprocess._active, which triggered a FD and memory
+ # leak.
+ # spawn a Popen, delete its reference and kill it
+ p = subprocess.Popen([sys.executable, "-c",
+ 'import time;'
+ 'time.sleep(3)'],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ ident = id(p)
+ pid = p.pid
+ del p
+ os.kill(pid, signal.SIGKILL)
+ # check that p is in the active processes list
+ self.assertIn(ident, [id(o) for o in subprocess._active])
+
+ # let some time for the process to exit, and create a new Popen: this
+ # should trigger the wait() of p
+ time.sleep(0.2)
+ with self.assertRaises(EnvironmentError) as c:
+ with subprocess.Popen(['nonexisting_i_hope'],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE) as proc:
+ pass
+ # p should have been wait()ed on, and removed from the _active list
+ self.assertRaises(OSError, os.waitpid, pid, 0)
+ self.assertNotIn(ident, [id(o) for o in subprocess._active])
+
@unittest.skipUnless(mswindows, "Windows specific tests")
class Win32ProcessTestCase(BaseTestCase):