Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add critical section guards to thread-sensitive accessors
Protect accessors with cython critical sections so free-threaded execution
avoids races when reading cached state.

At least one of these was noticed by free-threaded test runs.  I am not
certain that they will guarantee identity returns, but pretty sure this
will make them thread-safe.
(The reason being that at least on non free-threaded builds they may
release the GIL or, on free-threaded, do API calls that release the
critical section temporarily.)

About the `.pyi` fixes: I think these are incorrect but `cython`
is already used in other places and shouldn't be a runtime/typing requirement
at all.
  • Loading branch information
seberg committed Jun 15, 2026
commit 652a3e7480bc58cd07907080088af4cf2e563489
2 changes: 2 additions & 0 deletions cuda_core/cuda/core/_memory/_buffer.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import cython
from cuda.core._memory._device_memory_resource import DeviceMemoryResource
from cuda.core._memory._ipc import IPCBufferDescriptor
from cuda.core._memory._pinned_memory_resource import PinnedMemoryResource
Expand Down Expand Up @@ -88,6 +89,7 @@ class Buffer:
"""

@property
@cython.critical_section
def ipc_descriptor(self) -> IPCBufferDescriptor:
"""Descriptor for sharing this buffer with other processes."""

Expand Down
2 changes: 2 additions & 0 deletions cuda_core/cuda/core/_memory/_buffer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ cdef class Buffer:
return _ipc.Buffer_from_ipc_descriptor(cls, mr, ipc_descriptor, stream)

@property
@cython.critical_section
def ipc_descriptor(self) -> IPCBufferDescriptor:
"""Descriptor for sharing this buffer with other processes."""
if self._ipc_data is None:
Expand Down Expand Up @@ -445,6 +446,7 @@ cdef class Buffer:

# Memory Attribute Query Helpers
# ------------------------------
@cython.critical_section
Comment thread
seberg marked this conversation as resolved.
Resolved
cdef inline void _init_mem_attrs(Buffer self):
"""Initialize memory attributes by querying the pointer."""
if not self._mem_attrs_inited:
Expand Down
2 changes: 2 additions & 0 deletions cuda_core/cuda/core/_memory/_memory_pool.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from __future__ import annotations

import uuid

import cython
from cuda.core._memory._buffer import Buffer, MemoryResource
from cuda.core._stream import Stream
from cuda.core.graph import GraphBuilder
Expand Down Expand Up @@ -97,6 +98,7 @@ class _MemPool(MemoryResource):
"""

@property
@cython.critical_section
def attributes(self) -> _MemPoolAttributes:
"""Memory pool attributes."""

Expand Down
2 changes: 2 additions & 0 deletions cuda_core/cuda/core/_memory/_memory_pool.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

cimport cython
from libc.limits cimport ULLONG_MAX
from libc.stdint cimport uintptr_t
from libc.string cimport memset
Expand Down Expand Up @@ -177,6 +178,7 @@ cdef class _MemPool(MemoryResource):
_MP_deallocate(self, <uintptr_t>ptr, size, s)

@property
@cython.critical_section
def attributes(self) -> _MemPoolAttributes:
"""Memory pool attributes."""
if self._attributes is None:
Expand Down
4 changes: 4 additions & 0 deletions cuda_core/cuda/core/_memoryview.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

cimport cython
from ._dlpack cimport *
from ._dlpack import classify_dl_device
from libc.stdint cimport intptr_t
Expand Down Expand Up @@ -539,6 +540,7 @@ cdef class StridedMemoryView:
+ f" readonly={self.readonly},\n"
+ f" exporting_obj={get_simple_repr(self.exporting_obj)})")

@cython.critical_section
cdef inline _StridedLayout get_layout(self):
if self._layout is None:
if self.dl_tensor:
Expand All @@ -549,6 +551,7 @@ cdef class StridedMemoryView:
raise ValueError("Cannot infer layout from the exporting object")
return self._layout

@cython.critical_section
cdef inline object get_buffer(self):
"""
Returns Buffer instance with the underlying data.
Expand All @@ -562,6 +565,7 @@ cdef class StridedMemoryView:
self._buffer = Buffer.from_handle(self.ptr, 0, owner=self.exporting_obj)
return self._buffer

@cython.critical_section
cdef inline object get_dtype(self):
if self._dtype is None:
if self.dl_tensor != NULL:
Expand Down
3 changes: 3 additions & 0 deletions cuda_core/cuda/core/_module.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from __future__ import annotations
from collections import namedtuple
from os import PathLike

import cython
from cuda.core._device import Device
from cuda.core._launch_config import LaunchConfig
from cuda.core._stream import Stream
Expand Down Expand Up @@ -253,6 +254,7 @@ class Kernel:
...

@property
@cython.critical_section
def attributes(self) -> KernelAttributes:
"""Get the read-only attributes of this kernel."""

Expand All @@ -265,6 +267,7 @@ class Kernel:
"""list[ParamInfo]: (offset, size) for each argument of this function"""

@property
@cython.critical_section
def occupancy(self) -> KernelOccupancy:
"""Get the occupancy information for launching this kernel."""

Expand Down
4 changes: 4 additions & 0 deletions cuda_core/cuda/core/_module.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

cimport cython
from libc.stddef cimport size_t

from collections import namedtuple
Expand Down Expand Up @@ -454,6 +455,7 @@ cdef class Kernel:
return ker

@property
@cython.critical_section
def attributes(self) -> KernelAttributes:
"""Get the read-only attributes of this kernel."""
if self._attributes is None:
Expand Down Expand Up @@ -501,6 +503,7 @@ cdef class Kernel:
return param_info

@property
@cython.critical_section
def occupancy(self) -> KernelOccupancy:
"""Get the occupancy information for launching this kernel."""
if self._occupancy is None:
Expand Down Expand Up @@ -742,6 +745,7 @@ cdef class ObjectCode:

# TODO: do we want to unload in a finalizer? Probably not..

@cython.critical_section
cdef int _lazy_load_module(self) except -1:
if self._h_library:
return 0
Expand Down
Loading