diff options
author | Antoine Pitrou <pitrou@free.fr> | 2017-05-27 17:50:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-27 17:50:54 +0200 |
commit | 346cbd351ee0dd3ab9cb9f0e4cb625556707877e (patch) | |
tree | 8590c5fc85acf57750ecb8d07a407a3dbe233f85 /Modules/_posixsubprocess.c | |
parent | f931fd1c2ad969db72460d3ab41e3d1a4a62c371 (diff) | |
download | cpython-git-346cbd351ee0dd3ab9cb9f0e4cb625556707877e.tar.gz |
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
Diffstat (limited to 'Modules/_posixsubprocess.c')
-rw-r--r-- | Modules/_posixsubprocess.c | 29 |
1 files changed, 8 insertions, 21 deletions
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) |