diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-11-04 00:56:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-04 00:56:34 (GMT) |
commit | 02f8f781dac62112362fa8a96f5c08deccb7b27f (patch) | |
tree | a5cf221938c20be78a39fa60b58d963f7bfe918b /Lib/test/support/threading_helper.py | |
parent | 1a95ad68b4156fbd04bd5c479f3ef9ca30f0a8f5 (diff) | |
download | cpython-02f8f781dac62112362fa8a96f5c08deccb7b27f.zip cpython-02f8f781dac62112362fa8a96f5c08deccb7b27f.tar.gz cpython-02f8f781dac62112362fa8a96f5c08deccb7b27f.tar.bz2 |
[3.12] gh-111644: Fix support threading_cleanup() (GH-111714) (#111716)
gh-111644: Fix support threading_cleanup() (GH-111714)
Copy the list of dangling threads to make sure that the list of
"Dangling thread" is complete. Previously, the list was incomplete if
threads completed just before the list was displayed.
Changes:
* Rewrite the warning to make it easier to understand.
* Use support.sleeping_retry().
* threading_cleanup() no longer copies threading._dangling,
but only counts the number of dangling thread.
* Remove support.gc_support() call.
(cherry picked from commit f62c7ccf9abf6e0493978da9cf9ca43adcd403f9)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Lib/test/support/threading_helper.py')
-rw-r--r-- | Lib/test/support/threading_helper.py | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/Lib/test/support/threading_helper.py b/Lib/test/support/threading_helper.py index 7f16050..afa25a7 100644 --- a/Lib/test/support/threading_helper.py +++ b/Lib/test/support/threading_helper.py @@ -22,34 +22,37 @@ from test import support def threading_setup(): - return _thread._count(), threading._dangling.copy() + return _thread._count(), len(threading._dangling) def threading_cleanup(*original_values): - _MAX_COUNT = 100 - - for count in range(_MAX_COUNT): - values = _thread._count(), threading._dangling - if values == original_values: - break - - if not count: - # Display a warning at the first iteration - support.environment_altered = True - dangling_threads = values[1] - support.print_warning(f"threading_cleanup() failed to cleanup " - f"{values[0] - original_values[0]} threads " - f"(count: {values[0]}, " - f"dangling: {len(dangling_threads)})") - for thread in dangling_threads: - support.print_warning(f"Dangling thread: {thread!r}") - - # Don't hold references to threads - dangling_threads = None - values = None - - time.sleep(0.01) - support.gc_collect() + orig_count, orig_ndangling = original_values + + timeout = 1.0 + for _ in support.sleeping_retry(timeout, error=False): + # Copy the thread list to get a consistent output. threading._dangling + # is a WeakSet, its value changes when it's read. + dangling_threads = list(threading._dangling) + count = _thread._count() + + if count <= orig_count: + return + + # Timeout! + support.environment_altered = True + support.print_warning( + f"threading_cleanup() failed to clean up threads " + f"in {timeout:.1f} seconds\n" + f" before: thread count={orig_count}, dangling={orig_ndangling}\n" + f" after: thread count={count}, dangling={len(dangling_threads)}") + for thread in dangling_threads: + support.print_warning(f"Dangling thread: {thread!r}") + + # The warning happens when a test spawns threads and some of these threads + # are still running after the test completes. To fix this warning, join + # threads explicitly to wait until they complete. + # + # To make the warning more likely, reduce the timeout. def reap_threads(func): |