Skip to content

[BUG]: dropping a from_host DeviceBuffer stalls other threads' driver calls until the context is idle #1852

Open

Description

@build4me2

Description

Dropping a DeviceBuffer created by from_host (or zeroed) frees it with cuMemFree_v2 on the cuda-resource-retirement thread. cuMemFree doesn't return until all outstanding work in the context has finished, and while it waits, driver calls from other threads block too. So dropping one buffer stalls every other thread's submissions, including on streams unrelated to that buffer, for as long as any stream in the context has pending work.

I found this while tracing #1768. #1782 fixed that test by keeping the tests apart, but the library behavior is unchanged on main and with #1831 applied.

Error / actual behavior

Stream A is held by a host function for 5 s; stream B is idle. After an unrelated buffer is dropped, zero_async on B waits for A (5 runs each):

zero_async on idle stream B
from_host buffer dropped 4.80 s
zeroed buffer dropped 4.80 s
buffer kept (control) 0.11–0.19 ms

The stall tracks how long A is held: holding it for 2 s gives 1.80 s, and 8 s gives 7.80 s (the reproduction sleeps 0.2 s before the call). Same result on main (8046b9e9d) and on #1831's head (0b55a3441).

Raw driver calls give the same split. The free runs on a second thread; the memset goes to idle stream B:

Other thread memset on B
no free 0.03–0.05 ms
cuMemFree 4.80 s
cuMemFreeAsync 0.02 ms

Expected behavior

DeviceBuffer's docs say drop "retires deallocation on a worker without waiting on the caller's thread". The drop call itself does return immediately, but the caller's next driver call, and every other thread's, still waits for the whole context. Dropping a buffer should not delay submissions on streams it was never used on.

Minimal reproduction example

use cuda_core::{Context, DeviceBuffer};
use std::sync::mpsc;
use std::time::{Duration, Instant};

#[test]
fn dropping_from_host_buffer_stalls_unrelated_stream() {
    let ctx = Context::new(0).unwrap();
    let a = ctx.new_stream().unwrap(); // held by a host callback
    let b = ctx.new_stream().unwrap(); // idle, unrelated
    let mut target = DeviceBuffer::from_host(&b, &[1u32; 64]).unwrap();
    let victim = DeviceBuffer::from_host(&b, &[2u32; 64]).unwrap();
    b.synchronize().unwrap();
    let (tx, rx) = mpsc::channel::<()>();
    a.launch_host_function(move || {
        let _ = rx.recv_timeout(Duration::from_secs(5));
    })
    .unwrap();
    drop(victim); // cuMemFree deferred to the retirement thread
    std::thread::sleep(Duration::from_millis(200));
    let t = Instant::now();
    target.zero_async(&b).unwrap();
    let elapsed = t.elapsed();
    let _ = tx.send(());
    a.synchronize().unwrap();
    eprintln!("zero_async on idle stream B: {elapsed:?}"); // 4.8 s; ~0.1 ms without the drop
}

Operating System

Ubuntu 24.04.3 LTS (kernel 6.17.0-1032-nvidia)

GPU and GPU Architecture

NVIDIA GB10 (sm_121)

CUDA driver version

610.57.04 (CUDA 13.0)

Additional context

I think the fix is to put from_host, from_host_async_unchecked and zeroed on the stream-ordered allocation path (Owner::StreamOrdered, freed with cuMemFreeAsync), which doesn't stall in the probe above. #89 moved these constructors to malloc_sync to fix mismatched alloc/free pairing, so the change has to keep allocation and free matched rather than swap the call back. Buffers built with from_raw_parts from a cuMemAlloc pointer would still need the synchronous free.

Related: #1768 (where this showed up), #1782 (test-side fix), #89, #1831 (same worker thread).

Activity

  1. midagedev commented on Nov 24, 2026

    @midagedev
    Contributor

    We hit the same free as a hang, not a stall. When the work that cuMemFree waits for is a stream waiting on a host store that the blocked thread would make next, the free never returns and that thread never runs again. Stacks: the retirement thread in cuMemFree_v2 ← Allocation::drop closure ← completion::enqueue; our engine thread in cuGraphLaunch → pthread_rwlock_rdlock. Seen at ab2b6738 on an RTX 5090 (CUDA 13.4 driver). A public way to drain or pause the retirement thread would let callers order around it; for now we park it with a retain_until_complete marker on an idle stream while such a pass runs.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions