Skip to content

Commit e165c2f

Browse files
authored
Use "nodes * 4" as default for --max-worker-restart (#436)
Use "nodes * 4" as default for --max-worker-restart
2 parents a630e55 + bbc5416 commit e165c2f

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

changelog/226.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``--max-worker-restart`` now assumes a more reasonable value (4 times the number of
2+
nodes) when not given explicitly. This prevents test suites from running forever when the suite crashes during collection.

src/xdist/dsession.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ def __init__(self, config):
4646
self._failed_collection_errors = {}
4747
self._active_nodes = set()
4848
self._failed_nodes_count = 0
49-
self._max_worker_restart = self.config.option.maxworkerrestart
50-
if self._max_worker_restart is not None:
51-
self._max_worker_restart = int(self._max_worker_restart)
49+
self._max_worker_restart = get_default_max_worker_restart(self.config)
50+
5251
try:
5352
self.terminal = config.pluginmanager.getplugin("terminalreporter")
5453
except KeyError:
@@ -390,10 +389,16 @@ def pytest_testnodedown(self, node, error):
390389
return
391390
self.write_line("[%s] node down: %s" % (node.gateway.id, error))
392391

393-
# def pytest_xdist_rsyncstart(self, source, gateways):
394-
# targets = ",".join([gw.id for gw in gateways])
395-
# msg = "[%s] rsyncing: %s" %(targets, source)
396-
# self.write_line(msg)
397-
# def pytest_xdist_rsyncfinish(self, source, gateways):
398-
# targets = ", ".join(["[%s]" % gw.id for gw in gateways])
399-
# self.write_line("rsyncfinish: %s -> %s" %(source, targets))
392+
393+
def get_default_max_worker_restart(config):
394+
"""gets the default value of --max-worker-restart option if it is not provided.
395+
396+
Use a reasonable default to avoid workers from restarting endlessly due to crashing collections (#226).
397+
"""
398+
result = config.option.maxworkerrestart
399+
if result is not None:
400+
result = int(result)
401+
elif config.option.numprocesses:
402+
# if --max-worker-restart was not provided, use a reasonable default (#226)
403+
result = config.option.numprocesses * 4
404+
return result

testing/test_dsession.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from xdist.dsession import DSession
1+
from xdist.dsession import DSession, get_default_max_worker_restart
22
from xdist.report import report_collection_diff
33
from xdist.scheduler import EachScheduling, LoadScheduling
44

@@ -268,6 +268,24 @@ def test_report_collection_diff_equal():
268268
assert report_collection_diff(from_collection, to_collection, 1, 2) is None
269269

270270

271+
def test_default_max_worker_restart():
272+
class config:
273+
class option:
274+
maxworkerrestart = None
275+
numprocesses = 0
276+
277+
assert get_default_max_worker_restart(config) is None
278+
279+
config.option.numprocesses = 2
280+
assert get_default_max_worker_restart(config) == 8
281+
282+
config.option.maxworkerrestart = "1"
283+
assert get_default_max_worker_restart(config) == 1
284+
285+
config.option.maxworkerrestart = "0"
286+
assert get_default_max_worker_restart(config) == 0
287+
288+
271289
def test_report_collection_diff_different():
272290
"""Test reporting of different collections."""
273291
from_collection = ["aaa", "bbb", "ccc", "YYY"]

0 commit comments

Comments
 (0)