Skip to content

Fix stream handle cast in Resources and MultiGpuResources - #2946

Merged
rapids-bot[bot] merged 3 commits into
NVIDIA:mainfrom
ViderStar:fix-resources-stream-cast
Oct 30, 2026
Merged

rapids-bot[bot] merged 3 commits into
NVIDIA:mainfrom
ViderStar:fix-resources-stream-cast

Conversation

@ViderStar

Copy link
Copy Markdown
Contributor

Closes #1863

Resources(stream=...) and MultiGpuResources(stream=...) take the stream as an integer handle (the Resources docstring passes cupy.cuda.Stream().ptr). Both called

cuvsStreamSet(self.c_obj, <cudaStream_t>stream)

where stream is a Python object. Cython casts a Python object to a pointer type by taking the object's own address, so cuVS got the address of the Python int as the stream. The first call that used the stream then crashed. In the issue that call is res.sync().

The fix converts the value through uintptr_t first (<cudaStream_t><uintptr_t>stream) in both classes.

Tests: added python/cuvs/cuvs/tests/test_resources.py. It creates Resources from a CuPy stream handle, syncs it, and runs a small brute-force build and search on that handle.

How I tested (RTX 5070 Ti, CUDA 13 driver): I installed the cuvs-cu13 26.10 wheels, applied this change to their resources.pyx and mg_resources.pyx, and rebuilt the two modules with Cython against libcuvs_c. The pyx change is the same as on main. I could not build main's Python package against the 26.10 C library, because main uses newer C API functions.

  • Unmodified 26.10 modules: the issue's reproducer and the new test both segfault.
  • With the fix: the reproducer passes for both Resources and MultiGpuResources (single GPU), and test_resources.py, test_memory_tracking.py and test_brute_force.py all pass.

Resources(stream=...) takes the stream as an integer handle, for example
cupy.cuda.Stream().ptr, but cast the Python object directly to
cudaStream_t. Cython then passes the address of the Python int object
instead of its value, so the handle got an invalid stream and sync()
segfaulted. Convert through uintptr_t first.

Signed-off-by: Artsem Lebiadzevich <artyomlebedevich@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Oct 25, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ViderStar

Copy link
Copy Markdown
Contributor Author

Could a maintainer add the bug and non-breaking labels? I can't set labels from a fork.

@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration
  • Source mode: Auto
  • Repository: NVIDIA/cuvs
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 95601f8
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: bad83981-5a75-40a5-a21e-4a93c0ef892d
Commits

Reviewing files that changed from the base of the PR and between 04e1b6e and aa394dd.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: NVIDIA/cuvs
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 244f118
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: dcfc1602-89de-48dc-939e-a8891adf4c1a
Commits

Reviewing files that changed from the base of the PR and between 244f118 and d4f2dc4.

Files selected for processing: 3
  • python/cuvs/cuvs/common/mg_resources.pyx
  • python/cuvs/cuvs/common/resources.pyx
  • python/cuvs/cuvs/tests/test_resources.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 9 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved compatibility with externally provided GPU streams, including streams created by other Python GPU libraries. Index creation and search operations can now use these streams more reliably, while preserving the existing behavior when no stream is specified.
    • Added coverage confirming that nearest-neighbor search returns the expected results when run using an external stream.

The Python Resources and MultiGpuResources constructors now convert truthy stream handles through uintptr_t before passing them to cuvsStreamSet. A test exercises Resources with a CuPy stream during brute-force index construction and search.

Changes: Python resource streams

Layer / File(s) Summary
Stream conversion and external-stream test
python/cuvs/cuvs/common/mg_resources.pyx, python/cuvs/cuvs/common/resources.pyx, python/cuvs/cuvs/tests/test_resources.py
Both constructors convert truthy stream handles through uintptr_t before casting them to cudaStream_t. The test synchronizes a Resources instance created with a CuPy stream, builds and searches a brute-force index, and checks the returned neighbors.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: bug, non-breaking

Suggested reviewers: viclafargue, benfred, achirkin

Merge Risk: Minimal · previous review d4f2d

The reported stream-handle crash is addressed in both resource classes, with targeted external-stream coverage added.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title names the main change: fixing the stream handle cast in the Resources and MultiGpuResources classes. The raw summary and objectives both describe this cast fix as the primary change, so the …
Description check Passed The description states the bug (the Python object address was passed instead of the integer handle value), the fix (converting through uintptr_t in both classes), the added test, and the testing perfo…
Linked Issues check Passed Issue #1863 requires Resources(stream=stream.ptr) to synchronize without a segmentation fault. resources.pyx now converts the Python handle through uintptr_t before casting it to cudaStream_t.…
Out of Scope Changes check Passed The changes are limited to the related stream-handle conversion in Resources and MultiGpuResources, plus an automated regression test. The MultiGpuResources fix applies the same defect correctio…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (2 skipped: 2 …
Full details: Title check

Explanation

The title names the main change: fixing the stream handle cast in the Resources and MultiGpuResources classes. The raw summary and objectives both describe this cast fix as the primary change, so the title is specific and on-topic.

Full details: Description check

Explanation

The description states the bug (the Python object address was passed instead of the integer handle value), the fix (converting through uintptr_t in both classes), the added test, and the testing performed. It directly matches the changeset and linked issue #1863.

Full details: Linked Issues check

Explanation

Issue #1863 requires Resources(stream=stream.ptr) to synchronize without a segmentation fault. resources.pyx now converts the Python handle through uintptr_t before casting it to cudaStream_t. The new test_resources_with_external_stream creates a CuPy stream, constructs Resources with stream.ptr, calls res.sync(), and uses the resource for brute-force operations before synchronizing again.

Full details: Out of Scope Changes check

Explanation

The changes are limited to the related stream-handle conversion in Resources and MultiGpuResources, plus an automated regression test. The MultiGpuResources fix applies the same defect correction to the related class named in the PR intent. The test directly exercises the linked issue behavior and stream use.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (2 skipped: 2 unsupported.)

Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

…m-cast

Signed-off-by: Artsem Lebiadzevich <artyomlebedevich@gmail.com>
@ViderStar

Copy link
Copy Markdown
Contributor Author

@viclafargue @cjnolet gentle ping: could one of you review this when you have time? Passing a CUDA stream to the resources wrapper can segfault. I've merged the latest main. It also still needs the bug and non-breaking labels for the label checker.

@viclafargue
viclafargue self-requested a review October 28, 2026 17:36
@viclafargue viclafargue added bug Something isn't working non-breaking Introduces a non-breaking change labels Oct 28, 2026
@viclafargue

Copy link
Copy Markdown
Contributor

/ok to test 04e1b6e

@viclafargue viclafargue left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for fixing the stream handle cast in both resource classes and adding a regression test.

@viclafargue

Copy link
Copy Markdown
Contributor

/merge

@aamijar

aamijar commented Oct 30, 2026

Copy link
Copy Markdown
Member

/ok to test aa394dd

@aamijar aamijar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I remember opening that issue a while back

@rapids-bot
rapids-bot Bot merged commit 25c22bd into NVIDIA:main Oct 30, 2026
71 checks passed
@github-project-automation github-project-automation Bot moved this from Approved to Done in Unstructured Data Processing Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working non-breaking Introduces a non-breaking change

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG][Python] cuvs Resources segfault with explicit stream

3 participants