This script can cause deadlock on Python 3.6.
1. Other threads can register traceback to sys module after sys module dict is cleaned up.
2. In Py_Finalize, __del__ method of arbitrary object in the traceback can be called.
3. The __del__ method calls threading.Thread.start()
4. Thread.start() waits self.__started event which is set by Thread.__bootstrap
5. While finalizing, threads other than main thread exits when getting GIL. So Thread.__bootstrap() won't be called.
6. Thread.start() in main thread waits self.__started event forever.
I suggest thread_PyThread_start_new_thread() raise RuntimeError("can't start thread: finalizing") |