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

Commit 50e16e3

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).
1 parent 7fe28ad commit 50e16e3

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
@@ -1285,14 +1285,17 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
12851285

12861286
def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
12871287
_WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
1288-
_WEXITSTATUS=os.WEXITSTATUS):
1288+
_WEXITSTATUS=os.WEXITSTATUS, _WIFSTOPPED=os.WIFSTOPPED,
1289+
_WSTOPSIG=os.WSTOPSIG):
12891290
"""All callers to this function MUST hold self._waitpid_lock."""
12901291
# This method is called (indirectly) by __del__, so it cannot
12911292
# refer to anything outside of its local scope.
12921293
if _WIFSIGNALED(sts):
12931294
self.returncode = -_WTERMSIG(sts)
12941295
elif _WIFEXITED(sts):
12951296
self.returncode = _WEXITSTATUS(sts)
1297+
elif _WIFSTOPPED(sts):
1298+
self.returncode = -_WSTOPSIG(sts)
12961299
else:
12971300
# Should never happen
12981301
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
@@ -4,6 +4,8 @@
44
from test import support
55
import subprocess
66
import sys
7+
import platform
8+
import ctypes
79
import signal
810
import io
911
import locale
@@ -2448,6 +2450,45 @@ def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
24482450
proc.communicate(timeout=999)
24492451
mock_proc_stdin.close.assert_called_once_with()
24502452

2453+
_libc_file_extensions = {
2454+
'Linux': 'so.6',
2455+
'Darwin': '.dylib',
2456+
}
2457+
@unittest.skipIf(platform.uname()[0] not in _libc_file_extensions,
2458+
'Test requires a libc this code can load with ctypes.')
2459+
@unittest.skipIf(not sys.executable, 'Test requires sys.executable.')
2460+
def test_child_terminated_in_stopped_state(self):
2461+
"""Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
2462+
PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME).
2463+
libc_name = 'libc.' + self._libc_file_extensions[platform.uname()[0]]
2464+
libc = ctypes.CDLL(libc_name)
2465+
if not hasattr(libc, 'ptrace'):
2466+
raise unittest.SkipTest('ptrace() required.')
2467+
test_ptrace = subprocess.Popen(
2468+
[sys.executable, '-c', """if True:
2469+
import ctypes
2470+
libc = ctypes.CDLL({libc_name!r})
2471+
libc.ptrace({PTRACE_TRACEME}, 0, 0)
2472+
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
2473+
])
2474+
if test_ptrace.wait() != 0:
2475+
raise unittest.SkipTest('ptrace() failed - unable to test.')
2476+
child = subprocess.Popen(
2477+
[sys.executable, '-c', """if True:
2478+
import ctypes
2479+
libc = ctypes.CDLL({libc_name!r})
2480+
libc.ptrace({PTRACE_TRACEME}, 0, 0)
2481+
libc.printf(ctypes.c_char_p(0xdeadbeef)) # Crash the process.
2482+
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME)
2483+
])
2484+
try:
2485+
returncode = child.wait()
2486+
except Exception as e:
2487+
child.kill() # Clean up the hung stopped process.
2488+
raise e
2489+
self.assertNotEqual(0, returncode)
2490+
self.assertLess(returncode, 0) # signal death, likely SIGSEGV.
2491+
24512492

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

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #29335: Fix subprocess.Popen.wait() when the child process has
17+
exited to a stopped instead of terminated state (ex: when under ptrace).
18+
1619
- Issue #29290: Fix a regression in argparse that help messages would wrap at
1720
non-breaking spaces.
1821

0 commit comments

Comments
 (0)