From 6df6eefb6a8fe7d2d818a64eb44a93f0b6a9b2e1 Mon Sep 17 00:00:00 2001 From: Neil Schemenauer Date: Mon, 31 Aug 2026 14:54:35 -0700 Subject: [PATCH 1/3] gh-155981: Store refleak deltas in arrays (gh-55982) Store per-run deltas in array objects rather than lists of pooled integers. Large, unique deltas could otherwise grow int_pool and make the refleak checker report its own retained integers as reference leaks. (cherry picked from commit b38e073f1be8fe40af991c044a791cefff098f0d) --- Lib/test/libregrtest/refleak.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Lib/test/libregrtest/refleak.py b/Lib/test/libregrtest/refleak.py index 906ca9864c717f..adfc5e23308e49 100644 --- a/Lib/test/libregrtest/refleak.py +++ b/Lib/test/libregrtest/refleak.py @@ -1,6 +1,7 @@ import os import sys import warnings +from array import array from inspect import isabstract from typing import Any import linecache @@ -100,24 +101,20 @@ def runtest_refleak(test_name, test_func, for obj in ByteString.__subclasses__() + [ByteString]: # type: ignore[attr-defined] abcs[obj] = _get_dump(obj)[0] - # bpo-31217: Integer pool to get a single integer object for the same - # value. The pool is used to prevent false alarm when checking for memory - # block leaks. Fill the pool with values in -1000..1000 which are the most - # common (reference, memory block, file descriptor) differences. - int_pool = {value: value for value in range(-1000, 1000)} - def get_pooled_int(value): - return int_pool.setdefault(value, value) - warmups = hunt_refleak.warmups runs = hunt_refleak.runs filename = hunt_refleak.filename repcount = warmups + runs - # Pre-allocate to ensure that the loop doesn't allocate anything new + # Pre-allocate to ensure that the loop doesn't allocate anything new. + # Store the deltas as raw values in arrays rather than as int objects in + # lists: each unique delta stored as an object would live until the end of + # the loop and show up in the following repetition's reference and memory + # block deltas (gh-75400, gh-155981). rep_range = list(range(repcount)) - rc_deltas = [0] * repcount - alloc_deltas = [0] * repcount - fd_deltas = [0] * repcount + rc_deltas = array('q', [0]) * repcount + alloc_deltas = array('q', [0]) * repcount + fd_deltas = array('q', [0]) * repcount getallocatedblocks = sys.getallocatedblocks gettotalrefcount = sys.gettotalrefcount getunicodeinternedsize = sys.getunicodeinternedsize @@ -161,9 +158,9 @@ def get_pooled_int(value): rc_after = gettotalrefcount() fd_after = fd_count() - rc_deltas[i] = get_pooled_int(rc_after - rc_before) - alloc_deltas[i] = get_pooled_int(alloc_after - alloc_before) - fd_deltas[i] = get_pooled_int(fd_after - fd_before) + rc_deltas[i] = rc_after - rc_before + alloc_deltas[i] = alloc_after - alloc_before + fd_deltas[i] = fd_after - fd_before if not quiet: # use max, not sum, so total_leaks is one of the pooled ints From 6bd48af9fc42d4808a0bee68c968560e78830da5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 20 Nov 2026 00:42:03 +0100 Subject: [PATCH 2/3] Fix regrtest rerun: reset worker_bug (#162293) When a test fails with "worker bug" and tests are rerun in verbose mode, reset worker_bug so that tests are marked as success if the failed test doesn't fail again when rerun in verbose mode. TestResult.write_json_into(file) now calls also file.flush(). (cherry picked from commit f84083b08ab40b08c8d608a320008418b331932e) --- Lib/test/libregrtest/result.py | 1 + Lib/test/libregrtest/results.py | 1 + 2 files changed, 2 insertions(+) diff --git a/Lib/test/libregrtest/result.py b/Lib/test/libregrtest/result.py index 5324a46bb8cb1c..7180e2239bc1c7 100644 --- a/Lib/test/libregrtest/result.py +++ b/Lib/test/libregrtest/result.py @@ -222,6 +222,7 @@ def get_rerun_match_tests(self) -> FilterTuple | None: def write_json_into(self, file) -> None: json.dump(self, file, cls=_EncodeTestResult) + file.flush() @staticmethod def from_json(worker_json: StrJSON) -> 'TestResult': diff --git a/Lib/test/libregrtest/results.py b/Lib/test/libregrtest/results.py index ea71f53d3406a1..06dfb634d0cd05 100644 --- a/Lib/test/libregrtest/results.py +++ b/Lib/test/libregrtest/results.py @@ -171,6 +171,7 @@ def prepare_rerun(self, *, clear: bool = True) -> tuple[TestTuple, FilterDict]: self.bad.clear() self.env_changed.clear() self.rerun_results.clear() + self.worker_bug = False return (tuple(tests), match_tests_dict) From 5c9718c8df3bcbf4fae3dbad2f0275dc4254ae02 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 30 Nov 2026 12:06:46 +0100 Subject: [PATCH 3/3] Do not rerun failed tests in regrtest on worker bug (#162780) When a test fails with "worker bug", no longer reruns failed tests in verbose mode. Previously, re-running tests in verbose mode failed to run a test called "". (cherry picked from commit 352cc06f546f0ee89b5a48010bf5f27311bf1d55) --- Lib/test/libregrtest/main.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index 99206b904088c7..31bc5b82d06aae 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -566,11 +566,12 @@ def _run_tests(self, selected: TestTuple, tests: TestList | None) -> int: coverage = self.results.get_coverage_results() self.display_result(runtests) - if self.want_rerun and self.results.need_rerun(): - self.rerun_failed_tests(runtests) + if not self.results.worker_bug: + if self.want_rerun and self.results.need_rerun(): + self.rerun_failed_tests(runtests) - if self.want_bisect and self.results.need_rerun(): - self.run_bisect(runtests) + if self.want_bisect and self.results.need_rerun(): + self.run_bisect(runtests) finally: if use_load_tracker: self.logger.stop_load_tracker()