Skip to content

Fix Buffer.from_handle(mr=...) not calling mr.deallocate()#1625

Open
Andy-Jost wants to merge 1 commit intoNVIDIA:mainfrom
Andy-Jost:fix-buffer-from-handle-dealloc
Open

Fix Buffer.from_handle(mr=...) not calling mr.deallocate()#1625
Andy-Jost wants to merge 1 commit intoNVIDIA:mainfrom
Andy-Jost:fix-buffer-from-handle-dealloc

Conversation

@Andy-Jost
Copy link
Contributor

@Andy-Jost Andy-Jost commented Feb 13, 2026

Summary

  • Fixes [BUG]: Buffer.from_handle does not respect custom MemoryResource deallocation #1619: Buffer.from_handle(ptr, size, mr=mr) now calls mr.deallocate(ptr, size, stream) when the buffer is closed or garbage collected, restoring the pre-RAII behavior.
  • Adds deviceptr_create_with_mr() to the resource handle infrastructure, whose shared_ptr deleter invokes a registered callback with the pointer, size, and deallocation stream.
  • The callback is implemented in Cython and converts the raw stream handle to a cuda.core.Stream before calling mr.deallocate().
  • Removes the unused stream parameter from Buffer._init.

Closes #1619

Changes

  • resource_handles.hpp/.cpp: Add MRDeallocCallback type, register_mr_dealloc_callback(), and deviceptr_create_with_mr().
  • _resource_handles.pxd/.pyx: Expose the new types and functions to Cython.
  • _buffer.pyx: Register the deallocation callback at module init; use deviceptr_create_with_mr when mr is provided in Buffer._init; update docstrings.
  • _legacy.py: Remove unused stream argument from Buffer._init calls.
  • tests/helpers/buffers.py: Add TrackingMR test helper.
  • tests/test_memory.py: Add test_mr_deallocate_called_on_close and test_mr_deallocate_called_on_gc.

Test Plan

  • New tests verify mr.deallocate() is called on explicit close() and on GC
  • All existing memory and object protocol tests pass (338 passed, 15 skipped)
  • CI

Made with Cursor

The RAII resource handle migration broke the contract where
Buffer.from_handle(mr=mr) calls mr.deallocate() on close or GC.
Add deviceptr_create_with_mr() which invokes a registered callback
at destruction time, passing the deallocation stream from the handle.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Andy-Jost Andy-Jost added this to the cuda.core v0.6.0 milestone Feb 13, 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 Feb 13, 2026
@Andy-Jost Andy-Jost self-assigned this Feb 13, 2026
@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Feb 13, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@Andy-Jost
Copy link
Contributor Author

/ok to test c7fdab7

@Andy-Jost Andy-Jost requested a review from leofang February 13, 2026 21:42
@github-actions
Copy link

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.from_handle does not respect custom MemoryResource deallocation

1 participant