diff -r e97d9926da83 Lib/test/test_threading.py --- a/Lib/test/test_threading.py Thu Oct 31 18:44:48 2013 +0000 +++ b/Lib/test/test_threading.py Fri Nov 01 00:39:17 2013 +0100 @@ -701,6 +701,10 @@ class ThreadJoinOnShutdown(BaseTestCase) import sys import time import threading + import warnings + + # ignore "unclosed file ..." warnings + warnings.filterwarnings('ignore', '', ResourceWarning) thread_has_run = set() diff -r e97d9926da83 Python/pythonrun.c --- a/Python/pythonrun.c Thu Oct 31 18:44:48 2013 +0000 +++ b/Python/pythonrun.c Fri Nov 01 00:39:17 2013 +0100 @@ -566,11 +566,11 @@ Py_Finalize(void) _Py_Finalizing = tstate; initialized = 0; - /* Flush stdout+stderr */ - flush_std_files(); + /* Destroy the state of all threads except the current thread */ + _PyThreadState_DeleteExcept(tstate); - /* Disable signal handling */ - PyOS_FiniInterrupts(); + /* Clear the state of the current thread */ + PyThreadState_Clear(tstate); /* Collect garbage. This may call finalizers; it's nice to call these * before all modules are destroyed. @@ -585,6 +585,7 @@ Py_Finalize(void) * XXX I haven't seen a real-life report of either of these. */ PyGC_Collect(); + #ifdef COUNT_ALLOCS /* With COUNT_ALLOCS, it helps to run GC multiple times: each collection might release some types from the type @@ -592,6 +593,13 @@ Py_Finalize(void) while (PyGC_Collect() > 0) /* nothing */; #endif + + /* Flush stdout+stderr */ + flush_std_files(); + + /* Disable signal handling */ + PyOS_FiniInterrupts(); + /* Destroy all modules */ PyImport_Cleanup();