Repository navigation
Pool-backed MemoryResource buffers bypass overridden deallocate() methods #2615
Description
Summary
Buffers allocated by pool-backed memory resources are freed directly by their C++ DevicePtrHandle deleter. Their associated memory resource's deallocate() method is never called.
This makes deallocation behavior inconsistent and prevents subclasses of DeviceMemoryResource, PinnedMemoryResource, and ManagedMemoryResource from observing or customizing teardown.
Reproducer
from cuda.core import Device, DeviceMemoryResource
calls = []
class RecordingMemoryResource(DeviceMemoryResource):
def deallocate(self, ptr, size, *, stream):
calls.append((ptr, size, stream))
return super().deallocate(ptr, size, stream=stream)
device = Device()
device.set_current()
mr = RecordingMemoryResource(device)
buf = mr.allocate(1024, stream=device.default_stream)
assert buf.memory_resource is mr
buf.close()
assert len(calls) == 1 # Fails: calls is emptyThe allocation is freed, but directly through the C++ handle rather than through RecordingMemoryResource.deallocate().
Inconsistent behavior
The behavior depends on how the Buffer was constructed:
mr.allocate(...)uses a directly owning C++ handle and bypassesmr.deallocate().Buffer.from_handle(..., mr=mr)records the MR as the owner and dispatches teardown throughmr.deallocate().
Consequently, the same memory resource subclass has two different deallocation models.
Callback-backed resources such as LegacyPinnedMemoryResource also dispatch through mr.deallocate(), making the behavior inconsistent between built-in resource implementations.
Expected behavior
If concrete memory resources are supported as subclass bases, an overridden deallocate() should be honored for buffers allocated by the subclass.
This is useful for accounting, tracing, validation, failure injection, and custom resource policies—not only test instrumentation.
If concrete memory resources are intentionally not subclassable, that restriction should instead be explicit and enforced.
Implementation considerations
The current direct C++ path has important properties that should be preserved:
- It retains the memory-pool handle for the allocation lifetime.
- It can free without executing Python during interpreter shutdown.
- It stores the stream/context recipe needed for safe deferred teardown.
A fix may therefore require a callback-capable pool allocation handle that retains the pool while dispatching through the Python MR, or a fast path for exact built-in types with callback dispatch for subclasses.
GraphMemoryResource, which also creates directly owning handles, should be audited for the same behavior.
Related
- Inconsistent lifetime management across
from_handle/from_*APIs #1989 — inconsistent lifetime management acrossfrom_handle/from_*APIs - cuda.core: capture complete Buffer deallocation recipe at creation #2526 — capture complete Buffer deallocation recipe at creation
Activity
atiaomar1978-hub commentedon Aug 13, 2026
Opened PR #2620 for this issue.
The fix uses MR-owned device pointer handles for subclasses so overridden
deallocate()is called consistently withBuffer.from_handle(mr=...). Built-in pool/graph MR types retain the direct C++ deleter for shutdown-safe nogil frees.Includes regression tests for close/GC/stream forwarding on pool-backed subclasses.
atiaomar1978-hub commentedon Aug 13, 2026
Hi @Andy-Jost — I've opened PR #2620 for this issue.
When you have a moment, could a maintainer please:
cuda.core nextmilestone on the PRI don't have upstream permissions to set assignee/milestone on NVIDIA/cuda-python PRs. The
cuda.corelabel is already applied.Happy to adjust based on your feedback. Thanks!
docs(cuda.core): document subclass deallocate routing (NVIDIA#2615)
atiaomar1978-hub commentedon Aug 14, 2026
Validation update (GPU tests on RunPod)
I validated the fix in PR #2620 on real NVIDIA hardware outside CI.
Setup: RTX 4000 Ada (RunPod community), branch
fix/pool-backed-mr-deallocate-2615, editablecuda_corebuild against CUDA 13 bindings/headers.Targeted run:
pytest cuda_core/tests/test_memory.py \ -k "pool_backed_mr or dmr_deallocate_frees_pool_pointer or dmr_from_handle_deallocate or deallocate" -vOutcome: 11 passed, 2 skipped, 0 failed
The pool-backed MR tests confirm that overridden
deallocate()is now invoked on close/GC and receives the allocation stream, and that pool pointers are freed through the MR path (test_dmr_deallocate_frees_pool_pointer,test_dmr_from_handle_deallocate_frees_pool_pointer).This addresses the core issue described here: pool-backed buffers no longer bypass Python
deallocate()overrides.Full details: #2620 (comment)
chore: regenerate _buffer.pyi stubs (NVIDIA#2615)