diff -ur cpython.ori/Python/pystate.c cpython/Python/pystate.c --- cpython.ori/Python/pystate.c 2011-04-12 00:57:21.000000000 +0000 +++ cpython/Python/pystate.c 2011-04-27 06:47:50.000000000 +0000 @@ -38,7 +38,7 @@ GILState implementation */ static PyInterpreterState *autoInterpreterState = NULL; -static int autoTLSkey = 0; +int autoTLSkey = 0; /* needed by thread_pthread to flush TLS upon fork */ #else #define HEAD_INIT() /* Nothing */ #define HEAD_LOCK() /* Nothing */ diff -ur cpython.ori/Python/thread_pthread.h cpython/Python/thread_pthread.h --- cpython.ori/Python/thread_pthread.h 2011-04-12 00:57:21.000000000 +0000 +++ cpython/Python/thread_pthread.h 2011-04-27 06:47:41.000000000 +0000 @@ -128,6 +128,8 @@ #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; } +extern int autoTLSkey; + /* * Initialization. */ @@ -615,6 +617,18 @@ return pthread_getspecific(key); } +/* Reinit the TLS key upon fork. This should not be necessary with pthread, but + * some - buggy - pthread implementations don't flush TLS upon fork, see issue + * #10517 */ void PyThread_ReInitTLS(void) -{} +{ + PyThreadState *tstate = PyGILState_GetThisThreadState(); + PyThread_delete_key(autoTLSkey); + if ((autoTLSkey = PyThread_create_key()) == -1) + Py_FatalError("Could not allocate TLS entry"); + + /* re-associate the current thread state with the new key */ + if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) + Py_FatalError("Couldn't create autoTLSkey mapping"); +}