Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix stream registry corruption for owner-backed handles
Owner-backed stream handles (from create_stream_handle_with_owner) are
no longer registered in the stream_registry. Multiple Python owners can
wrap the same CUstream independently, each stacking its own
Py_INCREF/Py_DECREF without competing for a single registry slot.

The registry lookup at the top is preserved to reuse existing
cuda-core-owned handles that carry context metadata.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
  • Loading branch information
leofang and claude committed May 4, 2026
commit 9d21b6b6fe288d20e149f6b845eacab665f36b7b
11 changes: 5 additions & 6 deletions cuda_core/cuda/core/_cpp/resource_handles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ StreamHandle create_stream_handle_ref(CUstream stream) {
StreamHandle create_stream_handle_with_owner(CUstream stream, PyObject* owner) {
if (auto h = stream_registry.lookup(stream)) {
// Reuse handles that already carry structural context metadata, e.g.
// cuda-core-owned streams. Owner-backed foreign streams still need a
// fresh handle so the supplied owner is retained.
// cuda-core-owned streams.
if (get_box(h)->h_context) {
return h;
}
Expand All @@ -473,20 +472,20 @@ StreamHandle create_stream_handle_with_owner(CUstream stream, PyObject* owner) {
return create_stream_handle_ref(stream);
}
Py_INCREF(owner);
// Owner-backed handles are NOT registered in the stream registry to avoid
// corruption when multiple owners wrap the same CUstream (each stacks its
// own Py_INCREF/Py_DECREF independently).
auto box = std::shared_ptr<const StreamBox>(
new StreamBox{stream, {}},
[owner](const StreamBox* b) {
stream_registry.unregister_handle(b->resource);
GILAcquireGuard gil;
if (gil.acquired()) {
Py_DECREF(owner);
}
delete b;
}
);
StreamHandle h(box, &box->resource);
stream_registry.register_handle(stream, h);
return h;
return StreamHandle(box, &box->resource);
}

ContextHandle get_stream_context(const StreamHandle& h) noexcept {
Expand Down