Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ea1c8d

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.
2 parents 2356659 + 0c759fe commit 9ea1c8d

4 files changed

Lines changed: 23 additions & 0 deletions

File tree

Include/pystate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
132132
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
133133
#ifdef WITH_THREAD
134134
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
135+
PyAPI_FUNC(void) _PyGILState_Reinit(void);
135136
#endif
136137

137138
PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.3 Alpha 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 #10914: Initialize correctly the filesystem codec when creating a new
1418
subinterpreter to fix a bootstrap issue with codecs implemented in Python, as
1519
the ISO-8859-15 codec.

Modules/signalmodule.c

Lines changed: 1 addition & 0 deletions
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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,23 @@ _PyGILState_Fini(void)
586586
autoInterpreterState = NULL;
587587
}
588588

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

0 commit comments

Comments
 (0)