]> granicus.if.org Git - python/commitdiff
Merged revisions 78523 via svnmerge from
authorGregory P. Smith <greg@mad-scientist.com>
Mon, 1 Mar 2010 00:17:40 +0000 (00:17 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Mon, 1 Mar 2010 00:17:40 +0000 (00:17 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78523 | gregory.p.smith | 2010-02-28 16:05:08 -0800 (Sun, 28 Feb 2010) | 3 lines

  Issue #1068268: The subprocess module now handles EINTR in internal
  os.waitpid and os.read system calls where appropriate.
........

Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS

index 6e753cb7126d5232e9d8f2ca00d4c972eaef3b02..3017432fe0a01fdeed5630da44b725bba5e5d01d 100644 (file)
@@ -406,6 +406,16 @@ PIPE = -1
 STDOUT = -2
 
 
+def _eintr_retry_call(func, *args):
+    while True:
+        try:
+            return func(*args)
+        except OSError as e:
+            if e.errno == errno.EINTR:
+                continue
+            raise
+
+
 def call(*popenargs, **kwargs):
     """Run command with arguments.  Wait for command to complete, then
     return the returncode attribute.
@@ -1133,13 +1143,13 @@ class Popen(object):
 
                 # Wait for exec to fail or succeed; possibly raising an
                 # exception (limited to 1 MB)
-                data = os.read(errpipe_read, 1048576)
+                data = _eintr_retry_call(os.read, errpipe_read, 1048576)
             finally:
                 # be sure the FD is closed no matter what
                 os.close(errpipe_read)
 
             if data:
-                os.waitpid(self.pid, 0)
+                _eintr_retry_call(os.waitpid, self.pid, 0)
                 child_exception = pickle.loads(data)
                 for fd in (p2cwrite, c2pread, errread):
                     if fd is not None:
@@ -1175,7 +1185,7 @@ class Popen(object):
             """Wait for child process to terminate.  Returns returncode
             attribute."""
             if self.returncode is None:
-                pid, sts = os.waitpid(self.pid, 0)
+                pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
                 self._handle_exitstatus(sts)
             return self.returncode
 
index 24fa79bb1343068c2df3088743de33ed1331489a..685fabe6976e992d04c16fb2bbe00108de286d2e 100644 (file)
@@ -4,6 +4,7 @@ import subprocess
 import sys
 import signal
 import os
+import errno
 import tempfile
 import time
 import re
@@ -797,12 +798,31 @@ class ProcessTestCaseNoPoll(ProcessTestCase):
         ProcessTestCase.tearDown(self)
 
 
+class HelperFunctionTests(unittest.TestCase):
+    def test_eintr_retry_call(self):
+        record_calls = []
+        def fake_os_func(*args):
+            record_calls.append(args)
+            if len(record_calls) == 2:
+                raise OSError(errno.EINTR, "fake interrupted system call")
+            return tuple(reversed(args))
+
+        self.assertEqual((999, 256),
+                         subprocess._eintr_retry_call(fake_os_func, 256, 999))
+        self.assertEqual([(256, 999)], record_calls)
+        # This time there will be an EINTR so it will loop once.
+        self.assertEqual((666,),
+                         subprocess._eintr_retry_call(fake_os_func, 666))
+        self.assertEqual([(256, 999), (666,), (666,)], record_calls)
+
+
 def test_main():
     unit_tests = (ProcessTestCase,
                   POSIXProcessTestCase,
                   Win32ProcessTestCase,
                   CommandTests,
-                  ProcessTestCaseNoPoll)
+                  ProcessTestCaseNoPoll,
+                  HelperFunctionTests)
 
     support.run_unittest(*unit_tests)
     support.reap_children()
index a1f106c6adba98e1354a1dd348aa5e25e64e1e7a..c36aab3e95e121aab5addd562fe17e74456c41b1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -714,6 +714,9 @@ Library
 - Issue #7481: When a threading.Thread failed to start it would leave the
   instance stuck in initial state and present in threading.enumerate().
 
+- Issue #1068268: The subprocess module now handles EINTR in internal
+  os.waitpid and os.read system calls where appropriate.
+
 Extension Modules
 -----------------