]> granicus.if.org Git - python/commitdiff
Issue #29335: Fix subprocess.Popen.wait() when the child process has
authorGregory P. Smith <greg@krypto.org>
Mon, 23 Jan 2017 06:38:28 +0000 (22:38 -0800)
committerGregory P. Smith <greg@krypto.org>
Mon, 23 Jan 2017 06:38:28 +0000 (22:38 -0800)
exited to a stopped instead of terminated state (ex: when under ptrace).

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

index 407141e25d46b2cb3db4a33af99409bac6a83c1d..c6ecc46186ad0eb4abedc165e700c666d4846985 100644 (file)
@@ -1026,13 +1026,16 @@ class Popen(object):
 
         def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
                 _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
-                _WEXITSTATUS=os.WEXITSTATUS):
+                _WEXITSTATUS=os.WEXITSTATUS, _WIFSTOPPED=os.WIFSTOPPED,
+                _WSTOPSIG=os.WSTOPSIG):
             # This method is called (indirectly) by __del__, so it cannot
             # refer to anything outside of its local scope.
             if _WIFSIGNALED(sts):
                 self.returncode = -_WTERMSIG(sts)
             elif _WIFEXITED(sts):
                 self.returncode = _WEXITSTATUS(sts)
+            elif _WIFSTOPPED(sts):
+                self.returncode = -_WSTOPSIG(sts)
             else:
                 # Should never happen
                 raise RuntimeError("Unknown child exit status!")
index 160ccfeb16b583aec5f3c9996515ade555c59836..627275891fb3b29b826bb159c32124362c451114 100644 (file)
@@ -2,6 +2,7 @@ import unittest
 from test import test_support
 import subprocess
 import sys
+import platform
 import signal
 import os
 import errno
@@ -10,6 +11,11 @@ import time
 import re
 import sysconfig
 
+try:
+    import ctypes
+except ImportError:
+    ctypes = None
+
 try:
     import resource
 except ImportError:
@@ -1216,6 +1222,46 @@ class POSIXProcessTestCase(BaseTestCase):
 
         self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
 
+    _libc_file_extensions = {
+      'Linux': 'so.6',
+      'Darwin': 'dylib',
+    }
+    @unittest.skipIf(not ctypes, 'ctypes module required.')
+    @unittest.skipIf(platform.uname()[0] not in _libc_file_extensions,
+                     'Test requires a libc this code can load with ctypes.')
+    @unittest.skipIf(not sys.executable, 'Test requires sys.executable.')
+    def test_child_terminated_in_stopped_state(self):
+        """Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
+        PTRACE_TRACEME = 0  # From glibc and MacOS (PT_TRACE_ME).
+        libc_name = 'libc.' + self._libc_file_extensions[platform.uname()[0]]
+        libc = ctypes.CDLL(libc_name)
+        if not hasattr(libc, 'ptrace'):
+            raise unittest.SkipTest('ptrace() required.')
+        test_ptrace = subprocess.Popen(
+            [sys.executable, '-c', """if True:
+             import ctypes
+             libc = ctypes.CDLL({libc_name!r})
+             libc.ptrace({PTRACE_TRACEME}, 0, 0)
+             """.format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
+            ])
+        if test_ptrace.wait() != 0:
+            raise unittest.SkipTest('ptrace() failed - unable to test.')
+        child = subprocess.Popen(
+            [sys.executable, '-c', """if True:
+             import ctypes
+             libc = ctypes.CDLL({libc_name!r})
+             libc.ptrace({PTRACE_TRACEME}, 0, 0)
+             libc.printf(ctypes.c_char_p(0xdeadbeef))  # Crash the process.
+             """.format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
+            ])
+        try:
+            returncode = child.wait()
+        except Exception as e:
+            child.kill()  # Clean up the hung stopped process.
+            raise e
+        self.assertNotEqual(0, returncode)
+        self.assertLess(returncode, 0)  # signal death, likely SIGSEGV.
+
 
 @unittest.skipUnless(mswindows, "Windows specific tests")
 class Win32ProcessTestCase(BaseTestCase):
index 155ec8e06651454b714c071b69b6c1eac8ae76a9..08256a5f7fcf37c57a4c564b96878fd0e41d63c3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,9 @@ Extension Modules
 Library
 -------
 
+- Issue #29335: Fix subprocess.Popen.wait() when the child process has
+  exited to a stopped instead of terminated state (ex: when under ptrace).
+
 - Issue #29219: Fixed infinite recursion in the repr of uninitialized
   ctypes.CDLL instances.