mirror of https://github.com/python/cpython
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.
This commit is contained in:
parent
3b0f620c1a
commit
7b7c6dcfff
|
@ -29,6 +29,11 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
threading = None
|
threading = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
import _testcapi
|
||||||
|
except ImportError:
|
||||||
|
_testcapi = None
|
||||||
|
|
||||||
if support.PGO:
|
if support.PGO:
|
||||||
raise unittest.SkipTest("test is not helpful for PGO")
|
raise unittest.SkipTest("test is not helpful for PGO")
|
||||||
|
|
||||||
|
@ -2565,42 +2570,24 @@ class POSIXProcessTestCase(BaseTestCase):
|
||||||
proc.communicate(timeout=999)
|
proc.communicate(timeout=999)
|
||||||
mock_proc_stdin.close.assert_called_once_with()
|
mock_proc_stdin.close.assert_called_once_with()
|
||||||
|
|
||||||
@unittest.skipIf(not ctypes, 'ctypes module required')
|
@unittest.skipUnless(_testcapi is not None
|
||||||
@unittest.skipIf(not sys.executable, 'Test requires sys.executable')
|
and hasattr(_testcapi, 'W_STOPCODE'),
|
||||||
def test_child_terminated_in_stopped_state(self):
|
'need _testcapi.W_STOPCODE')
|
||||||
|
def test_stopped(self):
|
||||||
"""Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
|
"""Test wait() behavior when waitpid returns WIFSTOPPED; issue29335."""
|
||||||
PTRACE_TRACEME = 0 # From glibc and MacOS (PT_TRACE_ME).
|
args = [sys.executable, '-c', 'pass']
|
||||||
libc_name = ctypes.util.find_library('c')
|
proc = subprocess.Popen(args)
|
||||||
libc = ctypes.CDLL(libc_name)
|
|
||||||
if not hasattr(libc, 'ptrace'):
|
|
||||||
raise unittest.SkipTest('ptrace() required')
|
|
||||||
|
|
||||||
code = textwrap.dedent(f"""
|
# Wait until the real process completes to avoid zombie process
|
||||||
import ctypes
|
pid = proc.pid
|
||||||
import faulthandler
|
pid, status = os.waitpid(pid, 0)
|
||||||
from test.support import SuppressCrashReport
|
self.assertEqual(status, 0)
|
||||||
|
|
||||||
libc = ctypes.CDLL({libc_name!r})
|
status = _testcapi.W_STOPCODE(3)
|
||||||
libc.ptrace({PTRACE_TRACEME}, 0, 0)
|
with mock.patch('subprocess.os.waitpid', return_value=(pid, status)):
|
||||||
""")
|
returncode = proc.wait()
|
||||||
|
|
||||||
child = subprocess.Popen([sys.executable, '-c', code])
|
self.assertEqual(returncode, -3)
|
||||||
if child.wait() != 0:
|
|
||||||
raise unittest.SkipTest('ptrace() failed - unable to test')
|
|
||||||
|
|
||||||
code += textwrap.dedent(f"""
|
|
||||||
with SuppressCrashReport():
|
|
||||||
# Crash the process
|
|
||||||
faulthandler._sigsegv()
|
|
||||||
""")
|
|
||||||
child = subprocess.Popen([sys.executable, '-c', code])
|
|
||||||
try:
|
|
||||||
returncode = child.wait()
|
|
||||||
except:
|
|
||||||
child.kill() # Clean up the hung stopped process.
|
|
||||||
raise
|
|
||||||
self.assertNotEqual(0, returncode)
|
|
||||||
self.assertLess(returncode, 0) # signal death, likely SIGSEGV.
|
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(mswindows, "Windows specific tests")
|
@unittest.skipUnless(mswindows, "Windows specific tests")
|
||||||
|
|
|
@ -18,6 +18,10 @@
|
||||||
# include <winsock2.h> /* struct timeval */
|
# include <winsock2.h> /* struct timeval */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_SYS_WAIT_H
|
||||||
|
#include <sys/wait.h> /* For W_STOPCODE */
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef WITH_THREAD
|
#ifdef WITH_THREAD
|
||||||
#include "pythread.h"
|
#include "pythread.h"
|
||||||
#endif /* WITH_THREAD */
|
#endif /* WITH_THREAD */
|
||||||
|
@ -4272,6 +4276,7 @@ test_pyobject_fastcallkeywords(PyObject *self, PyObject *args)
|
||||||
return _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
|
return _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
stack_pointer(PyObject *self, PyObject *args)
|
stack_pointer(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
@ -4280,6 +4285,20 @@ stack_pointer(PyObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef W_STOPCODE
|
||||||
|
static PyObject*
|
||||||
|
py_w_stopcode(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
int sig, status;
|
||||||
|
if (!PyArg_ParseTuple(args, "i", &sig)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
status = W_STOPCODE(sig);
|
||||||
|
return PyLong_FromLong(status);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static PyMethodDef TestMethods[] = {
|
static PyMethodDef TestMethods[] = {
|
||||||
{"raise_exception", raise_exception, METH_VARARGS},
|
{"raise_exception", raise_exception, METH_VARARGS},
|
||||||
{"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
|
{"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
|
||||||
|
@ -4493,6 +4512,9 @@ static PyMethodDef TestMethods[] = {
|
||||||
{"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},
|
{"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},
|
||||||
{"pyobject_fastcallkeywords", test_pyobject_fastcallkeywords, METH_VARARGS},
|
{"pyobject_fastcallkeywords", test_pyobject_fastcallkeywords, METH_VARARGS},
|
||||||
{"stack_pointer", stack_pointer, METH_NOARGS},
|
{"stack_pointer", stack_pointer, METH_NOARGS},
|
||||||
|
#ifdef W_STOPCODE
|
||||||
|
{"W_STOPCODE", py_w_stopcode, METH_VARARGS},
|
||||||
|
#endif
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue