From 47919196867c9d4c3df0000b4e80f102eb5a3523 Mon Sep 17 00:00:00 2001 From: lixonik <71736299+lixonik@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:06:34 +0300 Subject: [PATCH] Fail semantic workers that exit without a result --- scripts/run_semantic_tests.py | 12 ++++++++---- tests/test_run_semantic_tests.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/scripts/run_semantic_tests.py b/scripts/run_semantic_tests.py index 514e781330..ca21b0b034 100644 --- a/scripts/run_semantic_tests.py +++ b/scripts/run_semantic_tests.py @@ -140,13 +140,13 @@ def positive_seconds(value): def read_count(path): - """The number of tests the child ran, as unittest's own result object reported it.""" + """The number of tests reported by unittest's result object, or None without a valid count.""" try: with open(path, encoding="utf-8") as handle: - return int(json.load(handle)["testsRun"]) + count = json.load(handle)["testsRun"] except (OSError, ValueError, KeyError, TypeError): - # The child wrote no result: it did not finish, so no test is known to have run. - return 0 + return None + return count if type(count) is int and count >= 0 else None def run_module(directory, module, verbose, timeout=None): @@ -183,6 +183,10 @@ def run_module(directory, module, verbose, timeout=None): _unregister_process(proc) count = read_count(count_path) out = stdout + stderr + if count is None: + if proc.returncode == 0: + return 1, 0, out + "\nTest worker exited without reporting a valid unittest result.\n" + return proc.returncode, 0, out # Discover imports every package initializer, but an empty one has no tests to run. It is included in the # module list so parallel runs see TestCase classes when there are any; without them, it is not a failure. if proc.returncode == 5 and count == 0 and module.endswith(".__init__") and "NO TESTS RAN" in stderr: diff --git a/tests/test_run_semantic_tests.py b/tests/test_run_semantic_tests.py index 7fc75e3722..df79c51147 100644 --- a/tests/test_run_semantic_tests.py +++ b/tests/test_run_semantic_tests.py @@ -64,6 +64,17 @@ def cli(*argv): class RunnerTest(unittest.TestCase): + def test_parallel_worker_without_a_result_fails_even_if_it_exits_zero(self): + early_exit = ("import os\nimport unittest\nclass T(unittest.TestCase):\n" + " def test_exits_before_reporting(self): os._exit(0)\n") + with tempfile.TemporaryDirectory() as tmp: + suite(tmp, {"helper.py": "X = 1\n", "test_early.py": early_exit, "test_pass.py": PASS}) + result = cli(tmp, "-j", "2") + self.assertEqual(result.returncode, 1, result.stdout + result.stderr) + self.assertIn("==== FAILED: test_early", result.stdout) + self.assertIn("Test worker exited without reporting a valid unittest result", result.stdout) + self.assertIn("2 files, 2 tests, 1 files failed", result.stdout) + def test_parallel_timeout_reports_partial_output_and_keeps_healthy_modules(self): with tempfile.TemporaryDirectory() as tmp: suite(tmp, {"helper.py": "X = 1\n", "test_hang.py": HANG,