Skip to content

Stop cleanly when a worker interrupts the session after collection errors - #1439

Draft
megacodes24 wants to merge 2 commits into
pytest-dev:masterfrom
megacodes24:collection-abort-clean-stop
Draft

megacodes24 wants to merge 2 commits into
pytest-dev:masterfrom
megacodes24:collection-abort-clean-stop

Conversation

@megacodes24

@megacodes24 megacodes24 commented Nov 28, 2026 •

Copy link
Copy Markdown

Companion to pytest-dev/pytest#15384, which moves the collection-error abort from pytest_runtestloop to pytest_collection (pytest-dev/pytest#15028). With that change, each xdist worker raises Interrupted after collecting, exits with status 2, and the controller treats it as a keyboard interrupt: it keeps restarting workers until "maximum crashed workers reached", and a test that was already scheduled is reported as "worker crashed while running".

Changes:

  • remote.py: the worker records the message in workeroutput["interrupted"] from pytest_keyboard_interrupt when the exception is pytest.Session.Interrupted (pytest_sessionfinish can't see it, because wrap_session catches it without re-raising). A real Ctrl+C raises a plain KeyboardInterrupt, so it keeps the current behavior.
  • dsession.py: in worker_workerfinished, an interrupted worker now stops the session cleanly (sets shouldstop to the worker's message, removes the node, triggers shutdown) instead of going through worker_errordown and restarting.
  • Tests: new test_worker_collection_errors_abort (1 and 2 workers), which simulates the pytest change with a conftest pytest_collection wrapper so it also runs with released pytest. test_n1_import_error and test_collection_crash now accept exit code 1 or INTERRUPTED, and test_n2_import_error checks the output based on the exit code. I first used a pytest.version_tuple >= (10,) check, but pytest main already reports 10.0.0.dev0 without Move collection error abort from pytest_runtestloop to pytest_collection pytest#15384, so this way the tests pass whichever PR is merged first.

Result with the pytest branch: pytest -n2 with an import error ends with xdist.dsession.Interrupted: 1 error during collection, with no restarts.

Tested on Windows, Python 3.12: the full test suite passes with pytest 9.2.0 and with the pytest-dev/pytest#15384 branch. pre-commit passes.

Copy link
Copy Markdown
Member

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

Thanks for picking this up. I checked it against the pytest-dev/pytest#15384 branch: on master pytest -n2 with an import error restarts workers up to gw9 and reports the passing tests as crashed, and with this PR it stops with Interrupted: 1 error during collection and no restarts. With pytest 9.2 nothing changes, as intended. pytest_keyboard_interrupt is the right place to catch it.

One problem is the overlap with #1438, which reworks the same exit paths for pytest.exit() and usage errors. Both PRs add a pytest_keyboard_interrupt method to WorkerInteractor, and git merges remote.py without a conflict, so the class ends up with two methods of the same name. The second one wins, and #1438's pytest.exit() handling stops working (10 of its tests fail on a trial merge).

#1438 keeps these cases under one private workeroutput key, _TERMINAL_ERROR_KEY, as (kind, payload). This PR fits in as one more kind:

# remote.py, in #1438's pytest_keyboard_interrupt
elif isinstance(excinfo.value, pytest.Session.Interrupted):
    workeroutput = self.config.workeroutput
    workeroutput[_TERMINAL_ERROR_KEY] = ("interrupted", str(excinfo.value))

# dsession.py, in _terminal_worker_error
if kind == "interrupted":
    return Interrupted(payload)

With that and your tests on top of #1438, the full suite passes with pytest 9.2 and with the pytest-dev/pytest#15384 branch. It also removes the node from the scheduler the same way the other terminal exits do. I'd like to land #1438 first and then rebase this onto it in that form. Does that work for you?


Generated by Claude Code

@megacodes24

Copy link
Copy Markdown
Author

Thanks for checking it and for the trial merge, I hadn't noticed that #1438 adds the same hook. That works for me. I tried your suggestion on top of #1438 (the "interrupted" kind in pytest_keyboard_interrupt and _terminal_worker_error, plus my tests) and the tests pass with released pytest, pytest main and the pytest-dev/pytest#15384 branch. I'll keep this as a draft and rebase it onto #1438 in that form once it's merged.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants