From 1f353c38e01d1c47ca0f75427aad87f07f40c0c2 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Thu, 3 Dec 2026 14:13:56 -0800 Subject: [PATCH 1/4] test(cuda.core): keep non-OOM failures across a sticky-OOM restart Follow-up to #3695, addressing the remaining CodeRabbit findings. - A test whose call failed for a reason other than OOM and then hit an OOM in teardown was left unfinished so it reruns, but its real failure was dropped: a pass after the restart turned the run green. Such a test is now also listed in the .failed file. - run_pytest_with_restarts ignored a log-folding failure in an attempt that exited with the sticky-OOM code. A fold failure in any attempt now fails the run. Co-Authored-By: Claude Sonnet 5.5 --- ci/tools/run-tests | 11 ++++++++--- cuda_core/tests/conftest.py | 22 +++++++++++++++++----- cuda_core/tests/helpers/sticky_oom.py | 18 ++++++++++++++---- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/ci/tools/run-tests b/ci/tools/run-tests index 3ec87094863..bceff73d524 100755 --- a/ci/tools/run-tests +++ b/ci/tools/run-tests @@ -34,7 +34,7 @@ run_pytest_with_restarts() { local junit_base="${1}" shift local done_file="${TEST_RESULTS_DIR}/${junit_base}-finished.txt" - local attempt=0 junit rc + local attempt=0 junit rc fold_rc fold_failed=0 rm -f "${done_file}" "${done_file}.failed" while true; do junit="${TEST_RESULTS_DIR}/${junit_base}.xml" @@ -45,10 +45,15 @@ run_pytest_with_restarts() { CUDA_CORE_TEST_DONE_FILE="${done_file}" "$@" --junitxml "${junit}" | fold_pytest_log rc=${PIPESTATUS[0]} fold_rc=${PIPESTATUS[1]} set -e - if [[ ${fold_rc} -ne 0 && ${rc} -eq 0 ]]; then - rc=${fold_rc} + if [[ ${fold_rc} -ne 0 ]]; then + fold_failed=${fold_rc} fi if [[ ${rc} -ne ${STICKY_OOM_EXIT_CODE} ]]; then + # A fold failure in any attempt fails the run, as the old pipefail pipeline did. + if [[ ${rc} -eq 0 && ${fold_failed} -ne 0 ]]; then + echo "Folding the pytest log failed (exit ${fold_failed})" >&2 + return "${fold_failed}" + fi # Earlier attempts' failures are not rerun: they must still fail the run. if [[ ${attempt} -gt 0 && ${rc} -eq 0 && -s "${done_file}.failed" ]]; then echo "Tests failed in an earlier attempt (listed in ${done_file}.failed)" >&2 diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index f76ec4b8466..c993c04b30e 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -115,6 +115,10 @@ def pytest_terminal_summary(terminalreporter): # be a symptom of the stuck driver, so the test runs again after a restart; a # genuine OOM fails again there. _oom_nodeids = set() +# Tests whose current attempt had a failure that does not mention OOM. The stuck +# driver does not cause those, so they fail the run even if the test passes after +# a restart. +_non_oom_failed_nodeids = set() def _stop_if_driver_is_stuck_in_oom(item, call, report): @@ -130,6 +134,7 @@ def pytest_runtest_logstart(nodeid, location): # pytest-rerunfailures calls logstart and logfinish once per attempt. _rerun_nodeids.discard(nodeid) _oom_nodeids.discard(nodeid) + _non_oom_failed_nodeids.discard(nodeid) def pytest_runtest_logreport(report): @@ -141,14 +146,21 @@ def pytest_runtest_logreport(report): _failed_nodeids.add(report.nodeid) if sticky_oom.OOM_MARKER in report.longreprtext: _oom_nodeids.add(report.nodeid) + else: + _non_oom_failed_nodeids.add(report.nodeid) def pytest_runtest_logfinish(nodeid, location): - # An attempt that is going to be rerun has not finished the test, and neither - # has the one that exposed the stuck driver or one that failed with OOM - # before the stuck state was detected (e.g. in a teardown, which has no - # current context to probe): all must run again. - if nodeid in _rerun_nodeids or nodeid in _oom_nodeids or nodeid == _sticky_oom_nodeid: + # An attempt that is going to be rerun has not finished the test. + if nodeid in _rerun_nodeids: + return + # Neither has the one that exposed the stuck driver or one that failed with + # OOM before the stuck state was detected (e.g. in a teardown, which has no + # current context to probe): these run again. A failure that the stuck driver + # did not cause is kept, so that it fails the run even if the rerun passes. + if nodeid in _oom_nodeids or nodeid == _sticky_oom_nodeid: + if nodeid in _non_oom_failed_nodeids: + sticky_oom.record_failed(nodeid) return sticky_oom.record_finished(nodeid, failed=nodeid in _failed_nodeids) diff --git a/cuda_core/tests/helpers/sticky_oom.py b/cuda_core/tests/helpers/sticky_oom.py index 0979bcf179a..81d3394218c 100644 --- a/cuda_core/tests/helpers/sticky_oom.py +++ b/cuda_core/tests/helpers/sticky_oom.py @@ -69,18 +69,28 @@ def is_oom_failure(report, call): return OOM_MARKER in str(call.excinfo.value) +def record_failed(nodeid): + """List a failed test in ``.failed``, if CI asked for a resume file. + + ``ci/tools/run-tests`` fails the run if this file is not empty, even if the + last attempt passes. + """ + path = os.environ.get(DONE_FILE_ENV) + if path: + with open(path + FAILED_SUFFIX, "a", encoding="utf-8") as f: + f.write(nodeid + "\n") + + def record_finished(nodeid, failed=False): """Append a finished test to the resume file, if CI asked for one. - A failed test is also listed in ``.failed``, so that - ``ci/tools/run-tests`` can fail the run even if the last attempt passes. + A failed test is also listed in the ``.failed`` file (see :func:`record_failed`). """ path = os.environ.get(DONE_FILE_ENV) if path: # The failure first: if this raises, the test is not finished and reruns. if failed: - with open(path + FAILED_SUFFIX, "a", encoding="utf-8") as f: - f.write(nodeid + "\n") + record_failed(nodeid) with open(path, "a", encoding="utf-8") as f: f.write(nodeid + "\n") From 2fb78272c4a68ffcf32b107e7d4f837220bed633 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Thu, 3 Dec 2026 14:26:57 -0800 Subject: [PATCH 2/4] test(cuda.core): classify sticky OOM from exceptions Signed-off-by: Ralf Juengling --- cuda_core/tests/conftest.py | 14 ++++-- cuda_core/tests/helpers/sticky_oom.py | 13 ++++-- cuda_core/tests/test_sticky_oom.py | 67 +++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 cuda_core/tests/test_sticky_oom.py diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index c993c04b30e..ce81bb02d97 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -87,7 +87,11 @@ def pytest_runtest_makereport(item, call): from helpers import oom_diagnostics oom_diagnostics.record_if_oom(item, call, report) - _stop_if_driver_is_stuck_in_oom(item, call, report) + # Keep the classification on the report so logreport can use the actual + # exception while still honoring the final outcome from pytest-rerunfailures. + report.cuda_core_oom_failure = sticky_oom.is_oom_failure(report, call) + if call.when in ("setup", "call") and report.cuda_core_oom_failure: + _stop_if_driver_is_stuck_in_oom(item) return report @@ -115,17 +119,17 @@ def pytest_terminal_summary(terminalreporter): # be a symptom of the stuck driver, so the test runs again after a restart; a # genuine OOM fails again there. _oom_nodeids = set() -# Tests whose current attempt had a failure that does not mention OOM. The stuck +# Tests whose current attempt had a non-OOM failure. The stuck # driver does not cause those, so they fail the run even if the test passes after # a restart. _non_oom_failed_nodeids = set() -def _stop_if_driver_is_stuck_in_oom(item, call, report): +def _stop_if_driver_is_stuck_in_oom(item): global _sticky_oom_nodeid if _sticky_oom_nodeid is not None or not sticky_oom.is_affected_platform(): return - if sticky_oom.is_oom_failure(report, call) and sticky_oom.process_is_stuck_in_oom(): + if sticky_oom.process_is_stuck_in_oom(): _sticky_oom_nodeid = item.nodeid item.session.shouldstop = f"driver stuck in CUDA_ERROR_OUT_OF_MEMORY (NVBugs 7092346) at {item.nodeid}" @@ -144,7 +148,7 @@ def pytest_runtest_logreport(report): _rerun_nodeids.add(report.nodeid) elif report.failed: _failed_nodeids.add(report.nodeid) - if sticky_oom.OOM_MARKER in report.longreprtext: + if getattr(report, "cuda_core_oom_failure", False): _oom_nodeids.add(report.nodeid) else: _non_oom_failed_nodeids.add(report.nodeid) diff --git a/cuda_core/tests/helpers/sticky_oom.py b/cuda_core/tests/helpers/sticky_oom.py index 81d3394218c..90814a9b2f5 100644 --- a/cuda_core/tests/helpers/sticky_oom.py +++ b/cuda_core/tests/helpers/sticky_oom.py @@ -62,11 +62,16 @@ def process_is_stuck_in_oom(): return sync_err == _OOM and mem_err == _OOM +def _exception_is_oom(exc): + # Pytest can group multiple teardown errors. Retry only if all of them are OOMs. + if isinstance(exc, BaseExceptionGroup): + return all(_exception_is_oom(child) for child in exc.exceptions) + return OOM_MARKER in str(exc) + + def is_oom_failure(report, call): - """True if ``report`` is a failed setup or call phase whose exception mentions OOM.""" - if not report.failed or call.excinfo is None or call.when not in ("setup", "call"): - return False - return OOM_MARKER in str(call.excinfo.value) + """Classify a failed phase from its exception, not its rendered traceback.""" + return report.failed and call.excinfo is not None and _exception_is_oom(call.excinfo.value) def record_failed(nodeid): diff --git a/cuda_core/tests/test_sticky_oom.py b/cuda_core/tests/test_sticky_oom.py new file mode 100644 index 00000000000..d8eb20baa30 --- /dev/null +++ b/cuda_core/tests/test_sticky_oom.py @@ -0,0 +1,67 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +import os +import pathlib +import subprocess +import sys + +import pytest +from helpers import sticky_oom + + +@pytest.mark.parametrize("non_oom_failure", [True, False], ids=["mixed", "oom-only"]) +@pytest.mark.agent_authored(model="gpt-6.2-sol") +def test_sticky_oom_keeps_non_oom_failure(tmp_path, non_oom_failure): + # Run the actual hooks in a separate session with no live driver probes. + test_file = tmp_path / "test_mixed.py" + test_file.write_text( + f"""\ +import pytest +from helpers import oom_diagnostics, sticky_oom + +sticky_oom.IS_WINDOWS = True +sticky_oom.process_is_stuck_in_oom = lambda: True +oom_diagnostics.record_if_oom = lambda *args: None + +@pytest.fixture +def cleanup(): + yield + if not {non_oom_failure!r}: + raise ExceptionGroup("teardown", [RuntimeError("CUDA_ERROR_OUT_OF_MEMORY")]) + raise RuntimeError("CUDA_ERROR_OUT_OF_MEMORY") + +def test_mixed(cleanup): + if {non_oom_failure!r}: + raise AssertionError("real failure") # Not CUDA_ERROR_OUT_OF_MEMORY + +def test_trigger(): + raise RuntimeError("CUDA_ERROR_OUT_OF_MEMORY") +""", + encoding="utf-8", + ) + done_file = tmp_path / "finished.txt" + failed_file = tmp_path / f"{done_file.name}{sticky_oom.FAILED_SUFFIX}" + tests_dir = pathlib.Path(__file__).parent + env = { + **os.environ, + sticky_oom.DONE_FILE_ENV: str(done_file), + "PYTEST_ADDOPTS": "", + "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", + "PYTHONPATH": os.pathsep.join(filter(None, (str(tests_dir), os.environ.get("PYTHONPATH")))), + } + result = subprocess.run( # noqa: S603 - trusted interpreter and test-owned arguments + [sys.executable, "-m", "pytest", "--noconftest", "-p", "conftest", "-q", test_file.name], + capture_output=True, + cwd=tmp_path, + env=env, + text=True, + timeout=30, + ) + assert result.returncode == sticky_oom.STICKY_OOM_EXIT_CODE, result.stdout + result.stderr + assert not done_file.exists() + if non_oom_failure: + assert failed_file.is_file(), result.stdout + assert failed_file.read_text(encoding="utf-8").splitlines() == ["test_mixed.py::test_mixed"] + else: + assert not failed_file.exists() From 3749c99cb56b548fb1e07b39cef1dc7d6409dd7e Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Thu, 3 Dec 2026 14:29:29 -0800 Subject: [PATCH 3/4] test(cuda.core): clarify sticky OOM checks and regressions Signed-off-by: Ralf Juengling --- cuda_core/tests/conftest.py | 5 +++-- cuda_core/tests/test_sticky_oom.py | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index ce81bb02d97..1be0c09f830 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -89,8 +89,9 @@ def pytest_runtest_makereport(item, call): oom_diagnostics.record_if_oom(item, call, report) # Keep the classification on the report so logreport can use the actual # exception while still honoring the final outcome from pytest-rerunfailures. - report.cuda_core_oom_failure = sticky_oom.is_oom_failure(report, call) - if call.when in ("setup", "call") and report.cuda_core_oom_failure: + is_oom = sticky_oom.is_oom_failure(report, call) + report.cuda_core_oom_failure = is_oom + if is_oom and report.when in ("setup", "call"): _stop_if_driver_is_stuck_in_oom(item) return report diff --git a/cuda_core/tests/test_sticky_oom.py b/cuda_core/tests/test_sticky_oom.py index d8eb20baa30..60ec762e9bb 100644 --- a/cuda_core/tests/test_sticky_oom.py +++ b/cuda_core/tests/test_sticky_oom.py @@ -5,6 +5,7 @@ import pathlib import subprocess import sys +from types import SimpleNamespace import pytest from helpers import sticky_oom @@ -27,8 +28,6 @@ def test_sticky_oom_keeps_non_oom_failure(tmp_path, non_oom_failure): @pytest.fixture def cleanup(): yield - if not {non_oom_failure!r}: - raise ExceptionGroup("teardown", [RuntimeError("CUDA_ERROR_OUT_OF_MEMORY")]) raise RuntimeError("CUDA_ERROR_OUT_OF_MEMORY") def test_mixed(cleanup): @@ -65,3 +64,18 @@ def test_trigger(): assert failed_file.read_text(encoding="utf-8").splitlines() == ["test_mixed.py::test_mixed"] else: assert not failed_file.exists() + + +@pytest.mark.parametrize( + ("second_error", "is_oom"), + [(sticky_oom.OOM_MARKER, True), ("unrelated failure", False)], + ids=["oom-only", "mixed"], +) +@pytest.mark.agent_authored(model="gpt-6.2-sol") +def test_sticky_oom_classifies_grouped_failures(second_error, is_oom): + with pytest.raises(ExceptionGroup) as excinfo: + raise ExceptionGroup("teardown", [RuntimeError(sticky_oom.OOM_MARKER), RuntimeError(second_error)]) + + report = SimpleNamespace(failed=True) + call = SimpleNamespace(excinfo=excinfo) + assert sticky_oom.is_oom_failure(report, call) is is_oom From ae847a379b9070be1fbc7a133391f28432e07e9e Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Thu, 3 Dec 2026 14:55:54 -0800 Subject: [PATCH 4/4] test(cuda.core): retry all failures after confirmed sticky OOM Treat non-OOM failures from a poisoned driver process as provisional. Checkpoint only completed non-failing tests and let failures that reproduce in a fresh process determine the CI result. Signed-off-by: Ralf Juengling --- ci/tools/run-tests | 12 ++--- cuda_core/tests/conftest.py | 48 ++++------------- cuda_core/tests/helpers/sticky_oom.py | 28 ++-------- cuda_core/tests/test_sticky_oom.py | 76 ++++++++++++++++----------- 4 files changed, 63 insertions(+), 101 deletions(-) diff --git a/ci/tools/run-tests b/ci/tools/run-tests index bceff73d524..a4e9145adea 100755 --- a/ci/tools/run-tests +++ b/ci/tools/run-tests @@ -28,14 +28,14 @@ STICKY_OOM_EXIT_CODE=75 MAX_STICKY_OOM_RESTARTS=3 # Usage: run_pytest_with_restarts -# Each attempt writes its own JUnit file; a restart skips tests earlier attempts finished. -# Any other pytest exit code is returned unchanged. +# Each attempt writes its own JUnit file; a restart skips tests completed without failure. +# Other pytest exit codes are returned unchanged unless pytest passed and log folding failed. run_pytest_with_restarts() { local junit_base="${1}" shift local done_file="${TEST_RESULTS_DIR}/${junit_base}-finished.txt" local attempt=0 junit rc fold_rc fold_failed=0 - rm -f "${done_file}" "${done_file}.failed" + rm -f "${done_file}" while true; do junit="${TEST_RESULTS_DIR}/${junit_base}.xml" if [[ ${attempt} -gt 0 ]]; then @@ -54,12 +54,6 @@ run_pytest_with_restarts() { echo "Folding the pytest log failed (exit ${fold_failed})" >&2 return "${fold_failed}" fi - # Earlier attempts' failures are not rerun: they must still fail the run. - if [[ ${attempt} -gt 0 && ${rc} -eq 0 && -s "${done_file}.failed" ]]; then - echo "Tests failed in an earlier attempt (listed in ${done_file}.failed)" >&2 - cat "${done_file}.failed" >&2 - return 1 - fi return "${rc}" fi attempt=$((attempt + 1)) diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index 1be0c09f830..3d04a605e91 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -87,11 +87,7 @@ def pytest_runtest_makereport(item, call): from helpers import oom_diagnostics oom_diagnostics.record_if_oom(item, call, report) - # Keep the classification on the report so logreport can use the actual - # exception while still honoring the final outcome from pytest-rerunfailures. - is_oom = sticky_oom.is_oom_failure(report, call) - report.cuda_core_oom_failure = is_oom - if is_oom and report.when in ("setup", "call"): + if report.when in ("setup", "call") and sticky_oom.is_oom_failure(report, call): _stop_if_driver_is_stuck_in_oom(item) return report @@ -114,16 +110,7 @@ def pytest_terminal_summary(terminalreporter): # dedicated code; ci/tools/run-tests restarts and skips the finished tests. # See helpers/sticky_oom.py. _sticky_oom_nodeid = None -_failed_nodeids = set() -_rerun_nodeids = set() -# Tests whose current attempt failed with OOM, in any phase. Such a failure may -# be a symptom of the stuck driver, so the test runs again after a restart; a -# genuine OOM fails again there. -_oom_nodeids = set() -# Tests whose current attempt had a non-OOM failure. The stuck -# driver does not cause those, so they fail the run even if the test passes after -# a restart. -_non_oom_failed_nodeids = set() +_unfinished_nodeids = set() def _stop_if_driver_is_stuck_in_oom(item): @@ -137,37 +124,22 @@ def _stop_if_driver_is_stuck_in_oom(item): def pytest_runtest_logstart(nodeid, location): # pytest-rerunfailures calls logstart and logfinish once per attempt. - _rerun_nodeids.discard(nodeid) - _oom_nodeids.discard(nodeid) - _non_oom_failed_nodeids.discard(nodeid) + _unfinished_nodeids.discard(nodeid) def pytest_runtest_logreport(report): # Logged outcomes, not makereport's: pytest-rerunfailures relabels a failed # attempt that it will rerun as "rerun" in between. - if report.outcome == "rerun": - _rerun_nodeids.add(report.nodeid) - elif report.failed: - _failed_nodeids.add(report.nodeid) - if getattr(report, "cuda_core_oom_failure", False): - _oom_nodeids.add(report.nodeid) - else: - _non_oom_failed_nodeids.add(report.nodeid) + if report.failed or report.outcome == "rerun": + _unfinished_nodeids.add(report.nodeid) def pytest_runtest_logfinish(nodeid, location): - # An attempt that is going to be rerun has not finished the test. - if nodeid in _rerun_nodeids: - return - # Neither has the one that exposed the stuck driver or one that failed with - # OOM before the stuck state was detected (e.g. in a teardown, which has no - # current context to probe): these run again. A failure that the stuck driver - # did not cause is kept, so that it fails the run even if the rerun passes. - if nodeid in _oom_nodeids or nodeid == _sticky_oom_nodeid: - if nodeid in _non_oom_failed_nodeids: - sticky_oom.record_failed(nodeid) - return - sticky_oom.record_finished(nodeid, failed=nodeid in _failed_nodeids) + # The stuck driver may cause non-OOM failures before detection. Leave all + # failures for the fresh process, where deterministic failures recur. + # The trigger must also rerun even if an in-process rerun passed. + if nodeid != _sticky_oom_nodeid and nodeid not in _unfinished_nodeids: + sticky_oom.record_finished(nodeid) def pytest_collection_modifyitems(config, items): diff --git a/cuda_core/tests/helpers/sticky_oom.py b/cuda_core/tests/helpers/sticky_oom.py index 90814a9b2f5..c7efcf770c4 100644 --- a/cuda_core/tests/helpers/sticky_oom.py +++ b/cuda_core/tests/helpers/sticky_oom.py @@ -12,7 +12,7 @@ This module detects that state after a failing test and makes pytest stop with :data:`STICKY_OOM_EXIT_CODE`. ``ci/tools/run-tests`` then starts a fresh process -that skips the tests which already finished. Both halves are inert outside +that skips tests already completed without failure. Both halves are inert outside Windows, and in a process that is healthy the probe returns False, so a genuine out-of-memory failure still fails the run. @@ -31,8 +31,6 @@ # Not a pytest exit code (0-5) and not a signal. Mirrored in ci/tools/run-tests. STICKY_OOM_EXIT_CODE = 75 DONE_FILE_ENV = "CUDA_CORE_TEST_DONE_FILE" -# Mirrored in ci/tools/run-tests. -FAILED_SUFFIX = ".failed" OOM_MARKER = "CUDA_ERROR_OUT_OF_MEMORY" _OOM = driver.CUresult.CUDA_ERROR_OUT_OF_MEMORY @@ -63,7 +61,7 @@ def process_is_stuck_in_oom(): def _exception_is_oom(exc): - # Pytest can group multiple teardown errors. Retry only if all of them are OOMs. + # For a group of errors, classify the phase as OOM only if all are OOMs. if isinstance(exc, BaseExceptionGroup): return all(_exception_is_oom(child) for child in exc.exceptions) return OOM_MARKER in str(exc) @@ -74,28 +72,10 @@ def is_oom_failure(report, call): return report.failed and call.excinfo is not None and _exception_is_oom(call.excinfo.value) -def record_failed(nodeid): - """List a failed test in ``.failed``, if CI asked for a resume file. - - ``ci/tools/run-tests`` fails the run if this file is not empty, even if the - last attempt passes. - """ - path = os.environ.get(DONE_FILE_ENV) - if path: - with open(path + FAILED_SUFFIX, "a", encoding="utf-8") as f: - f.write(nodeid + "\n") - - -def record_finished(nodeid, failed=False): - """Append a finished test to the resume file, if CI asked for one. - - A failed test is also listed in the ``.failed`` file (see :func:`record_failed`). - """ +def record_finished(nodeid): + """Record a test completed without failure, if CI asked for a resume file.""" path = os.environ.get(DONE_FILE_ENV) if path: - # The failure first: if this raises, the test is not finished and reruns. - if failed: - record_failed(nodeid) with open(path, "a", encoding="utf-8") as f: f.write(nodeid + "\n") diff --git a/cuda_core/tests/test_sticky_oom.py b/cuda_core/tests/test_sticky_oom.py index 60ec762e9bb..8a1d7e3b939 100644 --- a/cuda_core/tests/test_sticky_oom.py +++ b/cuda_core/tests/test_sticky_oom.py @@ -11,36 +11,40 @@ from helpers import sticky_oom -@pytest.mark.parametrize("non_oom_failure", [True, False], ids=["mixed", "oom-only"]) +@pytest.mark.parametrize( + ("probe_confirms_sticky_oom", "failure_repeats"), + [(True, False), (True, True), (False, False)], + ids=["clears", "repeats", "no-restart"], +) @pytest.mark.agent_authored(model="gpt-6.2-sol") -def test_sticky_oom_keeps_non_oom_failure(tmp_path, non_oom_failure): - # Run the actual hooks in a separate session with no live driver probes. - test_file = tmp_path / "test_mixed.py" +def test_sticky_oom_retries_non_oom_failure(tmp_path, probe_confirms_sticky_oom, failure_repeats): + # Run the actual hooks in separate processes with no live driver probes. + test_file = tmp_path / "test_retry.py" test_file.write_text( f"""\ -import pytest +import os + from helpers import oom_diagnostics, sticky_oom +restarting = os.environ["STICKY_OOM_TEST_RESTART"] == "1" sticky_oom.IS_WINDOWS = True -sticky_oom.process_is_stuck_in_oom = lambda: True +sticky_oom.process_is_stuck_in_oom = lambda: {probe_confirms_sticky_oom!r} oom_diagnostics.record_if_oom = lambda *args: None -@pytest.fixture -def cleanup(): - yield - raise RuntimeError("CUDA_ERROR_OUT_OF_MEMORY") +def test_already_passed(): + assert not restarting, "a completed test should be skipped" -def test_mixed(cleanup): - if {non_oom_failure!r}: - raise AssertionError("real failure") # Not CUDA_ERROR_OUT_OF_MEMORY +def test_non_oom(): + if not restarting or {failure_repeats!r}: + raise AssertionError("non-OOM failure") # Not CUDA_ERROR_OUT_OF_MEMORY def test_trigger(): - raise RuntimeError("CUDA_ERROR_OUT_OF_MEMORY") + if not restarting: + raise RuntimeError("CUDA_ERROR_OUT_OF_MEMORY: restart trigger") """, encoding="utf-8", ) done_file = tmp_path / "finished.txt" - failed_file = tmp_path / f"{done_file.name}{sticky_oom.FAILED_SUFFIX}" tests_dir = pathlib.Path(__file__).parent env = { **os.environ, @@ -49,21 +53,33 @@ def test_trigger(): "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTHONPATH": os.pathsep.join(filter(None, (str(tests_dir), os.environ.get("PYTHONPATH")))), } - result = subprocess.run( # noqa: S603 - trusted interpreter and test-owned arguments - [sys.executable, "-m", "pytest", "--noconftest", "-p", "conftest", "-q", test_file.name], - capture_output=True, - cwd=tmp_path, - env=env, - text=True, - timeout=30, - ) - assert result.returncode == sticky_oom.STICKY_OOM_EXIT_CODE, result.stdout + result.stderr - assert not done_file.exists() - if non_oom_failure: - assert failed_file.is_file(), result.stdout - assert failed_file.read_text(encoding="utf-8").splitlines() == ["test_mixed.py::test_mixed"] - else: - assert not failed_file.exists() + + def run_pytest(restart): + return subprocess.run( # noqa: S603 - trusted interpreter and test-owned arguments + [sys.executable, "-m", "pytest", "--noconftest", "-p", "conftest", "-q", test_file.name], + capture_output=True, + cwd=tmp_path, + env={**env, "STICKY_OOM_TEST_RESTART": str(int(restart))}, + text=True, + timeout=30, + ) + + first = run_pytest(restart=False) + expected = sticky_oom.STICKY_OOM_EXIT_CODE if probe_confirms_sticky_oom else pytest.ExitCode.TESTS_FAILED + assert first.returncode == expected, first.stdout + first.stderr + assert "non-OOM failure" in first.stdout + assert "CUDA_ERROR_OUT_OF_MEMORY: restart trigger" in first.stdout + assert done_file.read_text(encoding="utf-8").splitlines() == ["test_retry.py::test_already_passed"] + + if probe_confirms_sticky_oom: + second = run_pytest(restart=True) + expected = pytest.ExitCode.TESTS_FAILED if failure_repeats else pytest.ExitCode.OK + assert second.returncode == expected, second.stdout + second.stderr + assert ("non-OOM failure" in second.stdout) is failure_repeats + finished = ["test_retry.py::test_already_passed", "test_retry.py::test_trigger"] + if not failure_repeats: + finished.append("test_retry.py::test_non_oom") + assert sorted(done_file.read_text(encoding="utf-8").splitlines()) == sorted(finished) @pytest.mark.parametrize(