From 18ca962df69bba1a4e50005d23ed80e6458eb045 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Wed, 2 Dec 2026 17:41:29 -0800 Subject: [PATCH 1/7] test(cuda.core): stop the run when a Windows driver is stuck in OOM On Windows (WDDM/MCDM), a host pinned pool with device access granted can leave the process permanently returning CUDA_ERROR_OUT_OF_MEMORY (NVBugs 7092346, #3472). The test that trips it is arbitrary, so xfail would hide real failures and blame the wrong tests. After a failing setup/call phase that mentions OOM, probe cuCtxSynchronize and cuMemGetInfo; if both return OOM the process is stuck. Stop the session with exit code 75, record finished tests in a resume file, and skip them on a restart. Inert outside Windows; a genuine OOM leaves the probe False. Co-Authored-By: Claude Sonnet 5.5 --- cuda_core/tests/conftest.py | 45 ++++++++++++++ cuda_core/tests/helpers/sticky_oom.py | 84 +++++++++++++++++++++++++++ cuda_core/tests/test_sticky_oom.py | 49 ++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 cuda_core/tests/helpers/sticky_oom.py 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 9fadfcbe5a9..2ee8cd260f6 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -34,6 +34,7 @@ pytest_plugins = ["cuda_python_test_helpers._pytest_plugin"] # END SYNCED PYTEST PLUGIN BOOTSTRAP +from helpers import sticky_oom from helpers.constants import POOL_SIZE from helpers.memory import ( MEMPOOL_RESOURCE_CLASSES, @@ -86,6 +87,7 @@ 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) return report @@ -93,6 +95,49 @@ def pytest_terminal_summary(terminalreporter): from helpers import oom_diagnostics oom_diagnostics.report_terminal_summary(terminalreporter) + if _sticky_oom_nodeid is not None: + terminalreporter.write_line("") + terminalreporter.write_line( + f"Driver stuck in CUDA_ERROR_OUT_OF_MEMORY after {_sticky_oom_nodeid} (NVBugs 7092346, " + f"#3472); stopped with exit code {sticky_oom.STICKY_OOM_EXIT_CODE} so CI can resume " + "in a fresh process." + ) + + +# NVBugs 7092346: on Windows a host pinned pool can leave the process stuck in +# OOM. Stop the run at the first failing test that shows it and exit with a +# dedicated code; ci/tools/run-tests restarts and skips the finished tests. +# See helpers/sticky_oom.py. +_sticky_oom_nodeid = None + + +def _stop_if_driver_is_stuck_in_oom(item, call, report): + 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(): + _sticky_oom_nodeid = item.nodeid + item.session.shouldstop = f"driver stuck in CUDA_ERROR_OUT_OF_MEMORY (NVBugs 7092346) at {item.nodeid}" + + +def pytest_runtest_logfinish(nodeid, location): + # The test that exposed the stuck driver is not finished: it reruns. + if nodeid != _sticky_oom_nodeid: + sticky_oom.record_finished(nodeid) + + +def pytest_collection_modifyitems(config, items): + finished = sticky_oom.finished_nodeids() + if not finished: + return + kept = [item for item in items if item.nodeid not in finished] + config.hook.pytest_deselected(items=[item for item in items if item.nodeid in finished]) + items[:] = kept + + +def pytest_sessionfinish(session, exitstatus): + if _sticky_oom_nodeid is not None: + session.exitstatus = sticky_oom.STICKY_OOM_EXIT_CODE def _wrap_worker_cuda_test(func): diff --git a/cuda_core/tests/helpers/sticky_oom.py b/cuda_core/tests/helpers/sticky_oom.py new file mode 100644 index 00000000000..8a52c243bf8 --- /dev/null +++ b/cuda_core/tests/helpers/sticky_oom.py @@ -0,0 +1,84 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Stop the run, and let CI resume it, when the driver is stuck in OOM (NVBugs 7092346). + +On Windows (WDDM and MCDM), a host pinned memory pool that has had device +access granted can leave the process in a permanent error state: later driver +calls return ``CUDA_ERROR_OUT_OF_MEMORY`` even though no resource is exhausted, +and ``cuCtxSynchronize`` and ``cuMemGetInfo`` fail the same way. The state is +per process and the test that trips it is arbitrary, so marking tests xfail +would hide real failures and name the wrong tests (issue #3472). + +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 +Windows, and in a process that is healthy the probe returns False, so a +genuine out-of-memory failure still fails the run. + +Remove this module and its wiring once the fixed driver is the minimum CI +driver. +""" + +import os +import pathlib + +from cuda.bindings import driver +from cuda_python_test_helpers import IS_WINDOWS + +# 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" +OOM_MARKER = "CUDA_ERROR_OUT_OF_MEMORY" + +_OOM = driver.CUresult.CUDA_ERROR_OUT_OF_MEMORY + + +def is_affected_platform(): + """True where NVBugs 7092346 can occur. + + Linux is unaffected. TCC is unaffected too, but a healthy process never + passes :func:`process_is_stuck_in_oom`, so no driver-model check is needed. + """ + return IS_WINDOWS + + +def process_is_stuck_in_oom(): + """True if context-wide calls return OOM, which a genuine allocation failure never does. + + Needs a current context; returns False without one, so the next failing + test (the cascade produces many) gets another chance. Never raises and + never changes the current context. + """ + try: + err, ctx = driver.cuCtxGetCurrent() + if err != driver.CUresult.CUDA_SUCCESS or ctx is None or int(ctx) == 0: + return False + (sync_err,) = driver.cuCtxSynchronize() + mem_err = driver.cuMemGetInfo()[0] + except Exception: + return False + return sync_err == _OOM and mem_err == _OOM + + +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) + + +def record_finished(nodeid): + """Append a finished test to the resume file, if CI asked for one.""" + path = os.environ.get(DONE_FILE_ENV) + if path: + with open(path, "a", encoding="utf-8") as f: + f.write(nodeid + "\n") + + +def finished_nodeids(): + """Node IDs that earlier attempts of this run finished; empty on a first attempt.""" + path = os.environ.get(DONE_FILE_ENV) + if not path or not pathlib.Path(path).is_file(): + return set() + return {line.strip() for line in pathlib.Path(path).read_text(encoding="utf-8").splitlines() if line.strip()} diff --git a/cuda_core/tests/test_sticky_oom.py b/cuda_core/tests/test_sticky_oom.py new file mode 100644 index 00000000000..b10ffebf5bb --- /dev/null +++ b/cuda_core/tests/test_sticky_oom.py @@ -0,0 +1,49 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +from types import SimpleNamespace + +import pytest +from helpers import sticky_oom + + +@pytest.mark.agent_authored(model="claude-sonnet-5-5") +def test_healthy_process_is_not_stuck(init_cuda): + # A healthy driver, including one that has just reported a genuine + # allocation failure, must never be classified as stuck. + assert not sticky_oom.process_is_stuck_in_oom() + + +@pytest.mark.agent_authored(model="claude-sonnet-5-5") +def test_no_current_context_is_not_stuck(init_cuda): + from helpers.contexts import no_current_context + + with no_current_context(): + assert not sticky_oom.process_is_stuck_in_oom() + + +@pytest.mark.agent_authored(model="claude-sonnet-5-5") +@pytest.mark.parametrize( + ("failed", "when", "message", "expected"), + [ + (True, "call", "CUDA_ERROR_OUT_OF_MEMORY: x", True), + (True, "setup", "CUDA_ERROR_OUT_OF_MEMORY: x", True), + (True, "teardown", "CUDA_ERROR_OUT_OF_MEMORY: x", False), + (True, "call", "AssertionError: other", False), + (False, "call", "CUDA_ERROR_OUT_OF_MEMORY: x", False), + ], +) +def test_is_oom_failure(failed, when, message, expected): + report = SimpleNamespace(failed=failed) + call = SimpleNamespace(when=when, excinfo=SimpleNamespace(value=RuntimeError(message))) + assert sticky_oom.is_oom_failure(report, call) is expected + + +@pytest.mark.agent_authored(model="claude-sonnet-5-5") +def test_finished_nodeids_round_trip(tmp_path, monkeypatch): + done_file = tmp_path / "finished.txt" + monkeypatch.setenv(sticky_oom.DONE_FILE_ENV, str(done_file)) + assert sticky_oom.finished_nodeids() == set() + sticky_oom.record_finished("tests/test_a.py::test_one") + sticky_oom.record_finished("tests/test_a.py::test_two[x]") + assert sticky_oom.finished_nodeids() == {"tests/test_a.py::test_one", "tests/test_a.py::test_two[x]"} From 7699eb20bf1f1a2a8a03085c14fc1705c7139c05 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Wed, 2 Dec 2026 17:41:44 -0800 Subject: [PATCH 2/7] ci: restart the core tests in a fresh process when the driver is stuck in OOM Wrap the cuda.core pytest run so exit code 75 (stuck driver, NVBugs 7092346) reruns the unfinished tests in a new process, up to 3 times, with one JUnit file per attempt. Every other exit code passes through unchanged. Co-Authored-By: Claude Sonnet 5.5 --- ci/tools/run-tests | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/ci/tools/run-tests b/ci/tools/run-tests index 2b7b0c9d57e..77883ffc464 100755 --- a/ci/tools/run-tests +++ b/ci/tools/run-tests @@ -20,6 +20,43 @@ fold_pytest_log() { python "${CI_TOOLS_DIR}/fold_pytest_sections.py" } +# Windows drivers can get stuck in CUDA_ERROR_OUT_OF_MEMORY for the rest of a process +# (NVBugs 7092346, NVIDIA/cuda-python#3472). cuda_core/tests/conftest.py then stops pytest +# with this exit code; run_pytest_with_restarts reruns the unfinished tests in a fresh +# process. Mirrors STICKY_OOM_EXIT_CODE in cuda_core/tests/helpers/sticky_oom.py. +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. +run_pytest_with_restarts() { + local junit_base="${1}" + shift + local done_file="${TEST_RESULTS_DIR}/${junit_base}-finished.txt" + local attempt=0 junit rc + rm -f "${done_file}" + while true; do + junit="${TEST_RESULTS_DIR}/${junit_base}.xml" + if [[ ${attempt} -gt 0 ]]; then + junit="${TEST_RESULTS_DIR}/${junit_base}-restart${attempt}.xml" + fi + set +e + CUDA_CORE_TEST_DONE_FILE="${done_file}" "$@" --junitxml "${junit}" | fold_pytest_log + rc=${PIPESTATUS[0]} + set -e + if [[ ${rc} -ne ${STICKY_OOM_EXIT_CODE} ]]; then + return "${rc}" + fi + attempt=$((attempt + 1)) + if [[ ${attempt} -gt ${MAX_STICKY_OOM_RESTARTS} ]]; then + echo "Driver stuck in CUDA_ERROR_OUT_OF_MEMORY (NVBugs 7092346); giving up after ${MAX_STICKY_OOM_RESTARTS} restarts" >&2 + return "${rc}" + fi + echo "Driver stuck in CUDA_ERROR_OUT_OF_MEMORY (NVBugs 7092346); restarting pytest (${attempt}/${MAX_STICKY_OOM_RESTARTS}) to run the unfinished tests" + done +} + # JUnit XML, one file per suite. The test workflows upload this directory as an # artifact: a machine-readable record of the run for follow-up tooling. TEST_RESULTS_DIR="$(pwd)/test-results" @@ -187,7 +224,7 @@ elif [[ "${test_module}" == "core" || "${test_module}" == nightly-* ]]; then echo "Installed packages before core tests:" pip list echo "Running core tests" - ${SANITIZER_CMD} pytest -rsxXfE -v --randomly-dont-reorganize --exclude-warning-annotations "${PYTEST_PARALLEL_ARGS[@]}" --junitxml "${TEST_RESULTS_DIR}/junit-core.xml" tests/ | fold_pytest_log + run_pytest_with_restarts junit-core ${SANITIZER_CMD} pytest -rsxXfE -v --randomly-dont-reorganize --exclude-warning-annotations "${PYTEST_PARALLEL_ARGS[@]}" tests/ # Compiled Cython tests require the bindings used to build them. Older # toolkit rows intentionally exercise published floor bindings instead. if [[ "${SKIP_CYTHON_TEST}" == 0 ]]; then From 2a9774b857adfd5315077b84c87706e7675a2879 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Thu, 3 Dec 2026 10:19:16 -0800 Subject: [PATCH 3/7] test(cuda.core): drop unit tests for the temporary sticky-OOM workaround The workaround is removed once the fixed driver is the minimum CI driver, so its helpers do not need their own tests. Co-Authored-By: Claude Sonnet 5.5 --- cuda_core/tests/test_sticky_oom.py | 49 ------------------------------ 1 file changed, 49 deletions(-) delete mode 100644 cuda_core/tests/test_sticky_oom.py diff --git a/cuda_core/tests/test_sticky_oom.py b/cuda_core/tests/test_sticky_oom.py deleted file mode 100644 index b10ffebf5bb..00000000000 --- a/cuda_core/tests/test_sticky_oom.py +++ /dev/null @@ -1,49 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -from types import SimpleNamespace - -import pytest -from helpers import sticky_oom - - -@pytest.mark.agent_authored(model="claude-sonnet-5-5") -def test_healthy_process_is_not_stuck(init_cuda): - # A healthy driver, including one that has just reported a genuine - # allocation failure, must never be classified as stuck. - assert not sticky_oom.process_is_stuck_in_oom() - - -@pytest.mark.agent_authored(model="claude-sonnet-5-5") -def test_no_current_context_is_not_stuck(init_cuda): - from helpers.contexts import no_current_context - - with no_current_context(): - assert not sticky_oom.process_is_stuck_in_oom() - - -@pytest.mark.agent_authored(model="claude-sonnet-5-5") -@pytest.mark.parametrize( - ("failed", "when", "message", "expected"), - [ - (True, "call", "CUDA_ERROR_OUT_OF_MEMORY: x", True), - (True, "setup", "CUDA_ERROR_OUT_OF_MEMORY: x", True), - (True, "teardown", "CUDA_ERROR_OUT_OF_MEMORY: x", False), - (True, "call", "AssertionError: other", False), - (False, "call", "CUDA_ERROR_OUT_OF_MEMORY: x", False), - ], -) -def test_is_oom_failure(failed, when, message, expected): - report = SimpleNamespace(failed=failed) - call = SimpleNamespace(when=when, excinfo=SimpleNamespace(value=RuntimeError(message))) - assert sticky_oom.is_oom_failure(report, call) is expected - - -@pytest.mark.agent_authored(model="claude-sonnet-5-5") -def test_finished_nodeids_round_trip(tmp_path, monkeypatch): - done_file = tmp_path / "finished.txt" - monkeypatch.setenv(sticky_oom.DONE_FILE_ENV, str(done_file)) - assert sticky_oom.finished_nodeids() == set() - sticky_oom.record_finished("tests/test_a.py::test_one") - sticky_oom.record_finished("tests/test_a.py::test_two[x]") - assert sticky_oom.finished_nodeids() == {"tests/test_a.py::test_one", "tests/test_a.py::test_two[x]"} From 1a77afa4cc084da5e556408be731e99195c5a2ed Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Thu, 3 Dec 2026 10:29:13 -0800 Subject: [PATCH 4/7] test(cuda.core): keep failures from earlier attempts when the run restarts A test that failed before the driver got stuck was recorded as finished and skipped on restart, so a passing last attempt could turn the run green. Record failed tests separately, and fail the run after a restart if any exist. Failures are taken from the logged reports, because pytest-rerunfailures relabels a failed attempt it will rerun as "rerun" after makereport. Co-Authored-By: Claude Opus 5.5 --- ci/tools/run-tests | 8 +++++++- cuda_core/tests/conftest.py | 10 +++++++++- cuda_core/tests/helpers/sticky_oom.py | 13 +++++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/ci/tools/run-tests b/ci/tools/run-tests index 77883ffc464..db624dc3924 100755 --- a/ci/tools/run-tests +++ b/ci/tools/run-tests @@ -35,7 +35,7 @@ run_pytest_with_restarts() { shift local done_file="${TEST_RESULTS_DIR}/${junit_base}-finished.txt" local attempt=0 junit rc - rm -f "${done_file}" + rm -f "${done_file}" "${done_file}.failed" while true; do junit="${TEST_RESULTS_DIR}/${junit_base}.xml" if [[ ${attempt} -gt 0 ]]; then @@ -46,6 +46,12 @@ run_pytest_with_restarts() { rc=${PIPESTATUS[0]} set -e if [[ ${rc} -ne ${STICKY_OOM_EXIT_CODE} ]]; then + # 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 2ee8cd260f6..8c9eb8ae587 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -109,6 +109,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() def _stop_if_driver_is_stuck_in_oom(item, call, report): @@ -120,10 +121,17 @@ def _stop_if_driver_is_stuck_in_oom(item, call, report): item.session.shouldstop = f"driver stuck in CUDA_ERROR_OUT_OF_MEMORY (NVBugs 7092346) at {item.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.failed: + _failed_nodeids.add(report.nodeid) + + def pytest_runtest_logfinish(nodeid, location): # The test that exposed the stuck driver is not finished: it reruns. if nodeid != _sticky_oom_nodeid: - sticky_oom.record_finished(nodeid) + sticky_oom.record_finished(nodeid, failed=nodeid in _failed_nodeids) 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 8a52c243bf8..b4fd68a0cfd 100644 --- a/cuda_core/tests/helpers/sticky_oom.py +++ b/cuda_core/tests/helpers/sticky_oom.py @@ -29,6 +29,8 @@ # 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 @@ -68,12 +70,19 @@ def is_oom_failure(report, call): return OOM_MARKER in str(call.excinfo.value) -def record_finished(nodeid): - """Append a finished test to the resume file, if CI asked for one.""" +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. + """ path = os.environ.get(DONE_FILE_ENV) if path: with open(path, "a", encoding="utf-8") as f: f.write(nodeid + "\n") + if failed: + with open(path + FAILED_SUFFIX, "a", encoding="utf-8") as f: + f.write(nodeid + "\n") def finished_nodeids(): From e5b157f7fedd02cc01ea2fcc4f04b7db0b7387c9 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Thu, 3 Dec 2026 10:52:46 -0800 Subject: [PATCH 5/7] test(cuda.core): address review findings on the sticky-OOM restart - Do not record a test as finished after an attempt that pytest-rerunfailures will rerun, so a test that fails once and then exposes the stuck driver still runs in the restarted process. - Let a broken probe raise instead of reporting a healthy driver. - Turn the exit status into 75 only for a failed or interrupted run, so an internal or usage error is not retried. - Write the failure entry before the finished entry. - Fail the core test step again when the log folding step fails. Co-Authored-By: Claude Sonnet 5.5 --- ci/tools/run-tests | 5 ++++- cuda_core/tests/conftest.py | 21 ++++++++++++++++----- cuda_core/tests/helpers/sticky_oom.py | 21 ++++++++++----------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/ci/tools/run-tests b/ci/tools/run-tests index db624dc3924..3ec87094863 100755 --- a/ci/tools/run-tests +++ b/ci/tools/run-tests @@ -43,8 +43,11 @@ run_pytest_with_restarts() { fi set +e CUDA_CORE_TEST_DONE_FILE="${done_file}" "$@" --junitxml "${junit}" | fold_pytest_log - rc=${PIPESTATUS[0]} + rc=${PIPESTATUS[0]} fold_rc=${PIPESTATUS[1]} set -e + if [[ ${fold_rc} -ne 0 && ${rc} -eq 0 ]]; then + rc=${fold_rc} + fi if [[ ${rc} -ne ${STICKY_OOM_EXIT_CODE} ]]; then # Earlier attempts' failures are not rerun: they must still fail the run. if [[ ${attempt} -gt 0 && ${rc} -eq 0 && -s "${done_file}.failed" ]]; then diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index 8c9eb8ae587..308ad828e09 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -110,6 +110,7 @@ def pytest_terminal_summary(terminalreporter): # See helpers/sticky_oom.py. _sticky_oom_nodeid = None _failed_nodeids = set() +_rerun_nodeids = set() def _stop_if_driver_is_stuck_in_oom(item, call, report): @@ -121,17 +122,26 @@ def _stop_if_driver_is_stuck_in_oom(item, call, report): item.session.shouldstop = f"driver stuck in CUDA_ERROR_OUT_OF_MEMORY (NVBugs 7092346) at {item.nodeid}" +def pytest_runtest_logstart(nodeid, location): + # pytest-rerunfailures calls logstart and logfinish once per attempt. + _rerun_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.failed: + if report.outcome == "rerun": + _rerun_nodeids.add(report.nodeid) + elif report.failed: _failed_nodeids.add(report.nodeid) def pytest_runtest_logfinish(nodeid, location): - # The test that exposed the stuck driver is not finished: it reruns. - if nodeid != _sticky_oom_nodeid: - sticky_oom.record_finished(nodeid, failed=nodeid in _failed_nodeids) + # An attempt that is going to be rerun has not finished the test, and neither + # has the one that exposed the stuck driver: both must run again. + if nodeid in _rerun_nodeids or nodeid == _sticky_oom_nodeid: + return + sticky_oom.record_finished(nodeid, failed=nodeid in _failed_nodeids) def pytest_collection_modifyitems(config, items): @@ -144,7 +154,8 @@ def pytest_collection_modifyitems(config, items): def pytest_sessionfinish(session, exitstatus): - if _sticky_oom_nodeid is not None: + # Only a stop for this reason: an internal or usage error must not be retried. + if _sticky_oom_nodeid is not None and exitstatus in (pytest.ExitCode.TESTS_FAILED, pytest.ExitCode.INTERRUPTED): session.exitstatus = sticky_oom.STICKY_OOM_EXIT_CODE diff --git a/cuda_core/tests/helpers/sticky_oom.py b/cuda_core/tests/helpers/sticky_oom.py index b4fd68a0cfd..3442a584179 100644 --- a/cuda_core/tests/helpers/sticky_oom.py +++ b/cuda_core/tests/helpers/sticky_oom.py @@ -49,17 +49,15 @@ def process_is_stuck_in_oom(): """True if context-wide calls return OOM, which a genuine allocation failure never does. Needs a current context; returns False without one, so the next failing - test (the cascade produces many) gets another chance. Never raises and - never changes the current context. + test (the cascade produces many) gets another chance. Never changes the + current context. A broken probe raises rather than reporting a healthy + driver. """ - try: - err, ctx = driver.cuCtxGetCurrent() - if err != driver.CUresult.CUDA_SUCCESS or ctx is None or int(ctx) == 0: - return False - (sync_err,) = driver.cuCtxSynchronize() - mem_err = driver.cuMemGetInfo()[0] - except Exception: + err, ctx = driver.cuCtxGetCurrent() + if err != driver.CUresult.CUDA_SUCCESS or ctx is None or int(ctx) == 0: return False + (sync_err,) = driver.cuCtxSynchronize() + mem_err = driver.cuMemGetInfo()[0] return sync_err == _OOM and mem_err == _OOM @@ -78,11 +76,12 @@ def record_finished(nodeid, failed=False): """ path = os.environ.get(DONE_FILE_ENV) if path: - with open(path, "a", encoding="utf-8") as f: - f.write(nodeid + "\n") + # 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") + with open(path, "a", encoding="utf-8") as f: + f.write(nodeid + "\n") def finished_nodeids(): From 39ff0b774d327ec7c7d53d5082015142e81ef243 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Thu, 3 Dec 2026 12:03:58 -0800 Subject: [PATCH 6/7] test(cuda.core): rerun tests that failed with OOM before a sticky-OOM restart The first test of a stuck-driver cascade can fail with OOM before the stuck state is detected, e.g. in a teardown with no current context to probe. It was recorded as a finished failure, so the restart skipped it and ci/tools/run-tests failed the run although every test passed in the fresh process (seen in PR #3695's Windows CI). Do not record a test whose attempt failed with OOM in any phase: it runs again after a restart, where a genuine OOM fails again and a symptom of NVBugs 7092346 passes. Co-Authored-By: Claude Opus 5.5 --- cuda_core/tests/conftest.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index 308ad828e09..f76ec4b8466 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -111,6 +111,10 @@ def pytest_terminal_summary(terminalreporter): _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() def _stop_if_driver_is_stuck_in_oom(item, call, report): @@ -125,6 +129,7 @@ def _stop_if_driver_is_stuck_in_oom(item, call, report): def pytest_runtest_logstart(nodeid, location): # pytest-rerunfailures calls logstart and logfinish once per attempt. _rerun_nodeids.discard(nodeid) + _oom_nodeids.discard(nodeid) def pytest_runtest_logreport(report): @@ -134,12 +139,16 @@ 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: + _oom_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: both must run again. - if nodeid in _rerun_nodeids or nodeid == _sticky_oom_nodeid: + # 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: return sticky_oom.record_finished(nodeid, failed=nodeid in _failed_nodeids) From bf9bf5151678a7d40c91e3a428a15d5c46ce5f85 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Thu, 3 Dec 2026 12:09:40 -0800 Subject: [PATCH 7/7] test(cuda.core): raise if the sticky-OOM probe cannot query the context Use current_context_handle(), which raises on a failed cuCtxGetCurrent, instead of treating any failure as "no context". cuCtxGetCurrent still succeeds in the stuck state (it reported no context in #3695's Windows CI), so this does not stop the restart from working. Co-Authored-By: Claude Opus 5.5 --- cuda_core/tests/helpers/sticky_oom.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cuda_core/tests/helpers/sticky_oom.py b/cuda_core/tests/helpers/sticky_oom.py index 3442a584179..0979bcf179a 100644 --- a/cuda_core/tests/helpers/sticky_oom.py +++ b/cuda_core/tests/helpers/sticky_oom.py @@ -26,6 +26,8 @@ from cuda.bindings import driver from cuda_python_test_helpers import IS_WINDOWS +from .contexts import current_context_handle + # 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" @@ -51,10 +53,9 @@ def process_is_stuck_in_oom(): Needs a current context; returns False without one, so the next failing test (the cascade produces many) gets another chance. Never changes the current context. A broken probe raises rather than reporting a healthy - driver. + driver; ``cuCtxGetCurrent`` still succeeds in the stuck state. """ - err, ctx = driver.cuCtxGetCurrent() - if err != driver.CUresult.CUDA_SUCCESS or ctx is None or int(ctx) == 0: + if current_context_handle() == 0: return False (sync_err,) = driver.cuCtxSynchronize() mem_err = driver.cuMemGetInfo()[0]