fix: stop worker when shouldfail/shouldstop is set (--exitfirst / --maxfail)#1338
Open
goingforstudying-ctrl wants to merge 2 commits into
Open
Conversation
added 2 commits
June 16, 2026 00:38
When --exitfirst or --maxfail is used with pytest-xdist, the worker pre-fetches the next test via torun.get() inside run_one_test before the current test finishes. If the current test fails, pytest sets session.shouldfail, but the pre-fetched test still runs because the check happens AFTER run_one_test completes, not before. Add a shouldfail/shouldstop check in the while loop before calling run_one_test to catch the flag set during the previous iteration. Additionally, after sending runtest_protocol_complete, overwrite nextitem_index to Marker.SHUTDOWN so the while loop exits cleanly without executing the pre-fetched test. Fixes pytest-dev#420 Fixes pytest-dev#868 Fixes pytest-dev#1034
The shouldfail/shouldstop check inside run_one_test() already sets nextitem_index to Marker.SHUTDOWN when the flags are set, causing the while loop to exit naturally. The duplicate break after run_one_test() is unreachable and triggers a mypy error. Signed-off-by: goingforstudying-ctrl <goingforstudying-ctrl@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
--exitfirst/--maxfailflags don't stop workers promptly when a test fails. The failing worker keeps running — it executes the next pre-fetched test before checkingshouldfail.Turns out the issue is in the worker's main loop:
torun.get()pre-fetches the NEXT test before the current one finishes. When the current test fails,session.shouldfailgets set by pytest's runner. Butrun_one_testalready moved on — the pre-fetched item is about to execute and theshouldfailcheck afterrun_one_testcomes too late.Noticed this while looking at #420 (open since 2019), #868, and #1034 — they all point to the same underlying thing.
What this does:
shouldfail/shouldstopbeforerun_one_testin the while loop, so a locally-set flag from the previous iteration is caughtruntest_protocol_complete, ifshouldfail/shouldstopis truthy, overridesnextitem_indextoMarker.SHUTDOWN— this makes the loop exit cleanly without running the already-fetched next itemWith
-xset, the worker that hits a failure now stops right after the failing test instead of running one more.Happy to adjust the approach if there's a better place to hook this.