Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.

Commit 0f1958e

Browse files
authored
Fix wait() when the child exits to stopped state. (#22)
Fix wait() when the child exits to stopped state (ex: when under ptrace). https://bugs.python.org/issue29335
1 parent f43fa11 commit 0f1958e

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎ChangeLog‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
2016-??-?? 3.2.8
33
-----------------
44

5+
* Fix subprocess.Popen.wait() when the child process has exited to a
6+
a stopped instead of terminated state (ex: when under ptrace).
7+
https://bugs.python.org/issue29335
58
* Fix a compilation issue regarding O_CLOEXEC not being defined on old
69
Linux distros such as RHEL 5.
710

‎subprocess32.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,14 +1584,17 @@ def _dup2(a, b):
15841584

15851585
def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
15861586
_WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
1587-
_WEXITSTATUS=os.WEXITSTATUS):
1587+
_WEXITSTATUS=os.WEXITSTATUS, _WIFSTOPPED=os.WIFSTOPPED,
1588+
_WSTOPSIG=os.WSTOPSIG):
15881589
"""All callers to this function MUST hold self._waitpid_lock."""
15891590
# This method is called (indirectly) by __del__, so it cannot
15901591
# refer to anything outside of its local scope."""
15911592
if _WIFSIGNALED(sts):
15921593
self.returncode = -_WTERMSIG(sts)
15931594
elif _WIFEXITED(sts):
15941595
self.returncode = _WEXITSTATUS(sts)
1596+
elif _WIFSTOPPED(sts):
1597+
self.returncode = -_WSTOPSIG(sts)
15951598
else:
15961599
# Should never happen
15971600
raise RuntimeError("Unknown child exit status!")

‎test_subprocess32.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import subprocess32
44
subprocess = subprocess32
55
import sys
6+
try:
7+
import ctypes
8+
except ImportError:
9+
ctypes = None
10+
else:
11+
import ctypes.util
612
import signal
713
import os
814
import errno
@@ -1836,6 +1842,46 @@ def test_close_fds_after_preexec(self):
18361842

18371843
self.assertNotIn(fd, remaining_fds)
18381844

1845+
def test_child_terminated_in_stopped_state(self):
1846+
"""Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
1847+
if not ctypes:
1848+
sys.stderr.write('ctypes module required.\n')
1849+
return
1850+
if not sys.executable:
1851+
self.stderr.write('Test requires sys.executable.\n')
1852+
return
1853+
PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME).
1854+
libc_name = ctypes.util.find_library('c')
1855+
libc = ctypes.CDLL(libc_name)
1856+
if not hasattr(libc, 'ptrace'):
1857+
self.stderr.write('ptrace() required.\n')
1858+
return
1859+
test_ptrace = subprocess.Popen(
1860+
[sys.executable, '-c', """if True:
1861+
import ctypes
1862+
libc = ctypes.CDLL({libc_name!r})
1863+
libc.ptrace({PTRACE_TRACEME}, 0, 0)
1864+
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
1865+
])
1866+
if test_ptrace.wait() != 0:
1867+
sys.stderr.write('ptrace() failed - unable to test.\n')
1868+
return
1869+
child = subprocess.Popen(
1870+
[sys.executable, '-c', """if True:
1871+
import ctypes
1872+
libc = ctypes.CDLL({libc_name!r})
1873+
libc.ptrace({PTRACE_TRACEME}, 0, 0)
1874+
libc.printf(ctypes.c_char_p(0xdeadbeef)) # Crash the process.
1875+
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
1876+
])
1877+
try:
1878+
returncode = child.wait()
1879+
except Exception, e:
1880+
child.kill() # Clean up the hung stopped process.
1881+
raise e
1882+
self.assertNotEqual(0, returncode)
1883+
self.assertLess(returncode, 0) # signal death, likely SIGSEGV.
1884+
18391885

18401886
if mswindows:
18411887
class POSIXProcessTestCase(unittest.TestCase): pass

0 commit comments

Comments
 (0)