Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
unraisableexception: reduce gc_collect_harder default to 1 on CPython
The 5-iteration default was borrowed from the Trio project, where it was
determined empirically to handle PyPy's object resurrection behavior: on
PyPy, objects like coroutines can survive GC rounds because executing their
__del__ can resurrect them.

On CPython, reference counting frees most objects immediately. One GC pass
is sufficient to handle reference cycles, as confirmed by all
test_unraisableexception tests passing (including the refcycle variants).

Use 1 pass on CPython and retain 5 on PyPy.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
  • Loading branch information
miketheman committed May 5, 2026
commit 5efe979d2bec0b92ef176bc5edddbe7b0e74a31b
11 changes: 8 additions & 3 deletions src/_pytest/unraisableexception.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@ def collect_unraisable(config: Config) -> None:
def cleanup(
*, config: Config, prev_hook: Callable[[sys.UnraisableHookArgs], object]
) -> None:
# A single collection doesn't necessarily collect everything.
# Constant determined experimentally by the Trio project.
gc_collect_iterations = config.stash.get(gc_collect_iterations_key, 5)
# On PyPy, objects (e.g. coroutines) can survive GC rounds because executing
# their __del__ can resurrect them. The Trio project determined experimentally
# that 5 passes are needed on PyPy to flush everything. On CPython, reference
# counting handles most cleanup immediately, so 1 pass is sufficient.
_default_gc_collect_iterations = 5 if hasattr(sys, "pypy_version_info") else 1
gc_collect_iterations = config.stash.get(
gc_collect_iterations_key, _default_gc_collect_iterations
)
try:
try:
gc_collect_harder(gc_collect_iterations)
Expand Down
Loading