gh-104690 Disallow thread creation and fork at interpreter finalization (#104826)

Disallow thread creation and fork at interpreter finalization.

in the following functions, check if interpreter is finalizing and raise `RuntimeError` with appropriate message:
* `_thread.start_new_thread` and thus `threading`
* `posix.fork`
* `posix.fork1`
* `posix.forkpty`
* `_posixsubprocess.fork_exec` when a `preexec_fn=` is supplied.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
chgnrdv 2023-06-04 07:06:45 +03:00 committed by GitHub
parent eaff9c39aa
commit ce558e69d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 97 additions and 30 deletions

View file

@ -7620,7 +7620,13 @@ os_fork1_impl(PyObject *module)
{
pid_t pid;
if (!_Py_IsMainInterpreter(_PyInterpreterState_GET())) {
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->finalizing) {
PyErr_SetString(PyExc_RuntimeError,
"can't fork at interpreter shutdown");
return NULL;
}
if (!_Py_IsMainInterpreter(interp)) {
PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
return NULL;
}
@ -7656,6 +7662,11 @@ os_fork_impl(PyObject *module)
{
pid_t pid;
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->finalizing) {
PyErr_SetString(PyExc_RuntimeError,
"can't fork at interpreter shutdown");
return NULL;
}
if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_FORK)) {
PyErr_SetString(PyExc_RuntimeError,
"fork not supported for isolated subinterpreters");
@ -8327,7 +8338,13 @@ os_forkpty_impl(PyObject *module)
int master_fd = -1;
pid_t pid;
if (!_Py_IsMainInterpreter(_PyInterpreterState_GET())) {
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->finalizing) {
PyErr_SetString(PyExc_RuntimeError,
"can't fork at interpreter shutdown");
return NULL;
}
if (!_Py_IsMainInterpreter(interp)) {
PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
return NULL;
}