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

Commit 5c8706c

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 2a404b6 + 78034c8 commit 5c8706c

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

13271327
def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
13281328
_WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
1329-
_WEXITSTATUS=os.WEXITSTATUS):
1329+
_WEXITSTATUS=os.WEXITSTATUS, _WIFSTOPPED=os.WIFSTOPPED,
1330+
_WSTOPSIG=os.WSTOPSIG):
13301331
"""All callers to this function MUST hold self._waitpid_lock."""
13311332
# This method is called (indirectly) by __del__, so it cannot
13321333
# refer to anything outside of its local scope.
13331334
if _WIFSIGNALED(sts):
13341335
self.returncode = -_WTERMSIG(sts)
13351336
elif _WIFEXITED(sts):
13361337
self.returncode = _WEXITSTATUS(sts)
1338+
elif _WIFSTOPPED(sts):
1339+
self.returncode = -_WSTOPSIG(sts)
13371340
else:
13381341
# Should never happen
13391342
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
@@ -2485,6 +2487,45 @@ def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
24852487
proc.communicate(timeout=999)
24862488
mock_proc_stdin.close.assert_called_once_with()
24872489

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

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

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ Core and Builtins
215215
Library
216216
-------
217217

218+
- Issue #29335: Fix subprocess.Popen.wait() when the child process has
219+
exited to a stopped instead of terminated state (ex: when under ptrace).
220+
218221
- Issue #29290: Fix a regression in argparse that help messages would wrap at
219222
non-breaking spaces.
220223

0 commit comments

Comments
 (0)