Skip to content

Fix: delay CUDADriverWrapper instantiation to avoid uncaught exception #25117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

krahenbuhl
Copy link

Fix: delay CUDADriverWrapper instantiation to avoid uncaught exceptions when CUDA is unavailable

Description

This PR moves the static instantiation of CUDADriverWrapper from a class-level static field to a function-local static inside CUDADriverWrapper::GetInstance(). This change ensures that the CUDA driver is only loaded when the instance is actually needed, rather than at static initialization time. It preserves the singleton behavior while deferring instantiation to runtime.

Motivation and Context

When libcuda.so.1 is not available on the system, the constructor of CUDADriverWrapper throws an exception. Previously, this exception was triggered during static initialization, leading to an uncatchable std::terminate() and process termination. By moving the instance into GetInstance() as a function-local static, the exception can now be caught by client code (e.g., in try/catch), allowing graceful fallback when CUDA is unavailable.

…ns when CUDA is unavailable

Moved the static instance of CUDADriverWrapper into GetInstance() as a function-local static.
This ensures the constructor is only called on first use, allowing exceptions (e.g., missing libcuda.so.1)
to be caught gracefully, rather than causing process termination during static initialization.
@krahenbuhl
Copy link
Author

@microsoft-github-policy-service agree company="Pix4D"

@tianleiwu
Copy link
Contributor

/azp run Linux QNN CI Pipeline, Win_TRT_Minimal_CUDA_Test_CI, Windows ARM64 QNN CI Pipeline, Windows GPU Doc Gen CI Pipeline, Windows x64 QNN CI Pipeline

Copy link

Azure Pipelines successfully started running 5 pipeline(s).

const CUDADriverWrapper* CUDADriverWrapper::GetInstance() {
static CUDADriverWrapper instance;
Copy link
Member

Choose a reason for hiding this comment

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

Please be very careful that this object will be deleted before all global variables. Please check who depends on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants