Skip to content

Commit 974a7ad

Browse files
Andy-Jostclaude
andcommitted
cuda.core: scope the child-graph registry to the parent GraphBox
The process-wide graph registry mapped a raw CUgraph to its canonical GraphHandle. Every handle of a hierarchy aliases the hierarchy's single control block, so a lockable registry entry proved only that the hierarchy was alive, not that the child graph was. CUDA destroys a child graph inside cuGraphDestroyNode, cuGraphNodeSetParams, or capture invalidation, and the box is retired only after the driver call returns. In that window the map could hand one hierarchy's recycled address to another. The only reader, create_child_graph_handle, always knows the parent, and roots are never looked up. Each GraphBox now holds the registry of its direct child graphs, next to its node registry. A recycled address can only collide inside one hierarchy, where mutations are externally synchronized and every retire precedes the matching registration. The conditional unregister_handle overload, the bulk register_handles, the static registry, and the hierarchy deleter's unregister loop are gone. The same-parent registry test modeled two live generations at one address under one parent, which the synchronization contract excludes, and is removed. Written by Codex (gpt-6.1-sol); reviewed by Claude. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 46bab1f commit 974a7ad

5 files changed

Lines changed: 51 additions & 102 deletions

File tree

‎cuda_core/cuda/core/_cpp/rt/GRAPH_ATTACHMENTS.md‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,19 @@ metadata for the root graph and subgraphs. Each `GraphBox` stores:
167167
- the `CUgraph`
168168
- its parent box and owning node, when it is a child graph
169169
- a `GraphAttachmentMap` from nodes to node attachments (`NodeAttachment*`)
170-
- a per-graph weak registry to canonicalize node handles
171-
172-
When CUDA destroys a child graph, the associated `GraphBox` is cleared and
173-
moved to `GraphHierarchy::graveyard`. That invalidates existing handles and
174-
preserves the storage until the hierarchy's last shared owner is released.
175-
CUDA may reuse a child's raw handle before the old metadata is retired.
176-
Child lookup checks the parent box and owner node, and cleanup removes only
177-
its own resource generation's registry entry. This keeps old invalidation or
178-
hierarchy destruction from adopting or erasing an independent graph's newer
179-
metadata; see [registry identity](REGISTRY_DESIGN.md#reused-graph-handles).
180-
181-
The process-wide graph registry maps a live `CUgraph` to its canonical
182-
`GraphHandle`. Node handles are scoped to their `GraphBox`, where they can all
183-
be invalidated when CUDA destroys that graph. They use separate
184-
`GraphNodeBox::resource` fields and are nulled before the graph box is retired.
170+
- weak registries to canonicalize direct child graph and node handles
171+
172+
A retired `GraphBox` is cleared and moved to `GraphHierarchy::graveyard`.
173+
That invalidates existing handles and preserves the storage until the
174+
hierarchy's last shared owner is released.
175+
176+
Each `GraphBox` holds two registries: its direct child graphs keyed by
177+
`CUgraph` and its nodes keyed by `CUgraphNode`. Both are scoped to the box,
178+
so a raw handle recycled by CUDA in another hierarchy cannot resolve to this
179+
hierarchy's metadata; see
180+
[registry identity](REGISTRY_DESIGN.md#reused-graph-handles). Node handles use
181+
separate `GraphNodeBox::resource` fields and are nulled before the graph box
182+
is retired.
185183

186184
## Invariants
187185

‎cuda_core/cuda/core/_cpp/rt/REGISTRY_DESIGN.md‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,26 @@ Without this level, a round-tripped handle would produce a new Box
3030
with default metadata, losing information that was set at creation.
3131

3232
Instances: `context_registry`, `stream_registry`, `event_registry`,
33-
`kernel_registry`, `graph_registry`, and one node registry per `GraphBox`.
33+
`kernel_registry`, and, per `GraphBox`, one registry for its direct child
34+
graphs and one for its nodes.
3435

3536
### Reused graph handles
3637

37-
CUDA can recycle a child graph's raw address after native destruction but
38-
before its old metadata is retired. A child-graph lookup therefore accepts a
39-
cached handle only when its parent `GraphBox` and owning node match the
40-
requested relationship. Independent hierarchies must not share stale metadata
41-
merely because CUDA reused an address.
38+
A `GraphHandle` weak reference locks whenever any handle of its hierarchy is
39+
alive, so a lockable entry does not prove that a child graph is alive. CUDA
40+
destroys child graphs inside node operations and capture invalidation, before
41+
cuda.core retires their boxes. A process-wide map keyed by raw `CUgraph` could
42+
therefore resolve a recycled address to another hierarchy's metadata.
4243

43-
Graph cleanup uses identity-conditional unregistering: it erases the raw key
44-
only if the entry is expired or still names the retiring resource slot. Compare
45-
`shared_ptr::get()`, not its control block, because distinct graph generations
46-
within one hierarchy share ownership but have different stable resource slots.
47-
The temporary strong handle used for this check is released after unlocking
48-
the registry mutex; its deleter may re-enter the registry.
44+
Scoping the child registry to its parent `GraphBox` removes that case by
45+
construction: `create_child_graph_handle` always knows the parent, and roots
46+
are never looked up. Within one hierarchy, external synchronization and the
47+
retire-before-register order in `graph_commit_child_graph_update` keep the
48+
entry for an address on its live box, as long as every child graph that CUDA
49+
destroys is retired before that address is registered again. The one path
50+
that does not retire is a conditional-body capture that ends invalidated; the
51+
parent graph is already invalid there (issue #2918), and the registry does
52+
not guard it.
4953

5054
## Level 2: Resource Handle -> Python Object (Cython)
5155

‎cuda_core/cuda/core/_cpp/rt/graph.cpp‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct GraphBox : GraphBoxBase {
8282
CUgraphNode owner_node = nullptr; // Node in parent that owns this graph.
8383
GraphAttachmentMap attachments; // Non-owning attachment index.
8484
HandleRegistry<CUgraphNode, GraphNodeHandle> node_handles;
85+
// See REGISTRY_DESIGN.md (Level 1: Driver Handle -> Resource Handle)
86+
HandleRegistry<CUgraph, GraphHandle> children; // Canonical handles of the direct child graphs.
8587

8688
GraphBox(
8789
CUgraph resource_, GraphHierarchy *hierarchy_, GraphBox *parent_ = nullptr, CUgraphNode owner_node_ = nullptr
@@ -104,10 +106,6 @@ struct GraphHierarchy {
104106
}
105107
};
106108

107-
// See REGISTRY_DESIGN.md (Level 1: Driver Handle -> Resource Handle)
108-
using GraphRegistry = HandleRegistry<CUgraph, GraphHandle>;
109-
static GraphRegistry graph_registry;
110-
111109
// Immutable resource owners for one version of a graph node's parameters.
112110
// Inheriting DeferredCleanupItem lets CUDA's user-object destructor enqueue
113111
// the payload without destroying owners on the callback thread.
@@ -144,6 +142,17 @@ get_box(const GraphHandle &h) noexcept
144142
return const_cast<GraphBox *>(static_cast<const GraphBox *>(value));
145143
}
146144

145+
// Register child handles with their immediate parents. Each handle aliases a
146+
// GraphBox whose parent is set; a hierarchy's root is never registered.
147+
void
148+
register_child_handles(const std::vector<GraphHandle> &handles)
149+
{
150+
for (const GraphHandle &h : handles) {
151+
GraphBox *box = get_box(h);
152+
box->parent->children.register_handle(box->resource, h);
153+
}
154+
}
155+
147156
// Rekey a staged attachment map from source nodes to their cloned nodes.
148157
// The caller must release the GIL before calling this function.
149158
CUresult
@@ -302,11 +311,6 @@ create_graph_handle(CUgraph graph)
302311
auto hierarchy = std::shared_ptr<GraphHierarchy>(
303312
new GraphHierarchy{},
304313
[](GraphHierarchy *hierarchy) {
305-
for (const GraphBox& box : hierarchy->graphs) {
306-
if (box.resource) {
307-
graph_registry.unregister_handle(box.resource, &box.resource);
308-
}
309-
}
310314
GraphBox *root = hierarchy->root();
311315
if (root && root->resource) {
312316
GILReleaseGuard gil;
@@ -320,7 +324,6 @@ create_graph_handle(CUgraph graph)
320324
GraphBox &root = hierarchy->graphs.emplace_back(graph, hierarchy.get());
321325

322326
GraphHandle h_graph(hierarchy, &root.resource);
323-
graph_registry.register_handle(graph, h_graph);
324327
return h_graph;
325328
}
326329

@@ -331,9 +334,8 @@ create_child_graph_handle(CUgraph child_graph, const GraphHandle &h_parent, CUgr
331334
return {};
332335
}
333336
GraphBox *parent = get_box(h_parent);
334-
if (GraphHandle h_graph = graph_registry.lookup(child_graph)) {
335-
const GraphBox *child = get_box(h_graph);
336-
if (child->parent == parent && child->owner_node == owner_node) {
337+
if (GraphHandle h_graph = parent->children.lookup(child_graph)) {
338+
if (get_box(h_graph)->owner_node == owner_node) {
337339
return h_graph;
338340
}
339341
}
@@ -342,7 +344,7 @@ create_child_graph_handle(CUgraph child_graph, const GraphHandle &h_parent, CUgr
342344
GraphBox &child = hierarchy->graphs.emplace_back(child_graph, hierarchy, parent, owner_node);
343345

344346
GraphHandle h_child(h_parent, &child.resource);
345-
graph_registry.register_handle(child_graph, h_child);
347+
parent->children.register_handle(child_graph, h_child);
346348
return h_child;
347349
}
348350

@@ -398,7 +400,7 @@ publish_child_graph_update(PreparedChildGraphUpdateState &state, GraphHandle *ou
398400
GraphBox *parent = get_box(state.h_parent);
399401
parent->hierarchy->graphs.splice(parent->hierarchy->graphs.end(), state.replacement);
400402
*out_child = state.handles.front();
401-
graph_registry.register_handles(state.handles);
403+
register_child_handles(state.handles);
402404
}
403405

404406
CUresult
@@ -632,7 +634,7 @@ graph_clone_attachments(const GraphHandle &h_clone, const GraphHandle &h_source)
632634
}
633635

634636
clone->hierarchy->graphs.splice(clone->hierarchy->graphs.end(), subgraphs);
635-
graph_registry.register_handles(handles);
637+
register_child_handles(handles);
636638
return CUDA_SUCCESS;
637639
}
638640

@@ -662,7 +664,9 @@ retire_graph(GraphHierarchy &hierarchy, std::list<GraphBox>::iterator graph) noe
662664
}
663665
}
664666
if (graph->resource) {
665-
graph_registry.unregister_handle(graph->resource, &graph->resource);
667+
if (graph->parent) {
668+
graph->parent->children.unregister_handle(graph->resource);
669+
}
666670
graph->resource = nullptr;
667671
}
668672
graph->attachments.clear();

‎cuda_core/cuda/core/_cpp/rt/internal.hpp‎

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <type_traits>
1717
#include <unordered_map>
1818
#include <utility>
19-
#include <vector>
2019

2120
namespace cuda_core::rt::detail {
2221

@@ -193,36 +192,6 @@ class HandleRegistry {
193192
map_.erase(key);
194193
}
195194

196-
// A reused raw key may already name a newer resource generation.
197-
void
198-
unregister_handle(const Key &key, const typename Handle::element_type *expected) noexcept
199-
{
200-
// Releasing the last strong owner may re-enter this registry, so keep
201-
// it alive until after the mutex is released.
202-
Handle h;
203-
{
204-
std::lock_guard<std::mutex> lock(mutex_);
205-
auto it = map_.find(key);
206-
if (it != map_.end()) {
207-
h = it->second.lock();
208-
if (!h || h.get() == expected) {
209-
map_.erase(it);
210-
}
211-
}
212-
}
213-
}
214-
215-
void
216-
register_handles(const std::vector<Handle> &handles)
217-
{
218-
std::lock_guard<std::mutex> lock(mutex_);
219-
for (const Handle &h : handles) {
220-
if (h) {
221-
map_[*h] = h;
222-
}
223-
}
224-
}
225-
226195
Handle
227196
lookup(const Key &key)
228197
{

‎cuda_core/tests/cython/test_graph_registry.pyx‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# distutils: language = c++
55
# distutils: include_dirs = ../../cuda/core
66

7-
"""Deterministic raw-handle reuse tests against the real graph registry.
7+
"""Deterministic raw-handle reuse tests across independent parent registries.
88
99
Parents are real empty CUDA graphs. Stack-local addresses stand in for reused
1010
child and owner handles only inside the metadata API; they are never passed to
@@ -56,32 +56,6 @@ def test_child_graph_registry_rejects_foreign_parent(init_cuda):
5656
assert same_child.get() == new_child.get()
5757

5858

59-
@pytest.mark.agent_authored(model="gpt-6")
60-
def test_child_graph_registry_rejects_foreign_owner_in_same_parent(init_cuda):
61-
"""Retiring an old owner preserves the replacement within one hierarchy."""
62-
cdef uintptr_t tokens[3]
63-
cdef CUgraph raw_child = <CUgraph>&tokens[0]
64-
cdef CUgraphNode old_owner = <CUgraphNode>&tokens[1]
65-
cdef CUgraphNode new_owner = <CUgraphNode>&tokens[2]
66-
cdef GraphDefinition parent = GraphDefinition()
67-
cdef GraphHandle old_child = create_child_graph_handle(
68-
raw_child, parent._h_graph, old_owner
69-
)
70-
cdef GraphHandle new_child = create_child_graph_handle(
71-
raw_child, parent._h_graph, new_owner
72-
)
73-
assert new_child.get() != old_child.get()
74-
75-
invalidate_child_graph_state(parent._h_graph, old_owner)
76-
assert as_cu(old_child) == NULL
77-
assert as_cu(new_child) == raw_child
78-
79-
cdef GraphHandle same_child = create_child_graph_handle(
80-
raw_child, parent._h_graph, new_owner
81-
)
82-
assert same_child.get() == new_child.get()
83-
84-
8559
@pytest.mark.agent_authored(model="gpt-6")
8660
def test_graph_hierarchy_deletion_preserves_replacement_mapping(init_cuda):
8761
"""An old tree's destructor must not erase another tree's newer entry."""

0 commit comments

Comments
 (0)