Skip to content

Commit 0c759fe

Browse files
committed
Issue #10517: After fork(), reinitialize the TLS used by the PyGILState_*
APIs, to avoid a crash with the pthread implementation in RHEL 5. Patch by Charles-François Natali.
1 parent 43ae619 commit 0c759fe

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

Include/pystate.h

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
131131
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
132132
#ifdef WITH_THREAD
133133
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
134+
PyAPI_FUNC(void) _PyGILState_Reinit(void);
134135
#endif
135136

136137
PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.2.1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #10517: After fork(), reinitialize the TLS used by the PyGILState_*
14+
APIs, to avoid a crash with the pthread implementation in RHEL 5. Patch
15+
by Charles-François Natali.
16+
1317
- Issue #6780: fix starts/endswith error message to mention that tuples are
1418
accepted too.
1519

Modules/signalmodule.c

+1
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,7 @@ void
991991
PyOS_AfterFork(void)
992992
{
993993
#ifdef WITH_THREAD
994+
_PyGILState_Reinit();
994995
PyEval_ReInitThreads();
995996
main_thread = PyThread_get_thread_ident();
996997
main_pid = getpid();

Python/pystate.c

+17
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,23 @@ _PyGILState_Fini(void)
585585
autoInterpreterState = NULL;
586586
}
587587

588+
/* Reset the TLS key - called by PyOS_AfterFork.
589+
* This should not be necessary, but some - buggy - pthread implementations
590+
* don't flush TLS on fork, see issue #10517.
591+
*/
592+
void
593+
_PyGILState_Reinit(void)
594+
{
595+
PyThreadState *tstate = PyGILState_GetThisThreadState();
596+
PyThread_delete_key(autoTLSkey);
597+
if ((autoTLSkey = PyThread_create_key()) == -1)
598+
Py_FatalError("Could not allocate TLS entry");
599+
600+
/* re-associate the current thread state with the new key */
601+
if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
602+
Py_FatalError("Couldn't create autoTLSkey mapping");
603+
}
604+
588605
/* When a thread state is created for a thread by some mechanism other than
589606
PyGILState_Ensure, it's important that the GILState machinery knows about
590607
it so it doesn't try to create another thread state for the thread (this is

0 commit comments

Comments
 (0)