Skip to content

Commit 83b90e5

Browse files
committed
feat: reset worker after file
1 parent 911ac6d commit 83b90e5

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

src/xdist/plugin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ def pytest_addoption(parser: pytest.Parser) -> None:
9797
help="Maximum number of workers that can be restarted "
9898
"when crashed (set to zero to disable this feature)",
9999
)
100+
group.addoption(
101+
"--dist-reset-workers",
102+
action="store_true",
103+
dest="dist_reset_workers",
104+
default=False,
105+
help="If set, workers are restarted after each test file.",
106+
)
100107
group.addoption(
101108
"--dist",
102109
metavar="distmode",

src/xdist/remote.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def __init__(self, config: pytest.Config, channel: execnet.Channel) -> None:
119119
self.channel = channel
120120
self.torun = TestQueue(self.channel.gateway.execmodel)
121121
self.nextitem_index: int | None | Literal[Marker.SHUTDOWN] = None
122+
self.shutdown_on_finish = False
122123
config.pluginmanager.register(self)
123124

124125
def sendevent(self, name: str, **kwargs: object) -> None:
@@ -170,6 +171,8 @@ def handle_command(
170171
self.torun.put(i)
171172
elif name == "shutdown":
172173
self.torun.put(Marker.SHUTDOWN)
174+
elif name == "shutdown_after_finished":
175+
self.shutdown_on_finish = True
173176
elif name == "steal":
174177
self.steal(kwargs["indices"])
175178

@@ -200,12 +203,15 @@ def steal(self, indices: Sequence[int]) -> None:
200203
@pytest.hookimpl
201204
def pytest_runtestloop(self, session: pytest.Session) -> bool:
202205
self.log("entering main loop")
206+
self.shutdown_on_finish = False
203207
self.channel.setcallback(self.handle_command, endmarker=Marker.SHUTDOWN)
204208
self.nextitem_index = self.torun.get()
205209
while self.nextitem_index is not Marker.SHUTDOWN:
206210
self.run_one_test()
207211
if session.shouldfail or session.shouldstop:
208212
break
213+
if self.shutdown_on_finish:
214+
os._exit(1)
209215
return True
210216

211217
def run_one_test(self) -> None:

src/xdist/scheduler/loadscope.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ def _assign_work_unit(self, node: WorkerController) -> None:
281281

282282
node.send_runtest_some(nodeids_indexes)
283283

284+
if self.config.option.dist_reset_workers:
285+
node.send_shutdown_after_finished()
286+
284287
def _split_scope(self, nodeid: str) -> str:
285288
"""Determine the scope (grouping) of a nodeid.
286289

src/xdist/workermanage.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ def send_runtest_all(self) -> None:
372372
def send_steal(self, indices: Sequence[int]) -> None:
373373
self.sendcommand("steal", indices=indices)
374374

375+
def send_shutdown_after_finished(self) -> None:
376+
self.sendcommand("shutdown_after_finished")
377+
375378
def shutdown(self) -> None:
376379
if not self._down:
377380
try:

0 commit comments

Comments
 (0)