Skip to content

test(cuda.core): make the OOM probe work after context teardown and keep its report - #3543

Merged
juenglin merged 3 commits into
NVIDIA:mainfrom
juenglin:oom-probe-context-fixes
Nov 24, 2026
Merged

juenglin merged 3 commits into
NVIDIA:mainfrom
juenglin:oom-probe-context-fixes

Conversation

@juenglin

Copy link
Copy Markdown
Contributor

Description

Follow-up to the OOM reason checker added for #2381.

In this Windows MCDM run the checker fired but produced nothing useful:

  • The first OOM came from a teardown, after init_cuda had popped the context, so the probe stopped at cuCtxGetCurrent() (no context) and skipped every check. That report also consumed the once-per-session latch, so none of the ~180 OOMs that followed were probed.
  • The job later crashed with an access violation, so the end-of-run summary line was never printed, and the artifact file had been written to cuda_core/, which CI does not upload.

Changes, all in cuda_core/tests/helpers/oom_diagnostics.py:

  • When no context is current, the probe retains device 0's primary context, pushes it for the probe, and pops and releases it afterwards. The report says when this happened and warns if restoring the context state failed.
  • The report is written beside the --junitxml file when one is given (test-results/ in CI, which is uploaded). Because it is written when captured, it survives a later crash.
  • A report whose probe got no context is still emitted once, but does not consume the latch: the next OOM whose probe gets a context is reported too and appended to the same artifact.

The OOM reporter exists only in cuda_core/tests; the cuda_bindings_* test suites have no counterpart.

Checklist

  • I have read the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

…eep its report

The first OOM often surfaces in a teardown, after init_cuda has popped the context, so the probe found no context and skipped every check. Borrow device 0's primary context in that case, write the report beside the --junitxml file so CI uploads it, and do not let a report without a context consume the once-per-session latch.
@juenglin juenglin added this to the cuda.core 1.4.0 milestone Nov 23, 2026
@juenglin juenglin added test Improvements or additions to tests cuda.core Everything related to the cuda.core module labels Nov 23, 2026
@juenglin juenglin self-assigned this Nov 23, 2026
@copy-pr-bot

copy-pr-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

… artifacts

Follow-ups from review of the OOM probe context change:

- A failed borrow of device 0's primary context (cuDeviceGet, retain or
  push) is recorded in a dedicated borrow_error field, printed on its own
  log line and named in the verdict, instead of being attributed to
  cuCtxGetCurrent.
- Document that the borrow is always device 0.
- Resolve a relative --junitxml against the invocation directory, not the
  current one, which a test may have changed.
- Pick an unused artifact name rather than truncating a report left by an
  earlier pytest invocation sharing the results directory (tests/ and
  tests/cython).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@juenglin

Copy link
Copy Markdown
Contributor Author

/ok to test aabf2fc

@juenglin
juenglin marked this pull request as ready for review November 23, 2026 23:12
@juenglin
juenglin enabled auto-merge (squash) November 23, 2026 23:13
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Out-of-memory diagnostics can capture a later report when an earlier report has no usable CUDA context. Probes can temporarily borrow and restore the device’s primary context, with setup or restoration failures recorded.
    • Reports distinguish the initial out-of-memory event from the first successfully probed event.
    • Diagnostic artifacts are saved beside the JUnit report when configured, or in the project root otherwise. Unique filenames help preserve artifacts across recorder runs.
    • The terminal summary indicates whether a probe found a usable context and, when applicable, which test was probed.

OOM diagnostics can borrow device 0’s primary context when no context is current. The recorder can capture a later report after an initial contextless probe. Artifact paths account for JUnit output, and exclusive file creation avoids overwriting existing reports.

Changes: OOM Diagnostics

Layer / File(s) Summary
Probe context handling
cuda_core/tests/helpers/oom_diagnostics.py, cuda_core/tests/test_helpers.py
run_probe uses the current context or borrows device 0’s primary context. It records borrowing and restoration failures. Classification, logs, and tests cover these outcomes.
Deferred reports and artifact paths
cuda_core/tests/helpers/oom_diagnostics.py, cuda_core/tests/test_helpers.py
The recorder permits a later report when the initial probe has no context. It selects artifact paths from JUnit configuration or pytest’s root directory and creates collision-safe files. Tests cover deferred capture, path resolution, and write failures.

Suggested reviewers: rwgk

Change: Other

Merge Risk: Minimal · up to d7f7e

This change makes the OOM diagnostics probe more robust when no CUDA context is current and writes reports more safely. It only affects test helpers, and no merge-blocking issue remains.

Security architecture risk: Low · up to d7f7e

The change is confined to CUDA tests. The diagnostic can now alter CUDA state after a failed test has torn down its context. Normal cleanup restores the context, but a cleanup failure or a long-lived memory reservation can affect later tests in the same run.

Retained concerns

  • Low · reliability · inferred: The new borrow path attempts release even when pop fails, records the failure, and lets pytest continue. If the failed pop leaves a context binding in place, later work on that thread can run with unexpected CUDA state. A failed release is likewise reported without recovery. The exact post-failure driver state is unverified.
  • Low · reliability · inferred: The previously contextless path can now reach the existing default-pool query. If that query initializes the pool, its virtual-address reservation is documented to persist for the process lifetime. Even successful context cleanup therefore does not make the diagnostic state-neutral, and later tests can see different memory conditions.
Security review details

Security Blast Radius

  • inferred — The changed driver path runs with the test process’s existing authority. The directly supported scope is the calling thread’s CUDA context, device 0’s primary context, and resources used by the test process; the reviewed path does not establish a remotely callable entrypoint or cross-tenant access.

Trust Boundaries and Controls

  • observed — Exception text is checked for the OOM marker and included as report content; it does not determine the destination. The runner’s pytest configuration selects the directory, and exclusive creation prevents the first write from replacing an existing report.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between aabf2fc and d7f7e7a.

Files selected for processing: 2

  • cuda_core/tests/helpers/oom_diagnostics.py
  • cuda_core/tests/test_helpers.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between e2c95d7 and aabf2fc.

Files selected for processing: 2

  • cuda_core/tests/helpers/oom_diagnostics.py
  • cuda_core/tests/test_helpers.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 9 remain after this review.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor
Doc Preview CI
Preview removed because the pull request was closed or merged.

…ror handling

Picking an unused artifact name called Path.exists() outside the OSError handler, so an inaccessible results directory raised out of pytest_runtest_makereport and lost the report. Create the file exclusively instead, retrying numbered names on FileExistsError, so every filesystem error is reported with the diagnostics. Until a write succeeds the artifact stays unwritten, and a later report creates a new file rather than appending.
@juenglin

Copy link
Copy Markdown
Contributor Author

/ok to test d7f7e7a

@juenglin
juenglin merged commit a79afd0 into NVIDIA:main Nov 24, 2026
121 checks passed
@juenglin

Copy link
Copy Markdown
Contributor Author

Design review: a smaller fix for both problems

I stepped back from the diff and re-derived the fix from the failing run and the code on main. The diagnosis in the description is right, but I think each of the two problems can be solved with much less machinery. This is a design suggestion; I have not implemented or run the alternative.

What the failing run shows

From the Windows MCDM job log:

  • The first OOM was the teardown of test_mempool_watermark_reset[PinnedMR-used], with no context current, as described.
  • The next call-phase OOM, with a context current, came three tests later (test_mempool_trim[DeviceMR]).
  • After the first report there were 178 call-phase OOM failures, 732 teardown errors and 106 setup errors before the access violation.
  • The report text itself reached the job log despite the crash. Only the artifact file (written to cuda_core/) and the end-of-run summary line were lost.

Where the complexity comes from

  • Two mechanisms for one problem. The PR both borrows device 0's primary context and keeps the latch open when no context was obtained. Either one alone would have fixed this run. Having both is what creates the captured/probed split, the second report, append mode, probed_nodeid, borrow_error and context_restore_error.
  • The review rounds were about the artifact, not the context. Both review findings were against _unused_path(), the "pick an unused file name" logic added in the second commit, and the third commit replaced it with an exclusive-create retry loop. A side feature is generating the churn.

Suggested fix

  1. Latch only on a probe that had a context. In capture(), run the probe first. If has_context is false, return without latching and without a full report (at most a one-line note, once). The next call-phase OOM gets the real probe. The diagnostics hook then never touches CUDA context state: no borrow, no restore-failure path, and still exactly one report per session.
  2. Name the artifact after the JUnit file. Write <junit stem>-oom-diagnostics.txt beside --junitxml (junit-core-oom-diagnostics.txt, junit-core-cython-oom-diagnostics.txt), falling back to the rootdir as today. JUnit names are already unique per pytest invocation, so tests/ and tests/cython cannot collide. A plain write_text inside the existing except OSError is then enough: no existence check, no retry loop, no append.

That is roughly 15-20 lines in cuda_core/tests/helpers/oom_diagnostics.py plus two or three tests, against +200/+204 here. artifact_directory() (anchoring a relative path at the invocation directory) is fine and worth keeping.

Trade-off

This gives up probing at the very first OOM. A session whose only OOMs are in teardown, or a single transient teardown OOM, would get no probe. I think that is acceptable: the probes measure process-wide host VA and device memory, which were still exhausted three tests later in this run, and a transient OOM would mostly probe as "inconclusive" anyway.

If the first-event probe is wanted, a middle option is to keep the borrow but drop the open latch and the second-report logic, since a failed borrow is itself the finding. That is still simpler than the current diff, though it keeps context push/pop in a diagnostics hook.

Either way, I would take the JUnit-stem naming over the exclusive-create loop.

juenglin added a commit that referenced this pull request Nov 24, 2026
…ct naming (#3551)

Follow-up to #3543. Instead of borrowing device 0's primary context when no context is current, note the OOM once and probe at the next one that has a context, so the diagnostics never change context state. Name the artifact after the --junitxml file, which is unique per pytest invocation, instead of picking an unused name.
github-actions Bot pushed a commit that referenced this pull request Nov 25, 2026
Removed preview folders for the following PRs:
- PR #3499
- PR #3524
- PR #3543
- PR #3544
- PR #3546
- PR #3551
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module test Improvements or additions to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants