mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
bpo-16500: Allow registering at-fork handlers (#1715)
* bpo-16500: Allow registering at-fork handlers * Address Serhiy's comments * Add doc for new C API * Add doc for new Python-facing function * Add NEWS entry + doc nit
This commit is contained in:
parent
f931fd1c2a
commit
346cbd351e
15 changed files with 365 additions and 68 deletions
|
@ -559,9 +559,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
|
|||
int need_to_reenable_gc = 0;
|
||||
char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
|
||||
Py_ssize_t arg_num;
|
||||
#ifdef WITH_THREAD
|
||||
int import_lock_held = 0;
|
||||
#endif
|
||||
int need_after_fork = 0;
|
||||
|
||||
if (!PyArg_ParseTuple(
|
||||
args, "OOpO!OOiiiiiiiiiiO:fork_exec",
|
||||
|
@ -657,10 +655,8 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
|
|||
preexec_fn_args_tuple = PyTuple_New(0);
|
||||
if (!preexec_fn_args_tuple)
|
||||
goto cleanup;
|
||||
#ifdef WITH_THREAD
|
||||
_PyImport_AcquireLock();
|
||||
import_lock_held = 1;
|
||||
#endif
|
||||
PyOS_BeforeFork();
|
||||
need_after_fork = 1;
|
||||
}
|
||||
|
||||
if (cwd_obj != Py_None) {
|
||||
|
@ -686,7 +682,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
|
|||
* This call may not be async-signal-safe but neither is calling
|
||||
* back into Python. The user asked us to use hope as a strategy
|
||||
* to avoid deadlock... */
|
||||
PyOS_AfterFork();
|
||||
PyOS_AfterFork_Child();
|
||||
}
|
||||
|
||||
child_exec(exec_array, argv, envp, cwd,
|
||||
|
@ -703,17 +699,10 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
|
|||
/* Capture the errno exception before errno can be clobbered. */
|
||||
PyErr_SetFromErrno(PyExc_OSError);
|
||||
}
|
||||
#ifdef WITH_THREAD
|
||||
if (preexec_fn != Py_None
|
||||
&& _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
|
||||
PyErr_SetString(PyExc_RuntimeError,
|
||||
"not holding the import lock");
|
||||
pid = -1;
|
||||
}
|
||||
import_lock_held = 0;
|
||||
#endif
|
||||
|
||||
/* Parent process */
|
||||
if (need_after_fork)
|
||||
PyOS_AfterFork_Parent();
|
||||
if (envp)
|
||||
_Py_FreeCharPArray(envp);
|
||||
if (argv)
|
||||
|
@ -733,10 +722,8 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
|
|||
return PyLong_FromPid(pid);
|
||||
|
||||
cleanup:
|
||||
#ifdef WITH_THREAD
|
||||
if (import_lock_held)
|
||||
_PyImport_ReleaseLock();
|
||||
#endif
|
||||
if (need_after_fork)
|
||||
PyOS_AfterFork_Parent();
|
||||
if (envp)
|
||||
_Py_FreeCharPArray(envp);
|
||||
if (argv)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue