diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-09-13 20:42:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-13 20:42:00 (GMT) |
commit | ace1ecc00b35a8b1dc6e352d547dde07913017bb (patch) | |
tree | c3df0738fe0767df7a99c6d81658c0b12839ca67 | |
parent | 88031a9adedb594500db643404614f6648beec81 (diff) | |
download | cpython-ace1ecc00b35a8b1dc6e352d547dde07913017bb.zip cpython-ace1ecc00b35a8b1dc6e352d547dde07913017bb.tar.gz cpython-ace1ecc00b35a8b1dc6e352d547dde07913017bb.tar.bz2 |
bpo-31234: threading_cleanup() now warns immediately (#3138)
support.threading_cleanup() waits for 1 second before emitting a
warning if there are threads running in the background. With this
change, it now emits the warning immediately, to be able to catch
bugs more easily.
-rw-r--r-- | Lib/test/support/__init__.py | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index f57b251..df23505 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -2030,22 +2030,32 @@ def threading_cleanup(*original_values): global environment_altered _MAX_COUNT = 100 - t0 = time.monotonic() + 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 + environment_altered = True + dangling_threads = values[1] + print("Warning -- threading_cleanup() failed to cleanup " + "%s threads (count: %s, dangling: %s)" + % (values[0] - original_values[0], + values[0], len(dangling_threads)), + file=sys.stderr) + for thread in dangling_threads: + print(f"Dangling thread: {thread!r}", file=sys.stderr) + sys.stderr.flush() + + # Don't hold references to threads + dangling_threads = None + values = None + time.sleep(0.01) gc_collect() - else: - environment_altered = True - dt = time.monotonic() - t0 - print("Warning -- threading_cleanup() failed to cleanup %s threads " - "after %.0f sec (count: %s, dangling: %s)" - % (values[0] - original_values[0], dt, - values[0], len(values[1])), - file=sys.stderr) def reap_threads(func): """Use this function when threads are being used. This will |