Commit a20b6adb authored by Gregory P. Smith's avatar Gregory P. Smith Committed by Miss Islington (bot)

bpo-34658: Fix rare subprocess prexec_fn fork error. (GH-9255)



[bpo-34658](https://www.bugs.python.org/issue34658): Fix a rare interpreter unhandled exception state SystemError only
seen when using subprocess with a preexec_fn while an after_parent handler has
been registered with os.register_at_fork and the fork system call fails.



https://bugs.python.org/issue34658
parent 1abba455
Fix a rare interpreter unhandled exception state SystemError only seen when
using subprocess with a preexec_fn while an after_parent handler has been
registered with os.register_at_fork and the fork system call fails.
...@@ -564,6 +564,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args) ...@@ -564,6 +564,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
char *const *exec_array, *const *argv = NULL, *const *envp = NULL; char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Py_ssize_t arg_num; Py_ssize_t arg_num;
int need_after_fork = 0; int need_after_fork = 0;
int saved_errno = 0;
if (!PyArg_ParseTuple( if (!PyArg_ParseTuple(
args, "OOpO!OOiiiiiiiiiiO:fork_exec", args, "OOpO!OOiiiiiiiiiiO:fork_exec",
...@@ -700,14 +701,14 @@ subprocess_fork_exec(PyObject* self, PyObject *args) ...@@ -700,14 +701,14 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
_exit(255); _exit(255);
return NULL; /* Dead code to avoid a potential compiler warning. */ return NULL; /* Dead code to avoid a potential compiler warning. */
} }
Py_XDECREF(cwd_obj2); /* Parent (original) process */
if (pid == -1) { if (pid == -1) {
/* Capture the errno exception before errno can be clobbered. */ /* Capture errno for the exception. */
PyErr_SetFromErrno(PyExc_OSError); saved_errno = errno;
} }
/* Parent process */ Py_XDECREF(cwd_obj2);
if (need_after_fork) if (need_after_fork)
PyOS_AfterFork_Parent(); PyOS_AfterFork_Parent();
if (envp) if (envp)
...@@ -723,8 +724,13 @@ subprocess_fork_exec(PyObject* self, PyObject *args) ...@@ -723,8 +724,13 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
Py_XDECREF(preexec_fn_args_tuple); Py_XDECREF(preexec_fn_args_tuple);
Py_XDECREF(gc_module); Py_XDECREF(gc_module);
if (pid == -1) if (pid == -1) {
return NULL; /* fork() failed. Exception set earlier. */ errno = saved_errno;
/* We can't call this above as PyOS_AfterFork_Parent() calls back
* into Python code which would see the unreturned error. */
PyErr_SetFromErrno(PyExc_OSError);
return NULL; /* fork() failed. */
}
return PyLong_FromPid(pid); return PyLong_FromPid(pid);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment