Skip to content

fix(cuda.core): surface real CUDA error in Device.set_current() - #2461

Merged
juenglin merged 1 commit into
NVIDIA:mainfrom
juenglin:set-primary-context-bug
Jul 31, 2026
Merged

fix(cuda.core): surface real CUDA error in Device.set_current()#2461
juenglin merged 1 commit into
NVIDIA:mainfrom
juenglin:set-primary-context-bug

Conversation

@juenglin

@juenglinjuenglin commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

closes#2460

Device.set_current() raised a misleading ValueError ("Cannot set NULL
context as current") when cuDevicePrimaryCtxRetain failed, discarding
the actual driver error code and leaving the thread-local error state
polluted.

Replace with HANDLE_RETURN(get_last_error()) to raise CUDAError with
the real driver status, matching the established pattern used elsewhere.

Manually verified by patching get_primary_context to return a failure;
a regression test is not practical because the failure requires
exclusive compute mode (unavailable on WDDM) or hardware error states
that cannot be simulated from Python.

@copy-pr-bot

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@github-actionsgithub-actionsBot added the cuda.core Everything related to the cuda.core module label Jul 30, 2026
@juenglin
juenglin marked this pull request as ready for review July 30, 2026 22:37
@juenglinjuenglin added this to the cuda.core 1.2.0 milestone Jul 30, 2026
@juenglinjuenglin added the bug Something isn't working label Jul 30, 2026
@juenglinjuenglin self-assigned this Jul 30, 2026
@github-actions

This comment has been minimized.

@kkraus14

Copy link
Copy Markdown
Collaborator

@juenglin fix looks good, but is there a reasonable way to test this?

@juenglin

Copy link
Copy Markdown
ContributorAuthor

@juenglin fix looks good, but is there a reasonable way to test this?

I couldn't find a way to test it with an automated test.

To test it I reproduced the bug with a temporary code change by which get_primary_context would always fail (setting a CUDA error). Then I confirmed that this change fixes it.

@juenglin
juenglin merged commit 915d0da into NVIDIA:mainJul 31, 2026
109 of 111 checks passed
@github-actions

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

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugSomething isn't workingcuda.coreEverything related to the cuda.core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Device.set_current() discards real CUDA error and raises misleading ValueError

2 participants

@juenglin@kkraus14