Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b7c6dc

Browse files
authored
bpo-31173: Rewrite WSTOPSIG test of test_subprocess (#3055)
The current test_child_terminated_in_stopped_state() function test creates a child process which calls ptrace(PTRACE_TRACEME, 0, 0) and then crash (SIGSEGV). The problem is that calling os.waitpid() in the parent process is not enough to close the process: the child process remains alive and so the unit test leaks a child process in a strange state. Closing the child process requires non-trivial code, maybe platform specific. Remove the functional test and replaces it with an unit test which mocks os.waitpid() using a new _testcapi.W_STOPCODE() function to test the WIFSTOPPED() path.
1 parent 3b0f620 commit 7b7c6dc

2 files changed

Lines changed: 44 additions & 35 deletions

File tree

Lib/test/test_subprocess.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
except ImportError:
3030
threading = None
3131

32+
try:
33+
import _testcapi
34+
except ImportError:
35+
_testcapi = None
36+
3237
if support.PGO:
3338
raise unittest.SkipTest("test is not helpful for PGO")
3439

@@ -2565,42 +2570,24 @@ def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
25652570
proc.communicate(timeout=999)
25662571
mock_proc_stdin.close.assert_called_once_with()
25672572

2568-
@unittest.skipIf(not ctypes, 'ctypes module required')
2569-
@unittest.skipIf(not sys.executable, 'Test requires sys.executable')
2570-
def test_child_terminated_in_stopped_state(self):
2573+
@unittest.skipUnless(_testcapi is not None
2574+
and hasattr(_testcapi, 'W_STOPCODE'),
2575+
'need _testcapi.W_STOPCODE')
2576+
def test_stopped(self):
25712577
"""Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
2572-
PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME).
2573-
libc_name = ctypes.util.find_library('c')
2574-
libc = ctypes.CDLL(libc_name)
2575-
if not hasattr(libc, 'ptrace'):
2576-
raise unittest.SkipTest('ptrace() required')
2577-
2578-
code = textwrap.dedent(f"""
2579-
import ctypes
2580-
import faulthandler
2581-
from test.support import SuppressCrashReport
2582-
2583-
libc = ctypes.CDLL({libc_name!r})
2584-
libc.ptrace({PTRACE_TRACEME}, 0, 0)
2585-
""")
2586-
2587-
child = subprocess.Popen([sys.executable, '-c', code])
2588-
if child.wait() != 0:
2589-
raise unittest.SkipTest('ptrace() failed - unable to test')
2590-
2591-
code += textwrap.dedent(f"""
2592-
with SuppressCrashReport():
2593-
# Crash the process
2594-
faulthandler._sigsegv()
2595-
""")
2596-
child = subprocess.Popen([sys.executable, '-c', code])
2597-
try:
2598-
returncode = child.wait()
2599-
except:
2600-
child.kill() # Clean up the hung stopped process.
2601-
raise
2602-
self.assertNotEqual(0, returncode)
2603-
self.assertLess(returncode, 0) # signal death, likely SIGSEGV.
2578+
args = [sys.executable, '-c', 'pass']
2579+
proc = subprocess.Popen(args)
2580+
2581+
# Wait until the real process completes to avoid zombie process
2582+
pid = proc.pid
2583+
pid, status = os.waitpid(pid, 0)
2584+
self.assertEqual(status, 0)
2585+
2586+
status = _testcapi.W_STOPCODE(3)
2587+
with mock.patch('subprocess.os.waitpid', return_value=(pid, status)):
2588+
returncode = proc.wait()
2589+
2590+
self.assertEqual(returncode, -3)
26042591

26052592

26062593
@unittest.skipUnless(mswindows, "Windows specific tests")

Modules/_testcapimodule.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
# include <winsock2.h> /* struct timeval */
1919
#endif
2020

21+
#ifdef HAVE_SYS_WAIT_H
22+
#include <sys/wait.h> /* For W_STOPCODE */
23+
#endif
24+
2125
#ifdef WITH_THREAD
2226
#include "pythread.h"
2327
#endif /* WITH_THREAD */
@@ -4272,6 +4276,7 @@ test_pyobject_fastcallkeywords(PyObject *self, PyObject *args)
42724276
return _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
42734277
}
42744278

4279+
42754280
static PyObject*
42764281
stack_pointer(PyObject *self, PyObject *args)
42774282
{
@@ -4280,6 +4285,20 @@ stack_pointer(PyObject *self, PyObject *args)
42804285
}
42814286

42824287

4288+
#ifdef W_STOPCODE
4289+
static PyObject*
4290+
py_w_stopcode(PyObject *self, PyObject *args)
4291+
{
4292+
int sig, status;
4293+
if (!PyArg_ParseTuple(args, "i", &sig)) {
4294+
return NULL;
4295+
}
4296+
status = W_STOPCODE(sig);
4297+
return PyLong_FromLong(status);
4298+
}
4299+
#endif
4300+
4301+
42834302
static PyMethodDef TestMethods[] = {
42844303
{"raise_exception", raise_exception, METH_VARARGS},
42854304
{"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
@@ -4493,6 +4512,9 @@ static PyMethodDef TestMethods[] = {
44934512
{"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},
44944513
{"pyobject_fastcallkeywords", test_pyobject_fastcallkeywords, METH_VARARGS},
44954514
{"stack_pointer", stack_pointer, METH_NOARGS},
4515+
#ifdef W_STOPCODE
4516+
{"W_STOPCODE", py_w_stopcode, METH_VARARGS},
4517+
#endif
44964518
{NULL, NULL} /* sentinel */
44974519
};
44984520

0 commit comments

Comments
 (0)