Skip to content

Commit 0d5f08b

Browse files
committed
Restore previous behavior for DMR.from_allocation_handle when passed a file descriptor (caller closes the fd).
1 parent 0fac800 commit 0d5f08b

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

cuda_core/cuda/core/experimental/_memory/_dmr.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ cdef class DeviceMemoryResource(MemoryResource):
276276
resource is created.
277277

278278
alloc_handle : int | IPCAllocationHandle
279-
The shareable handle of the device memory resource to import.
279+
The shareable handle of the device memory resource to import. If an
280+
integer is supplied, it must represent a valid platform-specific
281+
handle. It is the caller's responsibility to close that handle.
280282

281283
Returns
282284
-------

cuda_core/cuda/core/experimental/_memory/_ipc.pyx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,21 @@ cdef Buffer Buffer_from_ipc_descriptor(
180180

181181
cdef DeviceMemoryResource DMR_from_allocation_handle(cls, device_id, alloc_handle):
182182
# Quick exit for registry hits.
183-
if isinstance(alloc_handle, int):
184-
alloc_handle = IPCAllocationHandle._init(alloc_handle, None)
185183
uuid = getattr(alloc_handle, 'uuid', None) # no-cython-lint
186184
mr = registry.get(uuid)
187185
if mr is not None:
188186
return mr
189187

188+
# Ensure we have an allocation handle. Duplicate the file descriptor, if
189+
# necessary.
190+
if isinstance(alloc_handle, int):
191+
fd = os.dup(alloc_handle)
192+
try:
193+
alloc_handle = IPCAllocationHandle._init(fd, None)
194+
except:
195+
os.close(fd)
196+
raise
197+
190198
# Construct a new DMR.
191199
cdef DeviceMemoryResource self = DeviceMemoryResource.__new__(cls)
192200
self._dev_id = getattr(device_id, 'device_id', device_id)
@@ -205,7 +213,7 @@ cdef DeviceMemoryResource DMR_from_allocation_handle(cls, device_id, alloc_handl
205213
registered = self.register(uuid)
206214
assert registered is self
207215

208-
# Always close the file handle (caller can dup it, if needed).
216+
# Always close the file handle.
209217
alloc_handle.close()
210218

211219
return self

cuda_core/tests/memory_ipc/test_serialize.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import multiprocessing as mp
55
import multiprocessing.reduction
6+
import os
67

78
from cuda.core.experimental import Buffer, Device, DeviceMemoryResource
89
from helpers.buffers import PatternGen
@@ -59,6 +60,7 @@ def child_main(self, conn):
5960
# Receive the memory resource.
6061
handle = mp.reduction.recv_handle(conn)
6162
mr = DeviceMemoryResource.from_allocation_handle(device, handle)
63+
os.close(handle)
6264

6365
# Receive the buffers.
6466
buffer1 = conn.recv() # directly

0 commit comments

Comments
 (0)