Skip to content

[BUG]: Accessing Stream.handle in finalizer fails on module import #1748

Closed

Description

@stiepan

Is this a duplicate?

Type of Bug

Runtime Error

Component

cuda.core

Describe the bug

Starting with cuda.core 0.6, accessing Stream.handle when the Python is shutting down, fails with

ModuleNotFoundError: import of builtins halted; None in sys.modules

It used to work just fine before (0.5.1). Looks like as_py(..) is the culprit.

How to Reproduce

from cuda.core import Device

class MyClass:
    def __init__(self, stream):
        self.stream = stream

    def __del__(self):
        print("MyClass del", self.stream.handle)

dev = Device(0)
dev.set_current()
cp_stream = dev.create_stream()
my_stream = MyClass(cp_stream)

Expected behavior

Accessing the handle doesn't try to import modules and is safe to call from finalizers that may run on shutdown.

Operating System

No response

nvidia-smi output

No response

Activity

  1. added
    P0High priority - Must do!
    cuda.coreEverything related to the cuda.core module
    on Mar 10, 2026
  2. added this to the cuda.core v0.7.0 milestone on Mar 10, 2026
  3. Andy-Jost commented on Mar 11, 2026

    @Andy-Jost
    Contributor

    @stiepan do you need stream.handle to (try to) return a valid handle during shutdown, or can it simply return None?

  4. added a commit that references this issue on Mar 11, 2026
    2417006
  5. added a commit that references this issue on Mar 11, 2026
    cc5e97e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P0High priority - Must do!bugSomething isn't workingcuda.coreEverything related to the cuda.core module

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions