Repository navigation
Always call pytest_runtest_logfinish, even on Exit/KeyboardInterrupt - #15316
Open
doubledare704 wants to merge 3 commits into
Open
doubledare704 wants to merge 3 commits into
doubledare704 wants to merge 3 commits into
Conversation
Exit (always) and KeyboardInterrupt (unless --pdb) are reraised out of runtestprotocol, so pytest_runtest_protocol skipped the logfinish hook and left the item's logstart unpaired. Plugins that emit output or clean up state in logfinish silently lost the last item. Wrap runtestprotocol in try/finally so logfinish is always called. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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_protocolcallslogstart, thenruntestprotocol, thenlogfinishwith notry/finally.Exit(always) andKeyboardInterrupt(unless--pdb) are reraised out ofruntestprotocol(seeget_reraise_exceptions), so onpytest.exit()or Ctrl-C the current item gets alogstartbut never alogfinish.This was pointed out as a prerequisite in #1004 (comment): moving per-item terminal output to
logfinish(for #1004 / #14761) would otherwise silently drop the last item's line on Ctrl-C. It's a latent bug on its own for any plugin that emits output or cleans up per-item state inlogfinish.Reproducer, with a conftest that prints from
pytest_runtest_logfinish:Before: no logfinish output. After: logfinish runs, then the session exits as before.
The fix wraps
runtestprotocolintry/finally. Added a parametrized test intest_runner.pyfor bothpytest.exit()andKeyboardInterrupt, checkinglogstart/logfinishare paired for the interrupted item and that nothing after it starts.Co-authored-bycommit trailers.changelogdirectory.AUTHORSin alphabetical order.🤖 Generated with Claude Code
Why this change uses try/finally instead of catching specific exceptions?
Exit and KeyboardInterrupt are deliberately reraised by CallInfo.from_call (via get_reraise_exceptions), so they escape runtestprotocol and skipped logfinish. finally keeps them propagating unchanged while guaranteeing the logstart/logfinish pairing. It doesn’t need to mirror the config-dependent reraise list, and it also covers exceptions raised from report hooks. Same pattern runtestprotocol already uses for clearing request state.