Skip to content

Conversation

@kingcrimsontianyu
Copy link
Contributor

#893 contains a tricky bug. The following is incorrect:

def stream_register(stream: int, flags: int) -> None:
    cdef CUstream cpp_stream = <CUstream>stream

stream is a Python object. The cast applies to the address of this Python object, instead of the underlying integer value. This surprisingly does not trigger a compile error.

There are two ways to fix this bug:

  • Method 1: double casting. The additional cast to uintptr_t extracts the underlying integer value of stream correctly.
def stream_register(stream: int, flags: int) -> None:
    cdef CUstream cpp_stream = <CUstream><uintptr_t>stream
  • Method 2: uintptr_t as type hint (used in this PR). uintptr_t is not just a type hint; it actually causes stream to be a C object.
def stream_register(stream: uintptr_t, flags: int) -> None:
    cdef CUstream cpp_stream = <CUstream>stream

For both method, uintptr_t must be cimport-ed into the source file.

@copy-pr-bot
Copy link

copy-pr-bot bot commented Jan 20, 2026

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@kingcrimsontianyu kingcrimsontianyu added bug Something isn't working non-breaking Introduces a non-breaking change python Affects the Python API of KvikIO labels Jan 20, 2026
@kingcrimsontianyu kingcrimsontianyu marked this pull request as ready for review January 20, 2026 16:04
@kingcrimsontianyu kingcrimsontianyu requested a review from a team as a code owner January 20, 2026 16:04
@kingcrimsontianyu kingcrimsontianyu moved this to Burndown in libcudf Jan 20, 2026
@kingcrimsontianyu kingcrimsontianyu self-assigned this Jan 20, 2026
@kingcrimsontianyu
Copy link
Contributor Author

/merge

@rapids-bot rapids-bot bot merged commit 01011eb into rapidsai:main Jan 20, 2026
77 checks passed
@mhaseeb123 mhaseeb123 moved this from Burndown to Landed in libcudf Jan 20, 2026
gforsyth pushed a commit to gforsyth/kvikio that referenced this pull request Jan 21, 2026
rapidsai#893 contains a tricky bug. The following is incorrect:

```python
def stream_register(stream: int, flags: int) -> None:
    cdef CUstream cpp_stream = <CUstream>stream
```
`stream` is a Python object. The cast applies to the address of this Python object, instead of the underlying integer value. This surprisingly does not trigger a compile error.

There are two ways to fix this bug:
- Method 1: double casting. The additional cast to `uintptr_t` extracts the underlying integer value of `stream` correctly.
```python
def stream_register(stream: int, flags: int) -> None:
    cdef CUstream cpp_stream = <CUstream><uintptr_t>stream
```

- Method 2: `uintptr_t` as type hint (used in this PR). `uintptr_t` is not just a type hint; it actually causes `stream` to be a C object.
```python
def stream_register(stream: uintptr_t, flags: int) -> None:
    cdef CUstream cpp_stream = <CUstream>stream
```
For both method, `uintptr_t` must be `cimport`-ed into the source file.

Authors:
  - Tianyu Liu (https://github.com/kingcrimsontianyu)

Approvers:
  - Lawrence Mitchell (https://github.com/wence-)

URL: rapidsai#904
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 python Affects the Python API of KvikIO

Projects

Status: Landed

Development

Successfully merging this pull request may close these issues.

2 participants