Skip to content

Fix: Handle pytest.exit() from workers gracefully#1347

Open
C1-BA-B1-F3 wants to merge 1 commit into
pytest-dev:masterfrom
C1-BA-B1-F3:fix-pytest-exit-internal-error
Open

Fix: Handle pytest.exit() from workers gracefully#1347
C1-BA-B1-F3 wants to merge 1 commit into
pytest-dev:masterfrom
C1-BA-B1-F3:fix-pytest-exit-internal-error

Conversation

@C1-BA-B1-F3

Copy link
Copy Markdown

Summary

  • Detect pytest.exit() on workers via the pytest_keyboard_interrupt hook
  • Propagate exit reason and return code to the controller
  • Handle graceful worker exit without triggering internal error
  • Re-raise pytest.exit.Exception in the controller to preserve the requested return code

Fixes #1239

Changes

src/xdist/remote.py (Worker side):

  • Added pytest_keyboard_interrupt hook in WorkerInteractor to detect pytest.exit.Exception
  • Stores exitreason and exitreturncode in workeroutput when pytest.exit() is called

src/xdist/dsession.py (Controller side):

  • Added _worker_exit field to track worker exit via pytest.exit()
  • In worker_workerfinished, checks for exit info and handles gracefully (no crashitem assertion)
  • In pytest_runtestloop, re-raises pytest.exit.Exception to preserve return code

testing/acceptance_test.py:

  • Added parametrized test for pytest.exit() with return codes 0 and 1
  • Verifies no INTERNALERROR and correct exit behavior

Tests

  • Ran pytest testing/acceptance_test.py::TestNodeFailure::test_pytest_exit_is_not_reported_as_internal_error -v - PASSED
  • Ran pytest testing/acceptance_test.py::TestNodeFailure -v - All existing tests PASSED

Closes #1239

When pytest.exit() is called in a worker test, it raises
pytest.exit.Exception (a BaseException subclass). Previously, the
worker's runtest loop would exit without marking the current test as
complete, causing the controller's scheduler to treat it as a crash.

This fix:
- Adds a pytest_keyboard_interrupt hook in WorkerInteractor to detect
  pytest.exit.Exception and store exit info in workeroutput
- In DSession.worker_workerfinished, checks for the exit info and
  handles it gracefully without asserting crashitem
- In pytest_runtestloop, re-raises pytest.exit.Exception to preserve
  the requested return code

Fixes pytest-dev#1239
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.

pytest.exit causes internal error

1 participant