Skip to content

[BUG]: shared_allocation pending_copy test fails under parallel test threads #1768

Open

Description

@alejandro-soto-franco

Description

pending_copy_excludes_other_stream_and_outlives_dropped_buffer in cuda-core/tests/shared_allocation.rs fails intermittently when the test binary runs its tests on parallel threads. It passes every time with --test-threads=1 and when run alone.

Error / actual behavior

thread 'pending_copy_excludes_other_stream_and_outlives_dropped_buffer' panicked at cuda-core/tests/shared_allocation.rs:84:5:
assertion failed: storage.acquire(&other, false, false).is_err()

Line 84 is the first check after zero_async. A read lease on other succeeds while the zero fill on stream sits behind the Gate host function. The write lease from enqueue_access has therefore left active before its completion event fired, or never conflicted.

Over 40 runs each of the built test binary at 8d8254869:

Mode Failures
default (parallel threads) 8 / 40
--test-threads=1 0 / 40
--exact pending_copy_excludes_other_stream_and_outlives_dropped_buffer 0 / 40

Expected behavior

The acquire on other returns CUDA_ERROR_NOT_READY until drop(gate), regardless of which tests share the process.

Minimal reproduction example

cargo test -p cuda-core --test shared_allocation --no-run
BIN=$(ls -t target/debug/deps/shared_allocation-* | grep -v '\.d$' | head -1)
fails=0; for i in $(seq 40); do "$BIN" -q >/dev/null 2>&1 || fails=$((fails+1)); done; echo "parallel: $fails/40"
fails=0; for i in $(seq 40); do "$BIN" -q --test-threads=1 >/dev/null 2>&1 || fails=$((fails+1)); done; echo "serial: $fails/40"

Operating System

Fedora 44, Linux 7.2.13

GPU and GPU Architecture

NVIDIA GeForce RTX 5060 Laptop GPU, sm_120

CUDA driver version

615.78.08 (CUDA 13.4 UMD), toolkit 13.3

cargo oxide doctor

All checks pass.

rustc version

1.98.0 (root workspace pin)

llc version

Not applicable (host workspace test).

Additional context

The other tests in the file create their own Context::new(0) and streams, and stream_drop_is_nonblocking_and_holds_context_until_completion also blocks its stream with a Gate host function. The interaction with those concurrent tests is the likely trigger. The full host workspace otherwise passes (264 tests).

Activity

  1. added
    cutile-rs
    on Nov 12, 2026
  2. build4me2 commented on Nov 15, 2026

    @build4me2

    I reproduced and traced this on a GB10 (sm_121, CUDA 13.0, driver 610.57.04). The cause matches the one named in #1782's concurrency audit (context-synchronizing setup overlapping gated work), and on current main (348895e42) the test passes: 0/200 parallel runs.

    The specific trigger is fresh_thread_allocation_binds_its_context. Dropping its from_host buffer defers cuMemFree_v2 to the cuda-resource-retirement thread. That call doesn't return until the gated stream drains, and while it waits, the test thread's cuMemsetD8Async (from zero_async) is blocked too, so nothing moves until the gate's 10 s timeout. At 6156bed1d, running just those two tests 30 times gave 9 failures, each one a run where zero_async took 10 s. Leaking the buffer instead of freeing it gave 0/30.

    The test lock doesn't cover the underlying problem: the same stall is reachable through the public API. On main, dropping a from_host buffer while another stream in the context has pending work blocks other threads' driver calls, including on an unrelated idle stream, until that work finishes.

    Reproduction (4.8 s stall, 3/3 runs)
    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
    }

    With raw driver calls the result is the same: the memset takes 4.8 s behind a pending cuMemFree, and 0.0 ms with no free or with cuMemFreeAsync.

    I think the fix is to move from_host and zeroed onto the stream-ordered allocation path (Owner::StreamOrdered, freed with cuMemFreeAsync), which doesn't stall. #89 moved them to malloc_sync to fix mismatched alloc/free pairing, so this needs the matched stream-ordered path rather than swapping the call back. I'll open a separate issue to track it unless you'd rather handle it here.

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

    cutile-rs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions