Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1439.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stop the session cleanly when a worker is interrupted after collection errors (with pytest 10), instead of restarting workers until the maximum is reached and reporting scheduled tests as crashed.
12 changes: 11 additions & 1 deletion src/xdist/dsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,17 @@ def worker_workerfinished(self, node: WorkerController) -> None:
messages = [f"{node} exited with a usage error"]
self.triggershutdown()
raise pytest.UsageError(*messages)
if node.workeroutput["exitstatus"] == 2: # keyboard-interrupt
if node.workeroutput["exitstatus"] == 2:
interrupted = node.workeroutput.get("interrupted")
if interrupted is not None:
# The worker stopped the session on purpose (for instance after
# collection errors): stop cleanly instead of restarting it.
self._active_nodes.remove(node)
if not self.shouldstop:
self.shouldstop = interrupted
self.triggershutdown()
return
# keyboard-interrupt
self.shouldstop = f"{node} received keyboard-interrupt"
self.worker_errordown(node, "keyboard-interrupt")
return
Expand Down
10 changes: 10 additions & 0 deletions src/xdist/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ def pytest_sessionfinish(self, exitstatus: int) -> Generator[None, object, objec
self.sendevent("workerfinished", workeroutput=workeroutput)
return result

@pytest.hookimpl
def pytest_keyboard_interrupt(
self, excinfo: pytest.ExceptionInfo[BaseException]
) -> None:
# pytest.Session.Interrupted is raised on purpose, for instance after
# collection errors; a real Ctrl+C raises a plain KeyboardInterrupt.
if isinstance(excinfo.value, pytest.Session.Interrupted):
workeroutput: dict[str, Any] = self.config.workeroutput # type: ignore[attr-defined]
workeroutput["interrupted"] = str(excinfo.value)

@pytest.hookimpl
def pytest_collection(self) -> None:
self.sendevent("collectionstart")
Expand Down
52 changes: 49 additions & 3 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,40 @@ def test_unknown_marker_usage_error(
)
assert "INTERNALERROR>" not in result.stdout.str()

@pytest.mark.parametrize("workers", [1, 2])
def test_worker_collection_errors_abort(
self, pytester: pytest.Pytester, workers: int
) -> None:
pytester.makepyfile(
test_ok="def test_ok(): pass",
test_broken="import this_module_does_not_exist",
)
# Simulate pytest-dev/pytest#15028: the session is interrupted
# from pytest_collection when there were collection errors.
pytester.makeconftest(
"""
import pytest

@pytest.hookimpl(wrapper=True)
def pytest_collection(session):
result = yield
if (
session.testsfailed
and not session.config.option.continue_on_collection_errors
):
raise session.Interrupted("1 error during collection")
return result
"""
)

result = pytester.runpytest(f"-n{workers}")

assert result.ret == pytest.ExitCode.INTERRUPTED
result.stdout.fnmatch_lines(["*1 error during collection*"])
assert "replacing crashed worker" not in result.stdout.str()
assert "keyboard-interrupt" not in result.stdout.str()
assert "crashed while running" not in result.stdout.str()

def test_n1_pass(self, pytester: pytest.Pytester) -> None:
p1 = pytester.makepyfile(
"""
Expand Down Expand Up @@ -104,7 +138,9 @@ def test_import():
"""
)
result = pytester.runpytest(p1, "-n1")
assert result.ret == 1
# Once pytest-dev/pytest#15384 is in, pytest interrupts the session after
# collection errors, so the exit code is INTERRUPTED instead of 1.
assert result.ret in (1, pytest.ExitCode.INTERRUPTED)
result.stdout.fnmatch_lines(
["E *Error: No module named *__import_of_missing_module*"]
)
Expand All @@ -121,7 +157,15 @@ def test_import():
)
result1 = pytester.runpytest(p1, "-n2")
result2 = pytester.runpytest(p1, "-n1")
assert len(result1.stdout.lines) == len(result2.stdout.lines)
if result1.ret == pytest.ExitCode.INTERRUPTED:
# With pytest-dev/pytest#15384 the session stops after the first
# collection error, possibly before every worker reported its
# collection, so the outputs can differ slightly. Check that the
# error is still reported only once.
for result in (result1, result2):
assert result.stdout.str().count("ERROR collecting") == 1
else:
assert len(result1.stdout.lines) == len(result2.stdout.lines)

def test_n1_skip(self, pytester: pytest.Pytester) -> None:
p1 = pytester.makepyfile(
Expand Down Expand Up @@ -1837,7 +1881,9 @@ def test_collection_crash(pytester: pytest.Pytester) -> None:
"""
)
result = pytester.runpytest(p1, "-n1")
assert result.ret == 1
# Once pytest-dev/pytest#15384 is in, pytest interrupts the session after
# collection errors, so the exit code is INTERRUPTED instead of 1.
assert result.ret in (1, pytest.ExitCode.INTERRUPTED)
result.stdout.fnmatch_lines(
[
"created: 1/1 worker",
Expand Down