Skip to content

Keep thread timeouts working without the terminal plugin - #226

Open
yqia03 wants to merge 1 commit into
pytest-dev:mainfrom
yqia03:fix/thread-timeout-without-terminal
Open

yqia03 wants to merge 1 commit into
pytest-dev:mainfrom
yqia03:fix/thread-timeout-without-terminal

Conversation

@yqia03

@yqia03 yqia03 commented Nov 16, 2026

Copy link
Copy Markdown

Summary

With --timeout-method=thread -p no:terminal, a test that exceeds its timeout can still finish and return exit code 0. config.get_terminal_writer() raises in the timer thread before the try/finally containing os._exit(1), so the timeout never terminates the process.

Keep the existing terminal reporter when available and otherwise use a TerminalWriter on stderr. Create it after disabling capture so it writes to the restored stream. Move writer acquisition inside the protected block and guard its final flush. Add a changelog entry and regression tests.

timeout -> disable capture -> existing reporter or stderr writer -> diagnostics -> exit(1)

Evidence

A test containing time.sleep(2) with --timeout=1 --timeout-method=thread -p no:terminal exits 0 before the fix and 1 afterward.

  • All five new regressions failed on the unchanged source and pass with the fix. They cover fd, sys, tee-sys, no capture, and the disabled capture plugin, asserting exit status, preserved output, stack diagnostics, and that execution does not reach the end of the test. Each nested run has a 20-second outer timeout.
  • Complete suite, macOS / Python 3.14.7 / pytest 9.2.0: 67 passed, 1 skipped, 6 xfailed. Unchanged baseline: 62 passed, 1 skipped, 6 xfailed.
  • Complete suite, macOS / Python 3.11.18 / minimum supported pytest 8.4.0: 66 passed, 2 skipped, 6 xfailed.
  • Configured pre-commit run --all-files --show-diff-on-failure passed, including Ruff, YAML and typo checks.
  • Independent probes also covered capsys and capfd with global capture disabled; both preserve buffered output and terminate with a stack dump.

Merge considerations

The change is limited to thread-timeout reporting; the signal method and debugger detection keep their current behavior. Linux, Windows and the remaining Python versions were not run locally and remain for CI. This change is reversible without migration.

Prepared with AI assistance and independently reviewed by another agent; that review is not maintainer approval.

Copilot AI balanced review requested due to automatic review settings November 16, 2026 02:25

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. Try requesting a review at a lower effort level.

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants