Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down