Skip to content

cuda.core: capture complete Buffer deallocation recipe at creation - #2526

Open
Andy-Jost wants to merge 6 commits into
NVIDIA:mainfrom
Andy-Jost:ajost/deallocation-stream
Open

cuda.core: capture complete Buffer deallocation recipe at creation#2526
Andy-Jost wants to merge 6 commits into
NVIDIA:mainfrom
Andy-Jost:ajost/deallocation-stream

Conversation

@Andy-Jost

Copy link
Copy Markdown
Contributor

Summary

Closes #2497. Supersedes the approach in draft #2455.

Buffer / device-pointer destruction can run with no suitable CUDA context current (deferred graph cleanup, off-thread GC). Owning paths now capture a complete free-ordering recipe at allocation or binding time — a StreamHandle with a bound context for default-stream tokens — and activate that context at teardown instead of relying on ambient thread state.

Changes

  • Introduce an internal DeallocationStream (stream handle + optional PTDS host-thread id) and bind context into default-stream tokens when recording free ordering.
  • Activate the bound context around device-pointer teardown; report cleanup failures instead of silently skipping frees.
  • Add keyword-only stream= on Buffer.from_handle / ManagedBuffer.from_handle when mr= owns the pointer, so the destructor is not the first place a stream is chosen.
  • Stop treating CUDA_ERROR_INVALID_CONTEXT as success in _MemPool.deallocate, and let explicit mr.deallocate() raise; destruction still contains errors in the MR dealloc callback.
  • Document PTDS deallocation ordering on the relevant stream parameters and note the fix in the 1.2.0 release notes.

PTDS remains app-ordered for cross-thread / deferred free (no attach-time warning): graph kernel-arg retention cannot be detected completely without traversing argument objects, and an incomplete warning would fire on correct single-threaded PTDS usage.

Related

Record a DeallocationStream at device-pointer creation so default-stream tokens pin the allocation context (and PTDS the allocating thread) instead of relying on ambient state at free time.
Make the deallocation stream's context current around free/unmap/MR cleanup so destruction no longer depends on ambient CUDA context, and wire cuCtxSetCurrent into the resource-handles driver table.
Add keyword-only stream= on Buffer/ManagedBuffer.from_handle when mr owns the pointer, bind it at construction, and cover teardown with no or foreign current context.
Stop treating CUDA_ERROR_INVALID_CONTEXT as a successful pool free, and let
explicit mr.deallocate() raise; destruction still contains errors in the
callback. Document PTDS deallocation ordering on the stream parameters and
note the context-safe Buffer teardown fix in the 1.2.0 release notes.
@Andy-Jost Andy-Jost added this to the cuda.core 1.2.0 milestone Aug 6, 2026
@Andy-Jost Andy-Jost added bug Something isn't working P0 High priority - Must do! cuda.core Everything related to the cuda.core module labels Aug 6, 2026
@Andy-Jost Andy-Jost self-assigned this Aug 6, 2026
@Andy-Jost
Andy-Jost requested review from juenglin and mdboom August 6, 2026 22:11
Require default deallocation streams to bind a current context at creation so teardown never relies on an ambiguous ambient token. Expand coverage and documentation for context-independent cleanup and failure reporting.
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown

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

Labels

bug Something isn't working cuda.core Everything related to the cuda.core module P0 High priority - Must do!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Buffer cleanup must capture a complete deallocation recipe at creation

1 participant