Skip to content

fix: handle pytest.exit with non-zero exit codes properly#1320

Open
BlocksecPHD wants to merge 2 commits intopytest-dev:masterfrom
BlocksecPHD:fix/handle-pytest-exit-1239
Open

fix: handle pytest.exit with non-zero exit codes properly#1320
BlocksecPHD wants to merge 2 commits intopytest-dev:masterfrom
BlocksecPHD:fix/handle-pytest-exit-1239

Conversation

@BlocksecPHD
Copy link
Copy Markdown

Summary

Fixes #1239

pytest.exit() with non-zero exit code caused INTERNALERROR in xdist. The assertion assert not crashitem failed because the scheduler had pending tests when the worker exited.

Changes

  • Handle non-zero exit statuses (other than 2) similarly to keyboard-interrupt in worker_workerfinished
  • Worker exits with custom exit codes now trigger shouldstop and proper shutdown
  • Add test case to verify INTERNALERROR is not raised

Testing

  • Added test_pytest_exit_nonzero_exitcode test case
  • Verified fix works locally with Python 3.12

Note

The session exit status is INTERRUPTED (2) due to shouldstop handling, but the fix prevents INTERNALERROR which was the main issue reported in #1239.

Funan Zhou added 2 commits April 9, 2026 07:24
Issue pytest-dev#1239: pytest.exit() with non-zero exit code caused INTERNALERROR
in xdist. The assertion 'assert not crashitem' failed because the
scheduler had pending tests when the worker exited.

Changes:
- Handle non-zero exit statuses (other than 2) similarly to keyboard-interrupt
- Worker exits with custom exit codes now trigger shouldstop and proper shutdown
- Add test case to verify INTERNALERROR is not raised

Note: The session exit status is INTERRUPTED (2) due to shouldstop handling,
but the fix prevents INTERNALERROR which was the main issue.
The previous fix was too aggressive - handling all non-zero exitstatuses
as pytest.exit cases. This broke normal test execution where exitstatus=1
means tests failed normally.

The correct approach: check if crashitem is non-empty after removing the
node. If there are pending tests when the worker exits, handle it
gracefully instead of raising INTERNALERROR.

This fixes issue pytest-dev#1239 without breaking normal test workflows.
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