@@ -252,7 +252,7 @@ cdef class Buffer:
252252 # The parent process's stream is not portable across processes, so the
253253 # pickle path cannot thread an explicit stream through. Seed the
254254 # imported buffer's deallocation with the current context's default
255- # stream; the receiver can override via buffer. close(stream) .
255+ # stream; the receiver can override it before or during close.
256256 return Buffer.from_ipc_descriptor(mr, ipc_descriptor, stream = default_stream())
257257
258258 def __reduce__ (self ) -> tuple[object , ...]:
@@ -347,9 +347,45 @@ cdef class Buffer:
347347 stream : :obj:`~_stream.Stream` | :obj:`~graph.GraphBuilder`, optional
348348 The stream object to use for asynchronous deallocation. If None ,
349349 the deallocation stream stored in the handle is used.
350+
351+ See Also
352+ --------
353+ set_deallocation_stream
354+ Change the deallocation stream without closing the buffer.
350355 """
351356 Buffer_close(self , stream )
352357
358+ def set_deallocation_stream(self , stream: Stream | GraphBuilder ) -> None:
359+ """Change the stream that orders this buffer's eventual deallocation.
360+
361+ The buffer remains open and usable. A later :meth:`close` without a
362+ stream , garbage collection , or release of the final retained device
363+ pointer handle uses the replacement stream.
364+
365+ This method does not synchronize streams or establish dependencies.
366+ The caller must ensure that allocation and all accesses are ordered
367+ before the deallocation on ``stream``.
368+
369+ Parameters
370+ ----------
371+ stream : :obj:`~_stream.Stream` | :obj:`~graph.GraphBuilder`
372+ The stream to use for eventual asynchronous deallocation.
373+
374+ Raises
375+ ------
376+ RuntimeError
377+ If the buffer is already closed , or if a default-stream token
378+ cannot be bound because no CUDA context is current.
379+ TypeError
380+ If ``stream`` is ``None`` or is not an accepted stream object.
381+
382+ Notes
383+ -----
384+ Synchronizing concurrent mutation and destruction of the same buffer
385+ is the caller's responsibility.
386+ """
387+ Buffer_set_deallocation_stream(self , stream )
388+
353389 def __enter__(self ):
354390 return self
355391
@@ -707,15 +743,21 @@ cdef tuple Buffer_coerce_batch(object buffers, str what, str single_hint):
707743 return tuple (out)
708744
709745
746+ cdef inline void Buffer_set_deallocation_stream(Buffer self , object stream):
747+ """ Validate and replace a live buffer's deallocation recipe."""
748+ if not self ._h_ptr:
749+ raise RuntimeError (" Cannot set the deallocation stream on a closed Buffer" )
750+ cdef Stream s = Stream_accept(stream)
751+ _apply_deallocation_stream(self ._h_ptr, s._h_stream)
752+
753+
710754cdef inline void Buffer_close(Buffer self , object stream):
711755 """ Close a buffer, freeing its memory."""
712- cdef Stream s
713756 if not self ._h_ptr:
714757 return
715758 # Update deallocation stream if provided
716759 if stream is not None :
717- s = Stream_accept(stream)
718- _apply_deallocation_stream(self ._h_ptr, s._h_stream)
760+ Buffer_set_deallocation_stream(self , stream)
719761 # Reset handle - RAII deleter will free the memory (and release owner ref in C++)
720762 self ._h_ptr.reset()
721763 self ._size = 0
0 commit comments