From a648ae63b4e8cfb571a39acf533e33137553cf91 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 11 Jun 2019 01:03:09 +0200 Subject: [PATCH] bpo-36402: Add test_threading.test_finalization_shutdown() --- Lib/test/test_threading.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 6ac4ea9623de0d0..b414b7cc4ceb07e 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -583,6 +583,41 @@ def __del__(self): self.assertEqual(data.splitlines(), ["GC: True True True"] * 2) + def test_finalization_shutdown(self): + # bpo-36402: Py_Finalize() calls threading._shutdown() which must wait + # until Python thread states of all threads get deleted. + # + # Test similar to SubinterpThreadingTests.test_threads_join_2(), but + # finalization of the main interpreter. + code = """if 1: + import os + import threading + import time + import random + + def random_sleep(): + seconds = random.random() * 0.010 + time.sleep(seconds) + + class Sleeper: + def __del__(self): + random_sleep() + + tls = threading.local() + + def f(): + # Sleep a bit so that the thread is still running when + # Py_EndInterpreter is called. + random_sleep() + tls.x = Sleeper() + random_sleep() + + threading.Thread(target=f).start() + random_sleep() + """ + rc, out, err = assert_python_ok("-c", code) + self.assertEqual(err, b"") + def test_tstate_lock(self): # Test an implementation detail of Thread objects. started = _thread.allocate_lock()