Index: Python/ceval.c =================================================================== --- Python/ceval.c (revision 60025) +++ Python/ceval.c (working copy) @@ -311,7 +311,13 @@ #ifdef WITH_THREAD if (interpreter_lock) { int err = errno; + extern int _py_exiting; PyThread_acquire_lock(interpreter_lock, 1); + if (_py_exiting && main_thread && PyThread_get_thread_ident() != main_thread) { + PyThread_release_lock(interpreter_lock); + PyThread_exit_thread(); + assert(0); /* Something's seriously buggered if this is reached */ + } errno = err; } #endif Index: Python/thread_pthread.h =================================================================== --- Python/thread_pthread.h (revision 60025) +++ Python/thread_pthread.h (working copy) @@ -226,29 +226,19 @@ } static void -do_PyThread_exit_thread(int no_cleanup) +do_PyThread_exit_thread(void) { dprintf(("PyThread_exit_thread called\n")); - if (!initialized) { - if (no_cleanup) - _exit(0); - else - exit(0); - } + assert(initialized); + pthread_exit(0); } void PyThread_exit_thread(void) { - do_PyThread_exit_thread(0); + do_PyThread_exit_thread(); } -void -PyThread__exit_thread(void) -{ - do_PyThread_exit_thread(1); -} - #ifndef NO_EXIT_PROG static void do_PyThread_exit_prog(int status, int no_cleanup) Index: Python/pythonrun.c =================================================================== --- Python/pythonrun.c (revision 60025) +++ Python/pythonrun.c (working copy) @@ -117,6 +117,7 @@ } static int initialized = 0; +int _py_exiting = 0; /* API to access the initialized flag -- useful for esoteric use */ @@ -365,6 +366,7 @@ */ call_sys_exitfunc(); initialized = 0; + _py_exiting = 1; /* Get current thread state and interpreter pointer */ tstate = PyThreadState_GET();