Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cuda_core/cuda/core/_memory/_ipc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ cdef _MemPool MP_from_allocation_handle(cls, alloc_handle):
# Register it.
if uuid is not None:
registered = self.register(uuid)
assert registered is self
if registered is not self:
raise RuntimeError(
"Internal error: register() returned a different memory "
"resource than the one being registered"
)

return self

Expand All @@ -292,7 +296,11 @@ cdef _MemPool MP_register(_MemPool self, uuid):
return existing
if not self.is_ipc_enabled:
raise RuntimeError("Memory resource is not IPC-enabled")
assert self.uuid is None or self.uuid == uuid
if self.uuid is not None and self.uuid != uuid:
raise ValueError(
f"Cannot register memory resource with UUID {uuid}: "
f"the resource already has UUID {self.uuid}"
)
registry[uuid] = self
self._ipc_data._alloc_handle._uuid = uuid
return self
Expand Down
28 changes: 28 additions & 0 deletions cuda_core/tests/memory_ipc/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ def test_register_rejects_non_ipc_memory_resource(mempool_device):
DeviceMemoryResource.from_registry(key)


@pytest.mark.human_authored
def test_register_rejects_mismatched_uuid(ipc_memory_resource):
"""register() rejects a UUID that does not match the resource's own.

The check is an explicit ValueError (not a bare ``assert``) so it remains
enforced even when CPython runs with ``-O``, where assertions are removed
(see #2697). Registering under a foreign key would otherwise rewrite the
resource's UUID and silently corrupt the IPC registry.
"""
mr = ipc_memory_resource
assert mr.is_ipc_enabled

# The resource must already carry its own UUID (assigned on creation).
assert mr.uuid is not None
other = uuid.uuid4()
while other == mr.uuid:
other = uuid.uuid4()

with pytest.raises(ValueError, match="already has UUID"):
mr.register(other)

# A rejected registration must not rewrite the resource's own UUID.
assert mr.uuid != other

# Registering under the resource's own UUID still succeeds.
assert mr.register(mr.uuid) is mr


@pytest.mark.skipif(os.name == "nt", reason="IPC allocation handles are not supported on Windows")
@pytest.mark.agent_authored(model="gpt-5.6")
def test_ipc_allocation_handle_state_tracks_close():
Expand Down
Loading