From 346cbd351ee0dd3ab9cb9f0e4cb625556707877e Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 27 May 2017 17:50:54 +0200 Subject: 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 --- Modules/_posixsubprocess.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) (limited to 'Modules/_posixsubprocess.c') diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c index d1434d59f8..5228fecfa9 100644 --- a/Modules/_posixsubprocess.c +++ b/Modules/_posixsubprocess.c @@ -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) -- cgit v1.2.1