diff options
author | Gregory P. Smith <greg@krypto.org> | 2012-11-11 09:41:49 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2012-11-11 09:41:49 (GMT) |
commit | c2c4cb624bac98e0fdb52258655d26c385a35335 (patch) | |
tree | 3274ada131123110abcb0561cfa4fec9a742548b /Lib/test/test_subprocess.py | |
parent | d4b645ef423667f179f136bee711b8ca85bd28d8 (diff) | |
parent | c8ac03d936fd9fa7b48c0aaac01b66e8d8fcf766 (diff) | |
download | cpython-c2c4cb624bac98e0fdb52258655d26c385a35335.zip cpython-c2c4cb624bac98e0fdb52258655d26c385a35335.tar.gz cpython-c2c4cb624bac98e0fdb52258655d26c385a35335.tar.bz2 |
Fixes issue #16140: The subprocess module no longer double closes its
child subprocess.PIPE parent file descriptors on child error prior to
exec().
This would lead to race conditions in multithreaded programs where
another thread opened a file reusing the fd which was then closed out
from beneath it by the errant second close.
Diffstat (limited to 'Lib/test/test_subprocess.py')
-rw-r--r-- | Lib/test/test_subprocess.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 8978b31..e502618 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1189,6 +1189,45 @@ class POSIXProcessTestCase(BaseTestCase): self.fail("Exception raised by preexec_fn did not make it " "to the parent process.") + @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.") + def test_preexec_errpipe_does_not_double_close_pipes(self): + """Issue16140: Don't double close pipes on preexec error.""" + class SafeConstructorPopen(subprocess.Popen): + def __init__(self): + pass # Do nothing so we can modify the instance for testing. + def RealPopen(self, *args, **kwargs): + subprocess.Popen.__init__(self, *args, **kwargs) + def raise_it(): + raise subprocess.SubprocessError( + "force the _execute_child() errpipe_data path.") + + p = SafeConstructorPopen() + + def _test_fds_execute_child_wrapper(*args, **kwargs): + try: + subprocess.Popen._execute_child(p, *args, **kwargs) + finally: + # Open a bunch of file descriptors and verify that + # none of them are the same as the ones the Popen + # instance is using for stdin/stdout/stderr. + devzero_fds = [os.open("/dev/zero", os.O_RDONLY) + for _ in range(8)] + try: + for fd in devzero_fds: + self.assertNotIn(fd, ( + p.stdin.fileno(), p.stdout.fileno(), + p.stderr.fileno()), + msg="At least one fd was closed early.") + finally: + map(os.close, devzero_fds) + + p._execute_child = _test_fds_execute_child_wrapper + + with self.assertRaises(subprocess.SubprocessError): + p.RealPopen([sys.executable, "-c", "pass"], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, preexec_fn=raise_it) + def test_preexec_gc_module_failure(self): # This tests the code that disables garbage collection if the child # process will execute any Python. |