Skip to content

Commit 21a0a6c

Browse files
authored
threading_cleanup() failure marks test as ENV_CHANGED (#2500)
If threading_cleanup() fails to cleanup threads, set a a new support.environment_altered flag to true, flag uses by save_env which is used by regrtest to check if a test altered the environment. At the end, the test file fails with ENV_CHANGED instead of SUCCESS, to report that it altered the environment.
1 parent 729780a commit 21a0a6c

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/libregrtest/runtest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ def runtest(ns, test):
103103
faulthandler.dump_traceback_later(ns.timeout, exit=True)
104104
try:
105105
support.match_tests = ns.match_tests
106+
# reset the environment_altered flag to detect if a test altered
107+
# the environment
108+
support.environment_altered = False
106109
if ns.failfast:
107110
support.failfast = True
108111
if output_on_failure:

Lib/test/libregrtest/save_env.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,13 @@ def __enter__(self):
268268
def __exit__(self, exc_type, exc_val, exc_tb):
269269
saved_values = self.saved_values
270270
del self.saved_values
271-
support.gc_collect() # Some resources use weak references
271+
272+
# Some resources use weak references
273+
support.gc_collect()
274+
275+
# Read support.environment_altered, set by support helper functions
276+
self.changed |= support.environment_altered
277+
272278
for name, get, restore in self.resource_info():
273279
current = get()
274280
original = saved_values.pop(name)

Lib/test/support/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,6 +2011,14 @@ def modules_cleanup(oldmodules):
20112011
#=======================================================================
20122012
# Threading support to prevent reporting refleaks when running regrtest.py -R
20132013

2014+
# Flag used by saved_test_environment of test.libregrtest.save_env,
2015+
# to check if a test modified the environment. The flag should be set to False
2016+
# before running a new test.
2017+
#
2018+
# For example, threading_cleanup() sets the flag is the function fails
2019+
# to cleanup threads.
2020+
environment_altered = False
2021+
20142022
# NOTE: we use thread._count() rather than threading.enumerate() (or the
20152023
# moral equivalent thereof) because a threading.Thread object is still alive
20162024
# until its __bootstrap() method has returned, even after it has been
@@ -2026,6 +2034,8 @@ def threading_setup():
20262034
return 1, ()
20272035

20282036
def threading_cleanup(*original_values):
2037+
global environment_altered
2038+
20292039
if not _thread:
20302040
return
20312041
_MAX_COUNT = 100
@@ -2037,6 +2047,8 @@ def threading_cleanup(*original_values):
20372047
time.sleep(0.01)
20382048
gc_collect()
20392049
else:
2050+
environment_altered = True
2051+
20402052
dt = time.monotonic() - t0
20412053
print("Warning -- threading_cleanup() failed to cleanup %s threads "
20422054
"after %.0f sec (count: %s, dangling: %s)"

0 commit comments

Comments
 (0)