cuda.core: allow updating Buffer deallocation streams - #2602
Open
Andy-Jost wants to merge 9 commits into
Open
Conversation
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.
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.
Ensure spawned children can bind the imported buffer's default deallocation stream before their process target starts.
|
juenglin
reviewed
Aug 13, 2026
| cdef Stream s | ||
| if not self._h_ptr: | ||
| raise RuntimeError("Cannot set the deallocation stream on a closed Buffer") | ||
| s = Stream_accept(stream) |
Contributor
There was a problem hiding this comment.
Suggested change
| s = Stream_accept(stream) | |
| cdef Stream s = Stream_accept(stream) |
| A :class:`Buffer` records the stream that will order its eventual deallocation. | ||
| Use :meth:`Buffer.set_deallocation_stream` to replace that stream without | ||
| closing the buffer. Changing the recorded stream does not synchronize streams; | ||
| the caller must order allocation and every access before the deallocation, |
Contributor
There was a problem hiding this comment.
Could you add a code example for cuda.core users that demonstrates how to safely switch deallocation streams?
| stream = Stream._from_handle(Stream, h_stream) if h_stream else default_stream() | ||
| if not h_stream: | ||
| print( | ||
| "Warning: no deallocation stream was recorded; falling back to " |
Contributor
There was a problem hiding this comment.
Should it say "no deallocation stream was recorded or the deallocation stream was closed"?
| """Validate and replace a live buffer's deallocation recipe.""" | ||
| cdef Stream s | ||
| if not self._h_ptr: | ||
| raise RuntimeError("Cannot set the deallocation stream on a closed Buffer") |
Contributor
There was a problem hiding this comment.
Should there be a similar runtime error for a closed stream?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Important
Please hold review until #2526 merges. This PR is based on that work and will be rebased onto
mainafterward.Summary
Closes #2600.
Add
Buffer.set_deallocation_streamso callers can replace the stream that orders eventual deallocation without closing the buffer. This supports transferring an allocation's lifetime to another stream while preserving the existingBufferobject.Changes
Buffer.set_deallocation_streamAPI and generated type stub.DeallocationStreamcapture path.Related Work
mainafter that PR merges.