[Cuda] Skip FreeDataSpace when CUDA driver is in inconsistent state #16980
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Prior to this commit, the RAII handler in
NDArray
would always attempt to free a cuda memory allocation on destruction. However, the call tocudaFree
may throw an exception. If this happens during stack unwinding due to a previously-thrown exception, this causes the program to immediately terminate, making it difficult to identify the source of the original error.This can commonly occur if an async compute kernel performs an illegal memory access. An exception is thrown from the next cuda API call following the asynchronous error, causing the stack to unwind. If the stack contains any
NDArray
instances which reference cuda allocations, the destructor of theseNDArray
instances will attempt to free memory, triggering the segfault.This commit updates the
CUDADeviceAPI::FreeDataSpace
function to check if the program is currently unwinding the stack due to a thrown exception, while the cuda driver has been left in an unrecoverable state. If this occurs, no attempt to free memory is made, as all cuda API calls will result in an error, and the original exception is allowed to propagate.If the cuda driver is in an unrecoverable state, but no exception is currently unwinding the stack, then this may be the first cuda API call to occur after the asynchronous error. In this case, the
cudaFree
call is still performed, which throws the initial exception.