Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 78034c8

Browse files
committed
Issue python#29335: Fix subprocess.Popen.wait() when the child process has
exited to a stopped instead of terminated state (ex: when under ptrace).
2 parents b168118 + 50e16e3 commit 78034c8

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

Lib/subprocess.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1329,14 +1329,17 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
13291329

13301330
def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
13311331
_WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
1332-
_WEXITSTATUS=os.WEXITSTATUS):
1332+
_WEXITSTATUS=os.WEXITSTATUS, _WIFSTOPPED=os.WIFSTOPPED,
1333+
_WSTOPSIG=os.WSTOPSIG):
13331334
"""All callers to this function MUST hold self._waitpid_lock."""
13341335
# This method is called (indirectly) by __del__, so it cannot
13351336
# refer to anything outside of its local scope.
13361337
if _WIFSIGNALED(sts):
13371338
self.returncode = -_WTERMSIG(sts)
13381339
elif _WIFEXITED(sts):
13391340
self.returncode = _WEXITSTATUS(sts)
1341+
elif _WIFSTOPPED(sts):
1342+
self.returncode = -_WSTOPSIG(sts)
13401343
else:
13411344
# Should never happen
13421345
raise SubprocessError("Unknown child exit status!")

Lib/test/test_subprocess.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from test import support
44
import subprocess
55
import sys
6+
import platform
7+
import ctypes
68
import signal
79
import io
810
import os
@@ -2498,6 +2500,45 @@ def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
24982500
proc.communicate(timeout=999)
24992501
mock_proc_stdin.close.assert_called_once_with()
25002502

2503+
_libc_file_extensions = {
2504+
'Linux': 'so.6',
2505+
'Darwin': '.dylib',
2506+
}
2507+
@unittest.skipIf(platform.uname()[0] not in _libc_file_extensions,
2508+
'Test requires a libc this code can load with ctypes.')
2509+
@unittest.skipIf(not sys.executable, 'Test requires sys.executable.')
2510+
def test_child_terminated_in_stopped_state(self):
2511+
"""Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
2512+
PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME).
2513+
libc_name = 'libc.' + self._libc_file_extensions[platform.uname()[0]]
2514+
libc = ctypes.CDLL(libc_name)
2515+
if not hasattr(libc, 'ptrace'):
2516+
raise unittest.SkipTest('ptrace() required.')
2517+
test_ptrace = subprocess.Popen(
2518+
[sys.executable, '-c', """if True:
2519+
import ctypes
2520+
libc = ctypes.CDLL({libc_name!r})
2521+
libc.ptrace({PTRACE_TRACEME}, 0, 0)
2522+
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
2523+
])
2524+
if test_ptrace.wait() != 0:
2525+
raise unittest.SkipTest('ptrace() failed - unable to test.')
2526+
child = subprocess.Popen(
2527+
[sys.executable, '-c', """if True:
2528+
import ctypes
2529+
libc = ctypes.CDLL({libc_name!r})
2530+
libc.ptrace({PTRACE_TRACEME}, 0, 0)
2531+
libc.printf(ctypes.c_char_p(0xdeadbeef)) # Crash the process.
2532+
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
2533+
])
2534+
try:
2535+
returncode = child.wait()
2536+
except Exception as e:
2537+
child.kill() # Clean up the hung stopped process.
2538+
raise e
2539+
self.assertNotEqual(0, returncode)
2540+
self.assertLess(returncode, 0) # signal death, likely SIGSEGV.
2541+
25012542

25022543
@unittest.skipUnless(mswindows, "Windows specific tests")
25032544
class Win32ProcessTestCase(BaseTestCase):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Core and Builtins
4747
Library
4848
-------
4949

50+
- Issue #29335: Fix subprocess.Popen.wait() when the child process has
51+
exited to a stopped instead of terminated state (ex: when under ptrace).
52+
5053
- Issue #29290: Fix a regression in argparse that help messages would wrap at
5154
non-breaking spaces.
5255

0 commit comments

Comments
 (0)