Skip to content

bpo-31234: threading_cleanup() now warns immediately #3138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2017
Merged
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
28 changes: 19 additions & 9 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down