You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Session-end gc.collect() in unraisableexception plugin runs after warning filters are torn down, silently losing ResourceWarnings #14263
Hi there, I ran into an issue recently where ResourceWarnings weren't getting raised as errors as expected.
The unraisableexception plugin registers a config.add_cleanup() callback that forces 5 rounds of gc.collect() at session end to catch leaked resources. However, config.add_cleanup() pushes onto a contextlib.ExitStack (LIFO), and the warnings plugin registers its cleanup afterunraisableexception in the default_plugins tuple. This means:
warnings cleanup runs first — tears down the catch_warnings context, removing filterwarnings = error::ResourceWarning from pytest.ini
unraisableexception cleanup runs second — calls gc.collect(), finalizing unclosed resources. Their __del__ methods emit ResourceWarning, but the error filter is gone. The warning is silently discarded and never reaches sys.unraisablehook.
Reorder default_plugins: Swap unraisableexception and warnings so that unraisableexception's cleanup (including gc.collect()) runs while warning filters are still active.
Move the GC logic to pytest_unconfigure: pytest_unconfigure runs before_cleanup_stack.close(), so both warning filters and sys.unraisablehook are still wired up at that point. This avoids depending on registration order entirely.
I came across the issue you're facing and will suggest a fix like this to handle to issue, requesting the reviewed to review the changes committed and suggest on the enhancements.
Activity
coder-jatin-s commentedon Mar 7, 2026
Hi @djrobstep
I came across the issue you're facing and will suggest a fix like this to handle to issue, requesting the reviewed to review the changes committed and suggest on the enhancements.
clawdney commentedon Mar 8, 2026
Hi! I'm interested in helping with this. Could you share more context about the use case?
Stabilize pytest-dev#14263 wrap-still-fires test on PyPy
Cover add_note dedup branch for pytest-dev#14263 codecov patch=100%
Surface filter-promoted unraisable warnings directly (refs pytest-dev…
Narrow unwrap to filter-active case (refs pytest-dev#14263)
Strengthen pytest-dev#14263 regression test contract
Polish pytest-dev#14263 fix: types, AUTHORS, changelog, neighbor-test…
Trim pytest-dev#14263 comments to match house style
Stabilize pytest-dev#14263 wrap-still-fires test on PyPy
Cover add_note dedup branch for pytest-dev#14263 codecov patch=100%
Surface filter-promoted unraisable warnings directly (refs pytest-dev…
Narrow unwrap to filter-active case (refs pytest-dev#14263)
Strengthen pytest-dev#14263 regression test contract
Polish pytest-dev#14263 fix: types, AUTHORS, changelog, neighbor-test…
12 remaining items
Move unraisable GC to pytest_unconfigure (refs pytest-dev#14263)
Add failing regression test for unset stash guard (red)
Guard pytest_unconfigure against unset stash (refs pytest-dev#14263)
Surface filter-promoted unraisable warnings directly (pytest-dev#14263)…
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#340)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#454)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#225)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#263)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#590)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#456)
⬆️ Update dependency pytest to v9.1.1 (#24)
[SECURITY] Update dependency pytest to v9 [SECURITY] (#2)
chore(deps): update root dev dependencies to v9.1.0
Update dependency pytest to v9 (#35)