Skip to content

Commit 679cb47

Browse files
bpo-44895: libregrtest: refleak check clears types later (GH-28113)
libregrtest now clears the type cache later to reduce the risk of false alarm when checking for reference leaks. Previously, the type cache was cleared too early and libregrtest raised a false alarm about reference leaks under very specific conditions. Move also support.gc_collect() outside clear/cleanup functions to make the garbage collection more explicit. Co-authored-by: Irit Katriel <[email protected]>
1 parent 863154c commit 679cb47

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

Lib/test/libregrtest/refleak.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ def get_pooled_int(value):
8585
flush=True)
8686

8787
dash_R_cleanup(fs, ps, pic, zdc, abcs)
88+
support.gc_collect()
8889

8990
for i in rep_range:
9091
test_func()
92+
9193
dash_R_cleanup(fs, ps, pic, zdc, abcs)
94+
support.gc_collect()
9295

93-
# dash_R_cleanup() ends with collecting cyclic trash:
94-
# read memory statistics immediately after.
96+
# Read memory statistics immediately after the garbage collection
9597
alloc_after = getallocatedblocks() - _getquickenedcount()
9698
rc_after = gettotalrefcount()
9799
fd_after = fd_count()
@@ -166,9 +168,6 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
166168
zipimport._zip_directory_cache.clear()
167169
zipimport._zip_directory_cache.update(zdc)
168170

169-
# clear type cache
170-
sys._clear_type_cache()
171-
172171
# Clear ABC registries, restoring previously saved ABC registries.
173172
abs_classes = [getattr(collections.abc, a) for a in collections.abc.__all__]
174173
abs_classes = filter(isabstract, abs_classes)
@@ -179,8 +178,12 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
179178
obj.register(ref())
180179
obj._abc_caches_clear()
181180

181+
# Clear caches
182182
clear_caches()
183183

184+
# Clear type cache at the end: previous function calls can modify types
185+
sys._clear_type_cache()
186+
184187

185188
def warm_caches():
186189
# char cache

Lib/test/libregrtest/runtest.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,13 @@ def _runtest_inner2(ns: Namespace, test_name: str) -> bool:
297297
test_runner()
298298
refleak = False
299299
finally:
300-
cleanup_test_droppings(test_name, ns.verbose)
300+
# First kill any dangling references to open files etc.
301+
# This can also issue some ResourceWarnings which would otherwise get
302+
# triggered during the following test run, and possibly produce
303+
# failures.
304+
support.gc_collect()
301305

302-
support.gc_collect()
306+
cleanup_test_droppings(test_name, ns.verbose)
303307

304308
if gc.garbage:
305309
support.environment_altered = True
@@ -330,6 +334,7 @@ def _runtest_inner(
330334

331335
try:
332336
clear_caches()
337+
support.gc_collect()
333338

334339
with save_env(ns, test_name):
335340
refleak = _runtest_inner2(ns, test_name)
@@ -373,11 +378,6 @@ def _runtest_inner(
373378

374379

375380
def cleanup_test_droppings(test_name: str, verbose: int) -> None:
376-
# First kill any dangling references to open files etc.
377-
# This can also issue some ResourceWarnings which would otherwise get
378-
# triggered during the following test run, and possibly produce failures.
379-
support.gc_collect()
380-
381381
# Try to clean up junk commonly left behind. While tests shouldn't leave
382382
# any files or directories behind, when a test fails that can be tedious
383383
# for it to arrange. The consequences can be especially nasty on Windows,

Lib/test/libregrtest/utils.py

-2
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,3 @@ def clear_caches():
217217
else:
218218
for f in typing._cleanups:
219219
f()
220-
221-
support.gc_collect()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
libregrtest now clears the type cache later to reduce the risk of false alarm
2+
when checking for reference leaks. Previously, the type cache was cleared too
3+
early and libregrtest raised a false alarm about reference leaks under very
4+
specific conditions.
5+
Patch by Irit Katriel and Victor Stinner.

0 commit comments

Comments
 (0)