From 6592976061a6580fee2ade3564f6497eb685ab67 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 5 Oct 2023 08:24:43 +0200 Subject: [PATCH] gh-110367: Enhance regrtest -jN --verbose3 (#110368) When using worker processes (-jN) with --verbose3 option, regrtest can now display the worker output even if a worker process does crash. Previously, sys.stdout and sys.stderr were replaced and so the worker output was lost on a crash. --- Lib/test/libregrtest/run_workers.py | 15 ++++++-- Lib/test/test_faulthandler.py | 2 +- Lib/test/test_regrtest.py | 34 +++++++++++++++++++ ...-10-04-18-27-47.gh-issue-110367.Nnq1I7.rst | 4 +++ 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2023-10-04-18-27-47.gh-issue-110367.Nnq1I7.rst diff --git a/Lib/test/libregrtest/run_workers.py b/Lib/test/libregrtest/run_workers.py index 6eb32e59635..106f9730832 100644 --- a/Lib/test/libregrtest/run_workers.py +++ b/Lib/test/libregrtest/run_workers.py @@ -262,6 +262,9 @@ def create_worker_runtests(self, test_name: TestName, json_file: JsonFile) -> Ru kwargs = {} if match_tests: kwargs['match_tests'] = match_tests + if self.runtests.output_on_failure: + kwargs['verbose'] = True + kwargs['output_on_failure'] = False return self.runtests.copy( tests=tests, json_file=json_file, @@ -562,8 +565,16 @@ def _process_result(self, item: QueueOutput) -> TestResult: self.results.accumulate_result(result, self.runtests) self.display_result(mp_result) - if mp_result.worker_stdout: - print(mp_result.worker_stdout, flush=True) + # Display worker stdout + if not self.runtests.output_on_failure: + show_stdout = True + else: + # --verbose3 ignores stdout on success + show_stdout = (result.state != State.PASSED) + if show_stdout: + stdout = mp_result.worker_stdout + if stdout: + print(stdout, flush=True) return result diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index 3c1e8c150ae..4dce7a7dd8c 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -35,7 +35,7 @@ def expected_traceback(lineno1, lineno2, header, min_count=1): return '^' + regex + '$' def skip_segfault_on_android(test): - # Issue #32138: Raising SIGSEGV on Android may not cause a crash. + # gh-76319: Raising SIGSEGV on Android may not cause a crash. return unittest.skipIf(is_android, 'raising SIGSEGV on Android is unreliable')(test) diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index f24d23e5c2f..66463fd2bbe 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -42,6 +42,8 @@ EXITCODE_RERUN_FAIL = 5 EXITCODE_INTERRUPTED = 130 +MS_WINDOWS = (sys.platform == 'win32') + TEST_INTERRUPTED = textwrap.dedent(""" from signal import SIGINT, raise_signal try: @@ -2036,6 +2038,38 @@ def test_add_python_opts(self): with self.subTest(opt=opt): self.check_add_python_opts(opt) + # gh-76319: Raising SIGSEGV on Android may not cause a crash. + @unittest.skipIf(support.is_android, + 'raising SIGSEGV on Android is unreliable') + def test_worker_output_on_failure(self): + try: + from faulthandler import _sigsegv + except ImportError: + self.skipTest("need faulthandler._sigsegv") + + code = textwrap.dedent(r""" + import faulthandler + import unittest + from test import support + + class CrashTests(unittest.TestCase): + def test_crash(self): + print("just before crash!", flush=True) + + with support.SuppressCrashReport(): + faulthandler._sigsegv(True) + """) + testname = self.create_test(code=code) + + output = self.run_tests("-j1", testname, exitcode=EXITCODE_BAD_TEST) + self.check_executed_tests(output, testname, + failed=[testname], + stats=0, parallel=True) + if not MS_WINDOWS: + exitcode = -int(signal.SIGSEGV) + self.assertIn(f"Exit code {exitcode} (SIGSEGV)", output) + self.check_line(output, "just before crash!", full=True, regex=False) + class TestUtils(unittest.TestCase): def test_format_duration(self): diff --git a/Misc/NEWS.d/next/Tests/2023-10-04-18-27-47.gh-issue-110367.Nnq1I7.rst b/Misc/NEWS.d/next/Tests/2023-10-04-18-27-47.gh-issue-110367.Nnq1I7.rst new file mode 100644 index 00000000000..a1a6a09da50 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-10-04-18-27-47.gh-issue-110367.Nnq1I7.rst @@ -0,0 +1,4 @@ +regrtest: When using worker processes (-jN) with --verbose3 option, regrtest +can now display the worker output even if a worker process does crash. +Previously, sys.stdout and sys.stderr were replaced and so the worker output +was lost on a crash. Patch by Victor Stinner.