Skip to content

[BUG]: Stream.from_handle(0) keeps the first context it sees #3242

Open

Description

@fedonman

Is this a duplicate?

Type of Bug

Silent Failure

Component

cuda.core

Describe the bug

A Stream over the NULL stream (handle 0), from Stream.from_handle(0) or a foreign object whose __cuda_stream__ returns (0, 0), still keeps the first context it resolves. #2485 fixed this for LEGACY_DEFAULT_STREAM and PER_THREAD_DEFAULT_STREAM, but Stream_is_default_token only matches CU_STREAM_LEGACY and CU_STREAM_PER_THREAD, while the driver resolves handle 0 against the current context just like those two. After a context switch, .context, .device, .resources and the event from record() still name the old context, and with a second context made by cuCtxCreate, record() fails with CUDA_ERROR_INVALID_HANDLE.

How to Reproduce

from cuda.core import ContextOptions, Device, SMResourceOptions, Stream

dev = Device(0)
dev.set_current()
groups, _ = dev.resources.sm.split(SMResourceOptions(count=None))
green = dev.create_context(ContextOptions(resources=[groups[0]]))

null = Stream.from_handle(0)
null.context  # first query, primary context current
dev.set_current(green)
print(null.context == green)
$ python repro.py
False

Expected behavior

True, as LEGACY_DEFAULT_STREAM.context == green already gives in the same script. The NULL stream refers to whatever context is current, so nothing resolved from it should be cached on the Stream.

Operating System

Ubuntu 26.04 LTS (WSL2)

nvidia-smi output

NVIDIA-SMI 615.65.07    KMD Version: 616.64    CUDA UMD Version: 13.4
GPU 0: NVIDIA GeForce RTX 4060 Ti

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNeeds the team's attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions