@@ -124,10 +124,8 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
124124
125125#ifdef HAVE_FORK
126126/* This function is called from PyOS_AfterFork_Child to ensure that
127- * newly created child processes do not share locks with the parent.
128- */
129-
130- void
127+ newly created child processes do not share locks with the parent. */
128+ PyStatus
131129_PyRuntimeState_ReInitThreads (_PyRuntimeState * runtime )
132130{
133131 // This was initially set in _PyRuntimeState_Init().
@@ -138,23 +136,20 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
138136 PyMemAllocatorEx old_alloc ;
139137 _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
140138
141- int interp_mutex = _PyThread_at_fork_reinit (& runtime -> interpreters .mutex );
142- int main_interp_id_mutex = _PyThread_at_fork_reinit (& runtime -> interpreters .main -> id_mutex );
143- int xidregistry_mutex = _PyThread_at_fork_reinit (& runtime -> xidregistry .mutex );
139+ int reinit_interp = _PyThread_at_fork_reinit (& runtime -> interpreters .mutex );
140+ int reinit_main_id = _PyThread_at_fork_reinit (& runtime -> interpreters .main -> id_mutex );
141+ int reinit_xidregistry = _PyThread_at_fork_reinit (& runtime -> xidregistry .mutex );
144142
145143 PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
146144
147- if (interp_mutex < 0 ) {
148- Py_FatalError ("Can't initialize lock for runtime interpreters" );
149- }
150-
151- if (main_interp_id_mutex < 0 ) {
152- Py_FatalError ("Can't initialize ID lock for main interpreter" );
153- }
145+ if (reinit_interp < 0
146+ || reinit_main_id < 0
147+ || reinit_xidregistry < 0 )
148+ {
149+ return _PyStatus_ERR ("Failed to reinitialize runtime locks" );
154150
155- if (xidregistry_mutex < 0 ) {
156- Py_FatalError ("Can't initialize lock for cross-interpreter data registry" );
157151 }
152+ return _PyStatus_OK ();
158153}
159154#endif
160155
@@ -373,19 +368,20 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
373368}
374369
375370
371+ #ifdef HAVE_FORK
376372/*
377373 * Delete all interpreter states except the main interpreter. If there
378374 * is a current interpreter state, it *must* be the main interpreter.
379375 */
380- void
376+ PyStatus
381377_PyInterpreterState_DeleteExceptMain (_PyRuntimeState * runtime )
382378{
383379 struct _gilstate_runtime_state * gilstate = & runtime -> gilstate ;
384380 struct pyinterpreters * interpreters = & runtime -> interpreters ;
385381
386382 PyThreadState * tstate = _PyThreadState_Swap (gilstate , NULL );
387383 if (tstate != NULL && tstate -> interp != interpreters -> main ) {
388- Py_FatalError ("not main interpreter" );
384+ return _PyStatus_ERR ("not main interpreter" );
389385 }
390386
391387 HEAD_LOCK (runtime );
@@ -411,10 +407,12 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
411407 HEAD_UNLOCK (runtime );
412408
413409 if (interpreters -> head == NULL ) {
414- Py_FatalError ("missing main interpreter" );
410+ return _PyStatus_ERR ("missing main interpreter" );
415411 }
416412 _PyThreadState_Swap (gilstate , tstate );
413+ return _PyStatus_OK ();
417414}
415+ #endif
418416
419417
420418PyInterpreterState *
@@ -1259,29 +1257,32 @@ _PyGILState_Fini(PyThreadState *tstate)
12591257 gilstate -> autoInterpreterState = NULL ;
12601258}
12611259
1260+ #ifdef HAVE_FORK
12621261/* Reset the TSS key - called by PyOS_AfterFork_Child().
12631262 * This should not be necessary, but some - buggy - pthread implementations
12641263 * don't reset TSS upon fork(), see issue #10517.
12651264 */
1266- void
1265+ PyStatus
12671266_PyGILState_Reinit (_PyRuntimeState * runtime )
12681267{
12691268 struct _gilstate_runtime_state * gilstate = & runtime -> gilstate ;
12701269 PyThreadState * tstate = _PyGILState_GetThisThreadState (gilstate );
12711270
12721271 PyThread_tss_delete (& gilstate -> autoTSSkey );
12731272 if (PyThread_tss_create (& gilstate -> autoTSSkey ) != 0 ) {
1274- Py_FatalError ( "Could not allocate TSS entry" );
1273+ return _PyStatus_NO_MEMORY ( );
12751274 }
12761275
12771276 /* If the thread had an associated auto thread state, reassociate it with
12781277 * the new key. */
12791278 if (tstate &&
12801279 PyThread_tss_set (& gilstate -> autoTSSkey , (void * )tstate ) != 0 )
12811280 {
1282- Py_FatalError ( "Couldn't create autoTSSkey mapping " );
1281+ return _PyStatus_ERR ( "failed to set autoTSSkey " );
12831282 }
1283+ return _PyStatus_OK ();
12841284}
1285+ #endif
12851286
12861287/* When a thread state is created for a thread by some mechanism other than
12871288 PyGILState_Ensure, it's important that the GILState machinery knows about
0 commit comments