Skip to content

faulthandler: keep faulthandler_timeout armed after a test failure - #15262

Open
mayur-dhavan wants to merge 4 commits into
pytest-dev:mainfrom
mayur-dhavan:mayur-dhavan-patch-1
Open

mayur-dhavan wants to merge 4 commits into
pytest-dev:mainfrom
mayur-dhavan:mayur-dhavan-patch-1

Conversation

@mayur-dhavan

Copy link
Copy Markdown

Fixes #15180.

Problem

The docs say faulthandler_timeout "dumps the tracebacks of all threads if a test takes longer than X seconds to run (including fixture setup and teardown)". In practice the timer was cancelled at the first failure in a test: call_and_report fires pytest_exception_interact for every setup/call/teardown exception (and for each failing subtest), and the faulthandler plugin implemented that hook solely to cancel the timeout. So a hanging fixture teardown after a failed test produced no traceback, and faulthandler_exit_on_timeout = true did not exit the process.

Fix

Remove the plugin's pytest_exception_interact hookimpl, mirroring the fix pytest-timeout shipped for the same bug (pytest-dev/pytest-timeout#197). The pdb case is still covered: entering pdb goes through pytestPDB._init_pdb, which calls the pytest_enter_pdb hook, where the plugin already cancels the timer.

Changes

  • src/_pytest/faulthandler.py: delete the pytest_exception_interact hookimpl.
  • testing/test_faulthandler.py: new regression test test_timeout_still_active_after_test_failure (failing test + 5s hanging teardown + faulthandler_timeout = 0.5 asserts the dump still happens); the old test_cancel_timeout_on_hook parametrization over pytest_exception_interact is replaced with a pytest_enter_pdb-only test documenting the intentional behavior change.
  • changelog/15180.bugfix.rst: towncrier entry.

Before the fix, the new regression test fails (no traceback dump); after it, testing/test_faulthandler.py passes fully (15 passed) and testing/test_runner.py passes (67 passed, 1 xfailed) on Python 3.13.

AI/LLM disclosure: this contribution was prepared with AI assistance (an automated coding agent acting on behalf of the author). The change, its rationale, and the tests were validated locally as described above, and I can explain every line on request.

Checklist

  • Include documentation when adding new features. (bug fix, no new feature)
  • Include new tests or update existing tests when applicable.
  • Add a changelog entry (changelog/15180.bugfix.rst).
  • Allow maintainers to edit the PR.

Removed the pytest_exception_interact hook implementation to prevent traceback dumping on interactive exceptions.
Add regression test for timeout behavior after test failure and update hook cancellation logic.
Fixes the cancellation of faulthandler_timeout during tests, ensuring it covers the entire test body and teardown process. Now, a hanging teardown after a failed test will dump a traceback and exit as documented.
Copilot AI balanced review requested due to automatic review settings October 25, 2026 18:44
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@OKAN-SOFTWARE OKAN-SOFTWARE left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 869f5bb against issue #15180.

The change addresses the reported root cause. Removing the pytest_exception_interact hook allows the timeout to remain active throughout the test lifecycle, including fixture teardown after an assertion failure.

The existing pytest_enter_pdb hook continues to handle timeout cancellation when entering the debugger.

The new regression test covers the reported hanging-teardown scenario, and the GitHub CI workflow passed.

Non-blocking coverage suggestion:

Consider adding a subprocess regression test with faulthandler_exit_on_timeout = true.

The original issue reports that a failure could prevent the configured timeout from terminating the process. The current regression verifies traceback output with the default exit setting but does not directly verify process termination.

An additional test could assert that the process exits after the timeout without completing the normal pytest failure summary.

The implementation looks consistent with the intended behavior. I have not independently rerun the tests.

No blocking issue found within the reviewed scope.

— Okan Tiktas

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

faulthandler_timeout stops covering a test once setup, call or a subtest fails (pytest_exception_interact cancels the timer)

3 participants