Skip to content

[3.6] bpo-34658: Fix rare subprocess prexec_fn fork error. (GH-9255) #9292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.
20 changes: 13 additions & 7 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
#ifdef WITH_THREAD
int import_lock_held = 0;
#endif
int saved_errno = 0;

if (!PyArg_ParseTuple(
args, "OOpO!OOiiiiiiiiiiO:fork_exec",
Expand Down Expand Up @@ -701,11 +702,10 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
_exit(255);
return NULL; /* Dead code to avoid a potential compiler warning. */
}
Py_XDECREF(cwd_obj2);

/* Parent (original) process */
if (pid == -1) {
/* Capture the errno exception before errno can be clobbered. */
PyErr_SetFromErrno(PyExc_OSError);
/* Capture errno for the exception. */
saved_errno = errno;
}
#ifdef WITH_THREAD
if (preexec_fn != Py_None
Expand All @@ -717,7 +717,8 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
import_lock_held = 0;
#endif

/* Parent process */
Py_XDECREF(cwd_obj2);

if (envp)
_Py_FreeCharPArray(envp);
if (argv)
Expand All @@ -731,8 +732,13 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
Py_XDECREF(preexec_fn_args_tuple);
Py_XDECREF(gc_module);

if (pid == -1)
return NULL; /* fork() failed. Exception set earlier. */
if (pid == -1) {
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);

Expand Down