From 3f1bdbccf0745ff3d2d25ba6cbb1626af0d991e6 Mon Sep 17 00:00:00 2001 From: Aleksandr Mezin Date: Sun, 4 Sep 2022 19:21:33 +0600 Subject: [PATCH 1/3] Add TestCrashItem test to expose the bug Apparently, mark_test_pending() does not work if the test is the last (or only) one scheduled on the worker. Everything is shutting down before the test has a chance to run. --- testing/test_newhooks.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/testing/test_newhooks.py b/testing/test_newhooks.py index 012f1ea7..0ef7d76d 100644 --- a/testing/test_newhooks.py +++ b/testing/test_newhooks.py @@ -94,3 +94,22 @@ def pytest_handlecrashitem(crashitem, report, sched): res = pytester.runpytest("-n2", "-s") res.stdout.fnmatch_lines_random(["*HOOK: pytest_handlecrashitem"]) res.stdout.fnmatch_lines(["*3 passed*"]) + + def test_handlecrashitem_one(self, pytester: pytest.Pytester) -> None: + """Test pytest_handlecrashitem hook with just one test.""" + pytester.makeconftest( + """ + test_runs = 0 + + def pytest_handlecrashitem(crashitem, report, sched): + global test_runs + + if test_runs == 0: + sched.mark_test_pending(crashitem) + test_runs = 1 + else: + print("HOOK: pytest_handlecrashitem") + """ + ) + res = pytester.runpytest("-n1", "-s", "-k", "test_b") + res.stdout.fnmatch_lines_random(["*HOOK: pytest_handlecrashitem"]) From 6ea8a45c53720dff9ab08cd0d766dcbc7da8363a Mon Sep 17 00:00:00 2001 From: Aleksandr Mezin Date: Sun, 4 Sep 2022 20:48:36 +0600 Subject: [PATCH 2/3] Cancel shutdown when crashed worker is restarted Otherwise, a test rescheduled from pytest_handlecrashitem hook won't have a chance to run. Fix test failure from the previous commit. --- changelog/813.bugfix | 1 + src/xdist/dsession.py | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog/813.bugfix diff --git a/changelog/813.bugfix b/changelog/813.bugfix new file mode 100644 index 00000000..5d084b50 --- /dev/null +++ b/changelog/813.bugfix @@ -0,0 +1 @@ +Cancel shutdown when a crashed worker is restarted. \ No newline at end of file diff --git a/src/xdist/dsession.py b/src/xdist/dsession.py index 2ae3db6b..a950df62 100644 --- a/src/xdist/dsession.py +++ b/src/xdist/dsession.py @@ -225,6 +225,7 @@ def worker_errordown(self, node, error): self.triggershutdown() else: self.report_line("\nreplacing crashed worker %s" % node.gateway.id) + self.shuttingdown = False self._clone_node(node) self._active_nodes.remove(node) From 3f2840d9a2c13d2b787b6b4434cd3537e4177177 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 6 Sep 2022 17:29:01 -0300 Subject: [PATCH 3/3] Improve the test --- changelog/813.bugfix | 2 +- testing/test_newhooks.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/changelog/813.bugfix b/changelog/813.bugfix index 5d084b50..251cf4b9 100644 --- a/changelog/813.bugfix +++ b/changelog/813.bugfix @@ -1 +1 @@ -Cancel shutdown when a crashed worker is restarted. \ No newline at end of file +Cancel shutdown when a crashed worker is restarted. diff --git a/testing/test_newhooks.py b/testing/test_newhooks.py index 0ef7d76d..dcd2bc05 100644 --- a/testing/test_newhooks.py +++ b/testing/test_newhooks.py @@ -113,3 +113,9 @@ def pytest_handlecrashitem(crashitem, report, sched): ) res = pytester.runpytest("-n1", "-s", "-k", "test_b") res.stdout.fnmatch_lines_random(["*HOOK: pytest_handlecrashitem"]) + res.stdout.fnmatch_lines( + [ + "FAILED test_handlecrashitem_one.py::test_b", + "FAILED test_handlecrashitem_one.py::test_b", + ] + )