PyOS_AfterFork_Child() uses PyStatus (GH-20596)
PyOS_AfterFork_Child() helper functions now return a PyStatus:
PyOS_AfterFork_Child() is now responsible to handle errors.
* Move _PySignal_AfterFork() to the internal C API
* Add #ifdef HAVE_FORK on _PyGILState_Reinit(), _PySignal_AfterFork()
and _PyInterpreterState_DeleteExceptMain().
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 7471844..afb6d18 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -34,6 +34,7 @@
#include "pycore_ceval.h" // _PyEval_ReInitThreads()
#include "pycore_import.h" // _PyImport_ReInitLock()
+#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "structmember.h" // PyMemberDef
#ifndef MS_WINDOWS
@@ -461,15 +462,41 @@
void
PyOS_AfterFork_Child(void)
{
+ PyStatus status;
_PyRuntimeState *runtime = &_PyRuntime;
- _PyGILState_Reinit(runtime);
- _PyEval_ReInitThreads(runtime);
- _PyImport_ReInitLock();
+
+ status = _PyGILState_Reinit(runtime);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto fatal_error;
+ }
+
+ status = _PyEval_ReInitThreads(runtime);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto fatal_error;
+ }
+
+ status = _PyImport_ReInitLock();
+ if (_PyStatus_EXCEPTION(status)) {
+ goto fatal_error;
+ }
+
_PySignal_AfterFork();
- _PyRuntimeState_ReInitThreads(runtime);
- _PyInterpreterState_DeleteExceptMain(runtime);
+
+ status = _PyRuntimeState_ReInitThreads(runtime);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto fatal_error;
+ }
+
+ status = _PyInterpreterState_DeleteExceptMain(runtime);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto fatal_error;
+ }
run_at_forkers(_PyInterpreterState_GET()->after_forkers_child, 0);
+ return;
+
+fatal_error:
+ Py_ExitStatusException(status);
}
static int
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 6d340a6..24dbd42 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -1796,14 +1796,17 @@
return 1;
}
+
+#ifdef HAVE_FORK
static void
_clear_pending_signals(void)
{
- int i;
- if (!_Py_atomic_load(&is_tripped))
+ if (!_Py_atomic_load(&is_tripped)) {
return;
+ }
+
_Py_atomic_store(&is_tripped, 0);
- for (i = 1; i < NSIG; ++i) {
+ for (int i = 1; i < NSIG; ++i) {
_Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
}
}
@@ -1816,6 +1819,8 @@
* the interpreter had an opportunity to call the handlers. issue9535. */
_clear_pending_signals();
}
+#endif /* HAVE_FORK */
+
int
_PyOS_IsMainThread(void)