From 460c3122957760e44975c2d75e6a3256f22f846e Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Tue, 23 Jun 2026 17:07:23 +0800 Subject: [PATCH 1/2] 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 --- python/cuvs/cuvs/common/mg_resources.pyx | 12 +++++++----- python/cuvs/cuvs/common/resources.pyx | 9 +++++---- python/cuvs/cuvs/tests/test_resources.py | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 python/cuvs/cuvs/tests/test_resources.py diff --git a/python/cuvs/cuvs/common/mg_resources.pyx b/python/cuvs/cuvs/common/mg_resources.pyx index 4b9e65df61..6aa3b8ab36 100644 --- a/python/cuvs/cuvs/common/mg_resources.pyx +++ b/python/cuvs/cuvs/common/mg_resources.pyx @@ -1,12 +1,13 @@ # -# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. +# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION. # SPDX-License-Identifier: Apache-2.0 # # cython: language_level=3 -from cuda.bindings.cyruntime cimport cudaStream_t +from libc.stdint cimport uintptr_t from cuvs.common.c_api cimport ( + cudaStream_t, cuvsMultiGpuResourcesCreate, cuvsMultiGpuResourcesCreateWithDeviceIds, cuvsMultiGpuResourcesDestroy, @@ -84,11 +85,12 @@ cdef class MultiGpuResources: else: check_cuvs(cuvsMultiGpuResourcesCreate(&self.c_obj)) - if stream: - check_cuvs(cuvsStreamSet(self.c_obj, stream)) + if stream is not None: + check_cuvs(cuvsStreamSet( + self.c_obj, stream)) def sync(self): - check_cuvs(cuvsStreamSync(self.c_obj)) + check_cuvs(cuvsStreamSync(self.c_obj)) def set_memory_pool(self, percent_of_free_memory): """ diff --git a/python/cuvs/cuvs/common/resources.pyx b/python/cuvs/cuvs/common/resources.pyx index cf6f3284a6..9b1c129485 100644 --- a/python/cuvs/cuvs/common/resources.pyx +++ b/python/cuvs/cuvs/common/resources.pyx @@ -1,14 +1,15 @@ # -# SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION. +# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. # SPDX-License-Identifier: Apache-2.0 # # cython: language_level=3 import functools -from cuda.bindings.cyruntime cimport cudaStream_t +from libc.stdint cimport uintptr_t from cuvs.common.c_api cimport ( + cudaStream_t, cuvsResources_t, cuvsResourcesCreate, cuvsResourcesDestroy, @@ -54,8 +55,8 @@ cdef class Resources: def __cinit__(self, stream=None): check_cuvs(cuvsResourcesCreate(&self.c_obj)) - if stream: - check_cuvs(cuvsStreamSet(self.c_obj, stream)) + if stream is not None: + check_cuvs(cuvsStreamSet(self.c_obj, stream)) def sync(self): check_cuvs(cuvsStreamSync(self.c_obj)) diff --git a/python/cuvs/cuvs/tests/test_resources.py b/python/cuvs/cuvs/tests/test_resources.py new file mode 100644 index 0000000000..6e722b7e08 --- /dev/null +++ b/python/cuvs/cuvs/tests/test_resources.py @@ -0,0 +1,17 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. +# SPDX-License-Identifier: Apache-2.0 + +import gc + +import cupy as cp +import cuvs.common.resources as resources_mod + +from cuvs.common import Resources + + +def test_resources_syncs_cupy_stream_pointer(): + # gh-issue: 1836 should not segfault when syncing a stream pointer from cupy + stream = cp.cuda.Stream() + resources = Resources(stream=stream.ptr) + + resources.sync() From 6381ff1c298107e0c3fdd24b37b388362596bce0 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Tue, 23 Jun 2026 17:32:30 +0800 Subject: [PATCH 2/2] fix: pre-commit Signed-off-by: yihong0618 --- python/cuvs/cuvs/tests/test_resources.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/cuvs/cuvs/tests/test_resources.py b/python/cuvs/cuvs/tests/test_resources.py index 6e722b7e08..1dc5d8547c 100644 --- a/python/cuvs/cuvs/tests/test_resources.py +++ b/python/cuvs/cuvs/tests/test_resources.py @@ -1,10 +1,8 @@ # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. # SPDX-License-Identifier: Apache-2.0 -import gc import cupy as cp -import cuvs.common.resources as resources_mod from cuvs.common import Resources