Skip to content

fix(core): return None from handle properties during Python shutdown#1754

Merged
Andy-Jost merged 1 commit intoNVIDIA:mainfrom
Andy-Jost:fix-stream-handle-shutdown
Mar 11, 2026
Merged

fix(core): return None from handle properties during Python shutdown#1754
Andy-Jost merged 1 commit intoNVIDIA:mainfrom
Andy-Jost:fix-stream-handle-shutdown

Conversation

@Andy-Jost
Copy link
Contributor

Summary

  • During Python interpreter shutdown, accessing .handle on Stream (or any other cuda.core object) raises ImportError because as_py() calls PyImport_ImportModule which fails when sys.modules is being cleared.
  • Guards make_py() with Py_IsFinalizing() (version-gated for Python < 3.13) to return None gracefully instead of raising.
  • Consolidates the py_is_finalizing() helper into the shared header, removing the duplicate from resource_handles.cpp.

Changes

  • resource_handles.hpp: Add py_is_finalizing() to detail namespace and guard make_py() to return None during finalization.
  • resource_handles.cpp: Remove duplicate py_is_finalizing() definition, use detail::py_is_finalizing via using declaration.

Test Coverage

Manually verified: a __del__ method accessing Stream.handle during shutdown now receives None instead of raising ImportError. Automated testing is impractical because __del__ timing during shutdown is non-deterministic.

Closes #1748

Made with Cursor

During interpreter finalization, `as_py()` calls `PyImport_ImportModule`
which fails because `sys.modules` is being cleared. Guard `make_py()`
with `py_is_finalizing()` to return None gracefully instead of raising.

Also consolidates the `py_is_finalizing()` helper into the shared header,
removing the duplicate definition from resource_handles.cpp.

Closes NVIDIA#1748

Made-with: Cursor
@Andy-Jost Andy-Jost added this to the cuda.core v0.7.0 milestone Mar 11, 2026
@Andy-Jost Andy-Jost added bug Something isn't working P0 High priority - Must do! cuda.core Everything related to the cuda.core module labels Mar 11, 2026
@Andy-Jost Andy-Jost self-assigned this Mar 11, 2026
@Andy-Jost
Copy link
Contributor Author

/ok to test 2417006

@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Mar 11, 2026

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.

@github-actions

This comment has been minimized.

@Andy-Jost Andy-Jost merged commit cc5e97e into NVIDIA:main Mar 11, 2026
92 checks passed
@Andy-Jost Andy-Jost deleted the fix-stream-handle-shutdown branch March 11, 2026 19:55
@github-actions
Copy link

Doc Preview CI
Preview removed because the pull request was closed or merged.

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

Labels

bug Something isn't working cuda.core Everything related to the cuda.core module P0 High priority - Must do!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants