[3.9] bpo-37788: Fix reference leak when Thread is never joined (GH-26103) (GH-26142)

When a Thread is not joined after it has stopped, its lock may remain in the _shutdown_locks set until interpreter shutdown.  If many threads are created this way, the _shutdown_locks set could therefore grow endlessly.  To avoid such a situation, purge expired locks each time a new one is added or removed..
(cherry picked from commit c10c2ec7a0)

Co-authored-by: Antoine Pitrou <antoine@python.org>

Automerge-Triggered-By: GH:pitrou
This commit is contained in:
Antoine Pitrou 2021-05-15 11:51:20 +02:00 committed by GitHub
parent fa9de0c383
commit b30b25b266
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 1 deletions

View file

@ -805,6 +805,14 @@ class ThreadTests(BaseTestCase):
""")
self.assertEqual(out.rstrip(), b"thread_dict.atexit = 'value'")
def test_leak_without_join(self):
# bpo-37788: Test that a thread which is not joined explicitly
# does not leak. Test written for reference leak checks.
def noop(): pass
with support.wait_threads_exit():
threading.Thread(target=noop).start()
# Thread.join() is not called
class ThreadJoinOnShutdown(BaseTestCase):