Skip to content

Commit 460c312

Browse files
committed
fix: close issue #1863 Fix Cython CUDA stream pointer conversion
Convert Python stream pointer integers through uintptr_t before casting to cudaStream_t. This prevents Cython from treating the int object address as the CUDA stream handle when using Resources(stream=stream.ptr) or MultiGpuResources(stream=stream.ptr). Signed-off-by: yihong0618 <zouzou0208@gmail.com>
1 parent 71306ec commit 460c312

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

python/cuvs/cuvs/common/mg_resources.pyx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#
2-
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION.
2+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION.
33
# SPDX-License-Identifier: Apache-2.0
44
#
55
# cython: language_level=3
66

7-
from cuda.bindings.cyruntime cimport cudaStream_t
7+
from libc.stdint cimport uintptr_t
88

99
from cuvs.common.c_api cimport (
10+
cudaStream_t,
1011
cuvsMultiGpuResourcesCreate,
1112
cuvsMultiGpuResourcesCreateWithDeviceIds,
1213
cuvsMultiGpuResourcesDestroy,
@@ -84,11 +85,12 @@ cdef class MultiGpuResources:
8485
else:
8586
check_cuvs(cuvsMultiGpuResourcesCreate(&self.c_obj))
8687

87-
if stream:
88-
check_cuvs(cuvsStreamSet(self.c_obj, <cudaStream_t>stream))
88+
if stream is not None:
89+
check_cuvs(cuvsStreamSet(
90+
self.c_obj, <cudaStream_t><uintptr_t>stream))
8991

9092
def sync(self):
91-
check_cuvs(cuvsStreamSync(self.c_obj))
93+
check_cuvs(cuvsStreamSync(self.c_obj))
9294

9395
def set_memory_pool(self, percent_of_free_memory):
9496
"""

python/cuvs/cuvs/common/resources.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#
2-
# SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION.
2+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
33
# SPDX-License-Identifier: Apache-2.0
44
#
55
# cython: language_level=3
66

77
import functools
88

9-
from cuda.bindings.cyruntime cimport cudaStream_t
9+
from libc.stdint cimport uintptr_t
1010

1111
from cuvs.common.c_api cimport (
12+
cudaStream_t,
1213
cuvsResources_t,
1314
cuvsResourcesCreate,
1415
cuvsResourcesDestroy,
@@ -54,8 +55,8 @@ cdef class Resources:
5455

5556
def __cinit__(self, stream=None):
5657
check_cuvs(cuvsResourcesCreate(&self.c_obj))
57-
if stream:
58-
check_cuvs(cuvsStreamSet(self.c_obj, <cudaStream_t>stream))
58+
if stream is not None:
59+
check_cuvs(cuvsStreamSet(self.c_obj, <cudaStream_t><uintptr_t>stream))
5960

6061
def sync(self):
6162
check_cuvs(cuvsStreamSync(self.c_obj))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import gc
5+
6+
import cupy as cp
7+
import cuvs.common.resources as resources_mod
8+
9+
from cuvs.common import Resources
10+
11+
12+
def test_resources_syncs_cupy_stream_pointer():
13+
# gh-issue: 1836 should not segfault when syncing a stream pointer from cupy
14+
stream = cp.cuda.Stream()
15+
resources = Resources(stream=stream.ptr)
16+
17+
resources.sync()

0 commit comments

Comments
 (0)