Skip to content

Commit 4dea065

Browse files
authored
bpo-31173: Rewrite WSTOPSIG test of test_subprocess (#3055) (#3071)
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. (cherry picked from commit 7b7c6dc)
1 parent 1247e2c commit 4dea065

2 files changed

Lines changed: 48 additions & 33 deletions

File tree

Lib/test/test_subprocess.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
except ImportError:
2929
threading = None
3030

31+
try:
32+
import _testcapi
33+
except ImportError:
34+
_testcapi = None
35+
3136
mswindows = (sys.platform == "win32")
3237

3338
#
@@ -1265,40 +1270,28 @@ def test_pipe_cloexec(self):
12651270

12661271
self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
12671272

1268-
@unittest.skipIf(not ctypes, 'ctypes module required')
1269-
@unittest.skipIf(not sys.executable, 'Test requires sys.executable')
1270-
def test_child_terminated_in_stopped_state(self):
1273+
@unittest.skipUnless(_testcapi is not None
1274+
and hasattr(_testcapi, 'W_STOPCODE'),
1275+
'need _testcapi.W_STOPCODE')
1276+
def test_stopped(self):
12711277
"""Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
1272-
PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME).
1273-
libc_name = ctypes.util.find_library('c')
1274-
libc = ctypes.CDLL(libc_name)
1275-
if not hasattr(libc, 'ptrace'):
1276-
raise unittest.SkipTest('ptrace() required')
1277-
1278-
code = textwrap.dedent("""
1279-
import ctypes
1280-
from test.support import _crash_python
1281-
1282-
libc = ctypes.CDLL({libc_name!r})
1283-
libc.ptrace({PTRACE_TRACEME}, 0, 0)
1284-
""".format(libc_name=libc_name, PTRACE_TRACEME=PTRACE_TRACEME))
1285-
1286-
child = subprocess.Popen([sys.executable, '-c', code])
1287-
if child.wait() != 0:
1288-
raise unittest.SkipTest('ptrace() failed - unable to test')
1289-
1290-
code += textwrap.dedent("""
1291-
# Crash the process
1292-
_crash_python()
1293-
""")
1294-
child = subprocess.Popen([sys.executable, '-c', code])
1295-
try:
1296-
returncode = child.wait()
1297-
except:
1298-
child.kill() # Clean up the hung stopped process.
1299-
raise
1300-
self.assertNotEqual(0, returncode)
1301-
self.assertLess(returncode, 0) # signal death, likely SIGSEGV.
1278+
args = [sys.executable, '-c', 'pass']
1279+
proc = subprocess.Popen(args)
1280+
1281+
# Wait until the real process completes to avoid zombie process
1282+
pid = proc.pid
1283+
pid, status = os.waitpid(pid, 0)
1284+
self.assertEqual(status, 0)
1285+
1286+
status = _testcapi.W_STOPCODE(3)
1287+
1288+
def mock_waitpid(pid, flags):
1289+
return (pid, status)
1290+
1291+
with test_support.swap_attr(os, 'waitpid', mock_waitpid):
1292+
returncode = proc.wait()
1293+
1294+
self.assertEqual(returncode, -3)
13021295

13031296

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

Modules/_testcapimodule.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
# include <crtdbg.h>
1616
#endif
1717

18+
#ifdef HAVE_SYS_WAIT_H
19+
#include <sys/wait.h> /* For W_STOPCODE */
20+
#endif
21+
1822
#ifdef WITH_THREAD
1923
#include "pythread.h"
2024
#endif /* WITH_THREAD */
@@ -2523,6 +2527,7 @@ msvcrt_CrtSetReportMode(PyObject* self, PyObject *args)
25232527
return PyInt_FromLong(res);
25242528
}
25252529

2530+
25262531
static PyObject*
25272532
msvcrt_CrtSetReportFile(PyObject* self, PyObject *args)
25282533
{
@@ -2540,6 +2545,20 @@ msvcrt_CrtSetReportFile(PyObject* self, PyObject *args)
25402545
#endif
25412546

25422547

2548+
#ifdef W_STOPCODE
2549+
static PyObject*
2550+
py_w_stopcode(PyObject *self, PyObject *args)
2551+
{
2552+
int sig, status;
2553+
if (!PyArg_ParseTuple(args, "i", &sig)) {
2554+
return NULL;
2555+
}
2556+
status = W_STOPCODE(sig);
2557+
return PyLong_FromLong(status);
2558+
}
2559+
#endif
2560+
2561+
25432562
static PyMethodDef TestMethods[] = {
25442563
{"raise_exception", raise_exception, METH_VARARGS},
25452564
{"set_errno", set_errno, METH_VARARGS},
@@ -2655,6 +2674,9 @@ static PyMethodDef TestMethods[] = {
26552674
#ifdef MS_WINDOWS
26562675
{"CrtSetReportMode", (PyCFunction)msvcrt_CrtSetReportMode, METH_VARARGS},
26572676
{"CrtSetReportFile", (PyCFunction)msvcrt_CrtSetReportFile, METH_VARARGS},
2677+
#endif
2678+
#ifdef W_STOPCODE
2679+
{"W_STOPCODE", py_w_stopcode, METH_VARARGS},
26582680
#endif
26592681
{NULL, NULL} /* sentinel */
26602682
};

0 commit comments

Comments
 (0)