threading_cleanup() failure marks test as ENV_CHANGED (#2500) · python/cpython@21a0a6c (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -103,6 +103,9 @@ def runtest(ns, test):
103 103 faulthandler.dump_traceback_later(ns.timeout, exit=True)
104 104 try:
105 105 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
106 109 if ns.failfast:
107 110 support.failfast = True
108 111 if output_on_failure:
Original file line number Diff line number Diff line change
@@ -268,7 +268,13 @@ def __enter__(self):
268 268 def __exit__(self, exc_type, exc_val, exc_tb):
269 269 saved_values = self.saved_values
270 270 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 +
272 278 for name, get, restore in self.resource_info():
273 279 current = get()
274 280 original = saved_values.pop(name)
Original file line number Diff line number Diff line change
@@ -2011,6 +2011,14 @@ def modules_cleanup(oldmodules):
2011 2011 #=======================================================================
2012 2012 # Threading support to prevent reporting refleaks when running regrtest.py -R
2013 2013
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 +
2014 2022 # NOTE: we use thread._count() rather than threading.enumerate() (or the
2015 2023 # moral equivalent thereof) because a threading.Thread object is still alive
2016 2024 # until its __bootstrap() method has returned, even after it has been
@@ -2026,6 +2034,8 @@ def threading_setup():
2026 2034 return 1, ()
2027 2035
2028 2036 def threading_cleanup(*original_values):
2037 +global environment_altered
2038 +
2029 2039 if not _thread:
2030 2040 return
2031 2041 _MAX_COUNT = 100
@@ -2037,6 +2047,8 @@ def threading_cleanup(*original_values):
2037 2047 time.sleep(0.01)
2038 2048 gc_collect()
2039 2049 else:
2050 +environment_altered = True
2051 +
2040 2052 dt = time.monotonic() - t0
2041 2053 print("Warning -- threading_cleanup() failed to cleanup %s threads "
2042 2054 "after %.0f sec (count: %s, dangling: %s)"