Skip to content

Move collection error abort from pytest_runtestloop to pytest_collection - #15384

Open
megacodes24 wants to merge 2 commits into
pytest-dev:mainfrom
megacodes24:move-collection-error-check
Open

megacodes24 wants to merge 2 commits into
pytest-dev:mainfrom
megacodes24:move-collection-error-check

Conversation

@megacodes24

@megacodes24 megacodes24 commented Nov 13, 2026 •

Copy link
Copy Markdown

Closes #15028.

The check that stops the session after collection errors lived in the default pytest_runtestloop. Since that hook is firstresult, a plugin that implements its own pytest_runtestloop never reached the check, and tests ran even though collection failed. As suggested by @bluetech in the issue, this moves the check into the default pytest_collection.

This picks up #15061 by @gyanu2507, which the author closed and invited others to continue. Compared to that PR:

  • In the pytest_collection docstring, Interrupted is written as a literal instead of :class:~pytest.Interrupted. Interrupted is not exported from the pytest namespace, so I believe that reference is what failed the Read the Docs build there (the docs build is nitpicky).
  • Added a second test checking that with --continue-on-collection-errors, a plugin's pytest_runtestloop still runs.

Changes:

  • src/_pytest/main.py: moved the testsfailed check from pytest_runtestloop to pytest_collection.
  • src/_pytest/hookspec.py: updated the pytest_collection and pytest_runtestloop docstrings.
  • testing/test_collection.py: two new tests.
  • changelog/15028.breaking.rst: breaking change entry, since the current behavior is documented.

How I tested (Windows, Python 3.12):

  • Reproduced the issue with a small project whose conftest.py replaces pytest_runtestloop and a test file that fails to import. Before the change: 1 passed, 1 error. After: Interrupted: 1 error during collection.

  • With the original main.py, the first new test fails (INTERNAL_ERROR instead of INTERRUPTED), and it passes with the change.

  • testing/test_collection.py: 109 passed. test_main.py, test_terminal.py, acceptance_test.py pass except test_pydoc, which also fails on main on my machine (UnicodeDecodeError, a Windows encoding issue unrelated to this change).

  • pre-commit passes on the changed files.

  • Include new tests or update existing tests when applicable.

  • Allow maintainers to push and squash when merging my commits.

  • Add text like closes #XYZW to the PR description and/or commits.

  • If AI agents were used, they are credited in Co-authored-by commit trailers.

  • Create a new changelog file in the changelog directory.

  • Add yourself to AUTHORS in alphabetical order.

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 13, 2026

Copy link
Copy Markdown
Member

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

The move itself looks right, and the docs fix over #15061 checks out (its RTD build failed, this one passes).

One thing blocks it: pytest-xdist. xdist workers implement their own pytest_runtestloop, which is why pytest -n2 with a collection error runs the remaining tests today. With the check in pytest_collection, each worker now raises Interrupted after collecting, reports exit status 2, and the controller reads that as a keyboard interrupt and restarts the worker. I simulated the change (a pytest_collection wrapper doing the same check) on main with xdist master:

[gw0] node down: keyboard-interrupt
replacing crashed worker gw0
...
xdist: maximum crashed workers reached: 8
! xdist.dsession.Interrupted: <WorkerController gw9> received keyboard-interrupt !
1 error in 1.29s

The run does stop, which is what #15028 wants, but through a restart storm with a misleading message. So this needs a matching change in pytest-xdist before it can be merged, most likely the controller handling a collection abort from a worker as a clean stop instead of a crash. Are you interested in doing the xdist side too? Otherwise we can take it from here. The changelog should also mention plugins that run collection in worker processes.


Generated by Claude Code

@megacodes24

Copy link
Copy Markdown
Author

Thanks for testing it with xdist! Yes, I'd like to do the xdist side too.

My plan, following how pytest-dev/pytest-xdist#1408 handles usage errors: in remote.py's pytest_sessionfinish, when the worker session ends with Interrupted from the collection check (and not a real KeyboardInterrupt), record that in workeroutput. Then in dsession.worker_workerfinished, treat it as a clean stop (set shouldstop, remove the node, trigger shutdown) instead of the keyboard-interrupt/restart path. For the test, I'd use a conftest pytest_collection wrapper that does the same check, so it works with the current pytest release too.

I'll also update the changelog here to mention plugins that run collection in worker processes. Does that sound right?

@megacodes24

megacodes24 commented Nov 28, 2026 •

Copy link
Copy Markdown
Author

Update: I have a working fix for the xdist side. One change from the plan above: pytest catches Interrupted in wrap_session without re-raising it, so sys.exc_info() is empty in pytest_sessionfinish. Instead the worker records it in pytest_keyboard_interrupt, only for Session.Interrupted, so a real Ctrl+C still takes the existing path.

With it, pytest -n2 with a collection error stops with Interrupted: 1 error during collection, with no worker restarts, and the passing test is no longer reported as "crashed while running". The xdist test suite passes with pytest 9.2.0 and with this branch. Three existing xdist tests needed a version check, since the exit code becomes 2 instead of 1 with pytest 10. I'll open it as a draft PR on pytest-xdist and link it here. The changelog here is updated too.

Draft PR: pytest-dev/pytest-xdist#1439

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.

Collection-error abort check lives in pytest_runtestloop instead of pytest_collection

2 participants