Repository navigation
Description
Description
When a deferred Allocation::drop closure holds the last Arc<Context>, Context::drop → cuDevicePrimaryCtxRelease_v2 runs on the detached "cuda-resource-retirement" thread (completion::enqueue) while main is already in exit(). Nothing joins that thread, so the context release races the driver's atexit teardown. Under Nsight Compute 2026.4.0 this aborts with heap corruption.
Error / actual behavior
retire mode below, run 10 times each:
| run | crashed |
|---|---|
under ncu, MALLOC_CHECK_=3 |
10/10 (free(): invalid pointer, SIGSEGV or SIGABRT) |
| under ncu, no malloc check | 2/10 (SIGABRT) |
no ncu, MALLOC_CHECK_=3 |
0/5 |
main mode (context dropped on main before exit), under ncu, MALLOC_CHECK_=3 |
0/10 |
Retirement thread at the abort:
#21 cuDevicePrimaryCtxRelease_v2 () from libcuda.so.1
#24 cuDevicePrimaryCtxRelease_v2 () from libcuda-injection.so
#25 <cuda_core::runtime::Context as core::ops::drop::Drop>::drop ()
#26 <alloc::sync::Arc<cuda_core::runtime::Context>>::drop_slow ()
#27 <<cuda_core::allocation::Allocation as core::ops::drop::Drop>::drop::{closure#0} as FnOnce<()>>::call_once
#28 std::sys::backtrace::__rust_begin_short_backtrace::<cuda_core::completion::enqueue::{closure#0}::{closure#0}, ()>
Main thread at the same moment:
#6 close () from libInterceptorInjectionTarget.so
#7-#11 ?? () from libnvidia-ml.so.1
#12 ?? () from libcuda-injection.so
#13 __run_exit_handlers (status=0, ...) at ./stdlib/exit.c:108
#14 __GI_exit (status=<optimized out>) at ./stdlib/exit.c:138
Expected behavior
The last context release finishes before the process exits, so no CUDA call runs on the retirement thread concurrently with exit().
Minimal reproduction example
Cargo.toml: cuda-core = { git = "https://github.com/NVIDIA/cuda-rust", rev = "367bc0f8e" }
//! Usage: repro [retire|main] [launches] [buffers]
//! `retire`: drop the context and stream handles, then the buffers, and return from main, so
//! the last `Arc<Context>` drops on the retirement thread while main runs exit().
//! `main` (control): drop the buffers, wait for the retirement thread, then drop the context.
use cuda_core::{CudaContext, DeviceBuffer, launch_kernel_on_stream};
use std::time::Duration;
const PTX: &str = ".version 8.0\n.target sm_52\n.address_size 64\n.visible .entry noop()\n{\n\tret;\n}\n";
fn main() {
let mut args = std::env::args().skip(1);
let mode = args.next().unwrap_or_else(|| "retire".into());
let mut count = |default| args.next().map_or(default, |a| a.parse().expect("count"));
let (launches, buffers): (usize, usize) = (count(1000), count(16));
let ctx = CudaContext::new(0).expect("context");
let stream = ctx.new_stream().expect("stream");
let bufs: Vec<DeviceBuffer<f32>> = (0..buffers)
.map(|_| DeviceBuffer::zeroed(&stream, 1 << 16).expect("buffer"))
.collect();
let module = ctx.load_module_from_ptx_src(PTX).expect("module");
let noop = module.load_function("noop").expect("function");
for _ in 0..launches {
// SAFETY: `noop` takes no parameters and touches no memory.
unsafe { launch_kernel_on_stream(&noop, (1, 1, 1), (1, 1, 1), 0, &stream, &mut []) }
.expect("launch");
}
stream.synchronize().expect("sync");
drop(noop);
drop(module);
match mode.as_str() {
"retire" => {
drop(ctx);
drop(stream);
drop(bufs);
}
"main" => {
drop(bufs);
drop(stream);
std::thread::sleep(Duration::from_millis(200));
drop(ctx);
}
_ => panic!("mode is retire or main"),
}
}MC="env LD_PRELOAD=/lib/x86_64-linux-gnu/libc_malloc_debug.so.0 MALLOC_CHECK_=3"
$MC ncu -k regex:zzzz target/release/repro retire # crashes
$MC ncu -k regex:zzzz target/release/repro main # exits 0
-k regex:zzzz profiles nothing, so no counter permission is needed.
Operating System
Ubuntu 24.04.5 LTS (kernel 6.14)
GPU and GPU Architecture
RTX 5000 Ada (sm_89)
CUDA driver version
620.70.06 (CUDA 13.5), Nsight Compute 2026.4.0
rustc version
rustc 1.99.1 (58cf8f9c3 2026-10-13)
Activity