Restore higher-scoped teardown when a flaky condition is falsy - #351
Open
teddytennant wants to merge 1 commit into
Open
Restore higher-scoped teardown when a flaky condition is falsy#351teddytennant wants to merge 1 commit into
teddytennant wants to merge 1 commit into
Conversation
teddytennant
force-pushed
the
fix-falsy-condition-finalizer-leak
branch
from
August 24, 2026 16:11
a56b20c to
72c7004
Compare
The teardown hook suspended module, class and session finalizers whenever the rerun limit and failure statuses allowed it, but it never checked the flaky marker's condition. With a falsy condition the protocol then declined to rerun, nothing restored the finalizers, and higher-scoped fixture teardown was skipped for the rest of the session.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pytest_runtest_teardowndecides whether to suspend module, class and session finalizers using a gate that checks the rerun limit, the failure statuses and terminal errors. It never checks theflakymarker'scondition, which_should_not_rerundoes check.So with a falsy condition on a failing test, teardown suspends the higher-scoped finalizers, the protocol then declines to rerun,
_restore_suspended_finalizersis never reached, and that fixture's teardown never runs again for the rest of the session:module teardownis not printed. Withcondition=True, or with no marker at all, it is. The damage is not local either, sincesuspended_finalizersis a module-level global: run that file alongside another and the first module's fixture stays un-torn-down past the end of its own module.Adding the missing term to the gate is the whole fix. I also updated the comment above it, which enumerated the terms and was already one short.
Tests cover class, module and session scope, a string condition that evaluates falsy, and the cross-module case. Two are guards rather than reproductions:
condition=Truestill reruns and still tears down exactly once, and a plain--rerunsfailure with no marker is unaffected.Scope, so you do not have to ask. This fixes the
conditiongap only. Two related leaks are pre-existing and untouched. A terminal error raised in the teardown phase still leaks, because_terminal_errors["teardown"]is written frompytest_runtest_makereportafter the teardown hook has already suspended, so it needs a hookwrapper rather than another gate term. And a call-phase failure consisting solely of failed subtests tears higher-scoped fixtures down prematurely on every attempt, which is the same drift in the opposite direction. Happy to take either as a follow-up if you want them.