]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44895: libregrtest: refleak check clears types later (GH-28113)
authorVictor Stinner <vstinner@python.org>
Wed, 1 Sep 2021 15:45:27 +0000 (17:45 +0200)
committerGitHub <noreply@github.com>
Wed, 1 Sep 2021 15:45:27 +0000 (17:45 +0200)
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 <1055913+iritkatriel@users.noreply.github.com>
Lib/test/libregrtest/refleak.py
Lib/test/libregrtest/runtest.py
Lib/test/libregrtest/utils.py
Misc/NEWS.d/next/Tests/2021-09-01-17-17-44.bpo-44895.kV7H77.rst [new file with mode: 0644]

index b94826a5daf92ce4a31db5e4d0b0a70b6c50b242..096b5381cd9339bca75d404f4f7a2042b90c7b26 100644 (file)
@@ -85,13 +85,15 @@ def dash_R(ns, test_name, test_func):
               flush=True)
 
     dash_R_cleanup(fs, ps, pic, zdc, abcs)
+    support.gc_collect()
 
     for i in rep_range:
         test_func()
+
         dash_R_cleanup(fs, ps, pic, zdc, abcs)
+        support.gc_collect()
 
-        # dash_R_cleanup() ends with collecting cyclic trash:
-        # read memory statistics immediately after.
+        # Read memory statistics immediately after the garbage collection
         alloc_after = getallocatedblocks() - _getquickenedcount()
         rc_after = gettotalrefcount()
         fd_after = fd_count()
@@ -166,9 +168,6 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
         zipimport._zip_directory_cache.clear()
         zipimport._zip_directory_cache.update(zdc)
 
-    # clear type cache
-    sys._clear_type_cache()
-
     # Clear ABC registries, restoring previously saved ABC registries.
     abs_classes = [getattr(collections.abc, a) for a in collections.abc.__all__]
     abs_classes = filter(isabstract, abs_classes)
@@ -179,8 +178,12 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
                     obj.register(ref())
             obj._abc_caches_clear()
 
+    # Clear caches
     clear_caches()
 
+    # Clear type cache at the end: previous function calls can modify types
+    sys._clear_type_cache()
+
 
 def warm_caches():
     # char cache
index 489ab986cb4e5e4d1ddc68263580eb1349c1a9b1..fe4693bad9ca6d2bd71ddfd42e608f7597931e90 100644 (file)
@@ -297,9 +297,13 @@ def _runtest_inner2(ns: Namespace, test_name: str) -> bool:
                 test_runner()
                 refleak = False
     finally:
-        cleanup_test_droppings(test_name, ns.verbose)
+        # First kill any dangling references to open files etc.
+        # This can also issue some ResourceWarnings which would otherwise get
+        # triggered during the following test run, and possibly produce
+        # failures.
+        support.gc_collect()
 
-    support.gc_collect()
+        cleanup_test_droppings(test_name, ns.verbose)
 
     if gc.garbage:
         support.environment_altered = True
@@ -330,6 +334,7 @@ def _runtest_inner(
 
     try:
         clear_caches()
+        support.gc_collect()
 
         with save_env(ns, test_name):
             refleak = _runtest_inner2(ns, test_name)
@@ -373,11 +378,6 @@ def _runtest_inner(
 
 
 def cleanup_test_droppings(test_name: str, verbose: int) -> None:
-    # First kill any dangling references to open files etc.
-    # This can also issue some ResourceWarnings which would otherwise get
-    # triggered during the following test run, and possibly produce failures.
-    support.gc_collect()
-
     # Try to clean up junk commonly left behind.  While tests shouldn't leave
     # any files or directories behind, when a test fails that can be tedious
     # for it to arrange.  The consequences can be especially nasty on Windows,
index 89d7e7e53540549597ee11ced9041acad21c31f3..ad18b50f5db99dd180ba661d4746c43bdb8ff92d 100644 (file)
@@ -217,5 +217,3 @@ def clear_caches():
     else:
         for f in typing._cleanups:
             f()
-
-    support.gc_collect()
diff --git a/Misc/NEWS.d/next/Tests/2021-09-01-17-17-44.bpo-44895.kV7H77.rst b/Misc/NEWS.d/next/Tests/2021-09-01-17-17-44.bpo-44895.kV7H77.rst
new file mode 100644 (file)
index 0000000..038466f
--- /dev/null
@@ -0,0 +1,5 @@
+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.
+Patch by Irit Katriel and Victor Stinner.