Skip to content

cuda.core: make buffer cleanup context-safe - #2455

Draft
Andy-Jost wants to merge 4 commits into
NVIDIA:mainfrom
Andy-Jost:ajost/context-safe-buffer-cleanup
Draft

cuda.core: make buffer cleanup context-safe#2455
Andy-Jost wants to merge 4 commits into
NVIDIA:mainfrom
Andy-Jost:ajost/context-safe-buffer-cleanup

Conversation

@Andy-Jost

Copy link
Copy Markdown
Contributor

Summary

Make device-pointer cleanup retain and activate the CUDA context required by the allocation or its deallocation stream. This prevents deferred graph attachment cleanup and ordinary Buffer destruction from leaking memory when no context is current on the cleanup thread.

Changes

  • Retain allocation and deallocation-stream context handles in device-pointer resource handles.
  • Recover context metadata from CUDA pointers and streams, including a current-context fallback for context-independent stream-ordered allocations.
  • Temporarily activate the retained context for native frees and MemoryResource.deallocate() callbacks, then restore the caller's exact prior context.
  • Stop treating CUDA_ERROR_INVALID_CONTEXT as successful memory-pool deallocation.

Review guide

  1. Review resource_handles.cpp/.hpp for context ownership, selection, and scoped restoration.
  2. Review _resource_handles.pyx for driver function-pointer plumbing.
  3. Review the memory and graph lifetime tests for cleanup with no current context.

Related Work

Ensure deferred and MR-backed buffer destruction activates the allocation or deallocation-stream context, preventing invalid-context leaks after callers clear their current context.
@Andy-Jost Andy-Jost added this to the cuda.core 1.2.0 milestone Jul 29, 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 Jul 29, 2026
@Andy-Jost Andy-Jost self-assigned this Jul 29, 2026
@copy-pr-bot

copy-pr-bot Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@kkraus14

Copy link
Copy Markdown
Collaborator

@Andy-Jost is there any performance concerns here? I vaguely remember some microbenchmarking you did but I don't recall if it was related to this PR.

@Andy-Jost

Andy-Jost commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@Andy-Jost is there any performance concerns here? I vaguely remember some microbenchmarking you did but I don't recall if it was related to this PR.

@kkraus14 I think you're referring to the discussion in #2311, which showed ~9ns for a current-context check and ~37ns for a check-and-switch. That came up when discussing Device objects as Python context managers. I don't anticipate any performance problems from this PR, though I can check if you're concerned about it.

@Andy-Jost Andy-Jost closed this Jul 29, 2026
@Andy-Jost Andy-Jost reopened this Jul 29, 2026
@Andy-Jost

Copy link
Copy Markdown
Contributor Author

/ok to test

@github-actions

Copy link
Copy Markdown

Restore fixture-managed contexts in direct cleanup tests and run deferred pending-call cleanup in a subprocess so parallel workers cannot leak thread-local state.
@Andy-Jost

Copy link
Copy Markdown
Contributor Author

/ok to test

Resolve allocation and stream contexts at the Buffer boundary so the C++ resource layer only retains and restores explicit context handles.
@Andy-Jost

Copy link
Copy Markdown
Contributor Author

/ok to test

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.

2 participants