Skip to content

[BUG]: cuda-core 0.5: cuMemFreeAsync from the retirement thread segfaults during graph capture #1850

Open

Description

@1994

Environment

  • cutile / cuda-core 0.5.0 (tag cutile-rs/v0.5.0), Rust 1.99.1, Linux x86_64
  • RTX 5090, driver 620.70.06, CUDA 13.4
  • Workload: a serving engine that loads a 27B NVFP4 model. Model load captures many CUDA graphs and drops many stream-ordered buffers along the way.

Symptom

One of six server starts using 0.5.0 died with SIGSEGV during model load. The same workload on 0.4.0 has not crashed in 12+ runs (same host, same driver, same model).

#0  libcuda.so.1 + 0x350ef4
#3  cuMemFreeAsync (libcuda.so.1)
#4  cuda_core::allocation::Allocation::drop::{closure}
#5  cuda_core::completion::enqueue::{closure}          thread "cuda-resource-retirement"

The free keeps its stream alive via Arc, and the pointer is freed exactly once (0.4 freed it synchronously, 0.5 frees it stream-ordered), so this does not look like a dangling stream or a double free.

What changed in 0.5, and why we suspect the retirement path

  • Allocation::drop now calls completion::defer for stream-ordered allocations. That pending item has event: None, so the cleanup is not completion-bound: it runs shortly after on the cuda-resource-retirement thread and calls cuMemFreeAsync(ptr, allocation_stream) (see cuda-core/src/allocation.rs drop and cuda-core/src/completion.rs).
  • That path has no capture awareness: no cuStreamIsCapturing check, and no coordination with capture begin/end.
  • cuda-async's capture_on always begins capture in CU_STREAM_CAPTURE_MODE_RELAXED (cuda-async/src/cuda_graph.rs), which disables the driver's cross-thread interference detection. Our own AOT driver captures in CU_STREAM_CAPTURE_MODE_GLOBAL, where a call from another thread to a capturing stream is an invalid operation.

So a buffer dropped during (or just before) a capture is freed by a different thread, at an arbitrary later time, on a stream that may be mid-capture. In 0.4 the free happened synchronously on the dropping thread, at a point the application controlled and could keep outside capture windows; in 0.5 it escapes that ordering entirely. That matches an intermittent failure that only shows up in a graph-capturing load.

At minimum this should surface as CUDA_ERROR_STREAM_CAPTURE_UNSUPPORTED (or be queued), not a segfault in cuMemFreeAsync. A plausible sketch of a reproducer: thread A loops begin_capture(GLOBAL on S) -> launch noop -> end_capture, while thread B drops DeviceBuffers that were allocated on S, so some frees land inside A's capture window.

Expected

  1. Freeing a stream-ordered allocation while its stream is capturing is either supported and queued until the capture ends, or
  2. the retirement path detects capture and postpones the free (e.g. cuStreamIsCapturing, retry after end_capture), and
  3. the driver reports an error instead of crashing.

Questions

  • Is dropping a stream-ordered buffer while its stream is capturing supported today? If not, can Allocation::drop / completion::defer avoid calling the driver in that state, the way other unknown-completion cases are retained/leaked instead of freed?
  • Would it be reasonable for cuda-async's capture path to own a capture depth so retirement defers frees until the outermost capture ends (or to expose the capture mode so callers can use THREAD_LOCAL)?
  • Is THREAD_LOCAL capture intended to make cross-thread retirement safe here, given cuda-async currently does not expose the mode?

Reproduction status

No minimal reproducer yet. The failure is intermittent (~1/6 model loads) and needs a graph-capturing load. We have a complete 0.5.0 port of the engine and a host we can run stress loops on; we can instrument completion::defer with cuStreamIsCapturing and thread-id logging to confirm the mechanism and to test a patch. If you tell us the harness shape you prefer, we will build a standalone reproducer.

Related: #1783 (Context::drop runs on the retirement thread and races exit()), #1842 (deferred frees and recorded drop errors).

Activity

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