Skip to content
Merged
Changes from all commits
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
46 changes: 24 additions & 22 deletions cuda_core/tests/graph/test_graph_definition_lifetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,29 +213,31 @@ def test_reconstructed_body_survives_parent_deletion(init_cuda):
@pytest.mark.agent_authored(model="gpt-5.6")
def test_destroying_conditional_node_releases_branch_attachments(init_cuda, builder, expected_count):
"""Destroying a conditional owner releases every branch attachment."""
graph_def = GraphDefinition()
condition = try_create_condition(graph_def)
branches = builder(graph_def, condition)
callback_refs = []

for branch in branches:

def callback():
pass

callback_refs.append(weakref.ref(callback))
branch.callback(callback)

del callback, branches
gc.collect()
assert len(callback_refs) == expected_count
assert all(ref() is not None for ref in callback_refs)
dev = Device()
with dev.allocate(4, stream=dev.default_stream) as buf:
dev.default_stream.sync()
dptr = int(buf.handle)
graph_def = GraphDefinition()
condition = try_create_condition(graph_def)
branches = builder(graph_def, condition)
owner_refs = []

# Conditional bodies do not permit host callbacks; memset can retain an owner.
for branch in branches:
attachment_owner = _Sentinel()
owner_refs.append(weakref.ref(attachment_owner))
branch.memset(dptr, 0, 4, dst_owner=attachment_owner)

del attachment_owner, branches
gc.collect()
assert len(owner_refs) == expected_count
assert all(ref() is not None for ref in owner_refs)

owner = next(node for node in graph_def.nodes() if isinstance(node, ConditionalNode))
owner.destroy()
del owner
owner = next(node for node in graph_def.nodes() if isinstance(node, ConditionalNode))
owner.destroy()
del owner

_wait_until(lambda: all(ref() is None for ref in callback_refs))
_wait_until(lambda: all(ref() is None for ref in owner_refs))


@pytest.mark.parametrize("builder, expected_count", _COND_BUILDERS)
Expand All @@ -245,7 +247,7 @@ def test_destroying_conditional_node_invalidates_branch_handles(init_cuda, build
graph_def = GraphDefinition()
condition = try_create_condition(graph_def)
branches = builder(graph_def, condition)
branch_nodes = [branch.callback(lambda: None) for branch in branches]
branch_nodes = [branch.empty() for branch in branches]

owner = next(node for node in graph_def.nodes() if isinstance(node, ConditionalNode))
owner.destroy()
Expand Down
Loading