Repository navigation
Conversation
Each thread that missed its thread-local cache in get_primary_context() retained the primary context separately and overwrote the registry entry of the previous thread's box, and every context deleter erased the registry entry for its CUcontext unconditionally. When a thread exited, its box erased the entry of a still-live box, later lookups returned non-retaining handles, and the driver destroyed the primary context while cuda.core objects still referred to it. The same unconditional erase and overwrite let Device.create_context() drop its new green context in favor of a stale plain entry at a reused address. HandleRegistry now records the element address of each entry, erases an entry only for the handle that registered it, and gains insert_or_get(), which registers a candidate without displacing a live entry unless a policy says so. Contexts use it to keep one owning box per live context: a box that holds a primary-context retain or the owning green-context handle replaces a plain one, and nothing replaces an owning one. All threads now share one retaining primary-context box per device. Stream, event and graph registries use the ownership-checked erase as well. The primary-context deleter now reports cuDevicePrimaryCtxRelease failures through a pw_ wrapper instead of discarding the status. Part of NVIDIA#3533 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
insert_or_get() destroyed a displaced live handle at the end of its inner scope, while the registry mutex was still held. If another thread had dropped its own copy in the meantime, that handle was the last reference, and its deleter took the same mutex and deadlocked. Declare the handle before the lock so that it is destroyed after the unlock. Add a test in which several threads call Device.set_current() concurrently in a fresh process. It covers the losing-candidate path in get_primary_context(): one held Context keeps the primary context active, and dropping it leaves the context inactive. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Release notes: say that only the retaining handle keeps the primary context alive, and that the green-context bug needs a live Context for the earlier context. Tests: drop the stderr-text assertion, let create_context errors propagate instead of skipping, and destroy the foreign green context and close the stream on failure. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…otes test_green_context_replaces_stale_plain_entry: if set_current() fails after the foreign green context was made current, make the primary context current again before destroying the foreign one, and let the original error propagate instead of a rollback failure from handle_return. test_concurrent_set_current_shares_one_primary_retain: drop the fixed one-second wait. The held Context keeps the primary context active either way, and the release check already polls with a deadline. REGISTRY_DESIGN.md: state that insert_or_get releases a displaced live handle only after the unlock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ten NVIDIA#3533 tests context.cpp: the thread-local primary-context cache is now a struct whose destructor sets a plain thread_local flag before dropping its entries from a local. A CUDAWarning hook that runs while the last entry is released and calls Device.set_current() then bypasses the cache instead of reading one that is being destroyed. test_primary_context_survives_thread_exit: start thread B only after thread A has retained the primary context, so the test cannot pass because B registered first. test_green_context_replaces_stale_plain_entry: check liveness with green.resources.sm, which works on driver 12.4, instead of a stream, which needs 12.5. Both thread tests now run module-level child functions through run_child and use ThreadPoolExecutor, which propagates thread errors through result(). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…h rollback failures
The thread-exit and concurrent-retain children used simplefilter("error",
CUDAWarning), but a promoted warning from a deleter reaches
sys.unraisablehook, so the child still exited 0. Run both bodies inside
assert_no_cuda_warning(), which records warnings and fails on a CUDAWarning.
test_green_context_replaces_stale_plain_entry: keep both rollback calls and
attach each failed status to the original exception instead of discarding it.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
cuda.core again keeps each device's primary context alive for the rest of the process, as the CUDA runtime does and as cuda.core did before 0.6.0 (NVIDIA#1368). get_primary_context() pins one retaining box per device in a process-wide table that is never freed and is read without a lock, so no thread exit or handle release can drop it. The per-thread cache is gone. create_context_handle_ref() and get_current_context() also pin a primary context that another library made current: if the context is the calling thread's current context and its device's active primary context, it is retained once and pinned. The retain taken to compare addresses is given back for any other context, and an inactive primary context is never created. Device.set_current(ctx) resolves the previous context before it switches, while that context is still current. Context resolution now goes through one helper in C++ and one in Cython (Context_primary() / Context_get_primary_handle()), which Device.set_current() and both memory resources share, so failures are raised consistently. These lookups may take a mutex and report a failure, which acquires the GIL, so they must not run under a C++ lock. deviceptr_import_ipc() therefore resolves the deallocation stream before it takes the IPC pointer cache's mutex, and peeks at the cache first with a non-waiting find_live(), so a repeat import is returned without resolving a stream. Device.set_current(ctx) no longer returns a stale Context for the previous context: the driver reuses the address of a destroyed context, so it now compares the remembered Context's handle with the handle just resolved instead of comparing raw addresses. DESIGN.md and REGISTRY_DESIGN.md describe the primary-context policy and its lock rule. Tests cover concurrent set_current() leaving exactly one retain, lookups and set_current() of a primary context that another owner made current, a green-context lookup leaving no primary retain, and the stale-address case. Fixes NVIDIA#3533 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
3 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
closes #3533
Stacked on #3538. Until #3538 merges, this diff also contains its commits, so review only the last commit (
2ca321ec4a4). I will rebase ontomainonce #3538 merges.cuda.core again keeps each device's primary context alive for the rest of the process, as the CUDA runtime does and as cuda.core did before 0.6.0 (#1368).
create_context_handle_ref(),create_current_context_handle_ref()andget_current_context()share one resolver. If it resolves the calling thread's current context and that is its device's active primary context, it is retained and pinned. Otherwise the retain taken to compare addresses is given back, and an inactive primary context is never created.deviceptr_import_ipc()resolves the deallocation stream before it takes the IPC pointer cache's mutex, and peeks at the cache first so a repeat import resolves no stream.Context_primary()/Context_get_primary_handle()) forDevice.set_current()and both memory resources.Device.set_current(ctx)resolves the previous context before it switches, and no longer returns a staleContextwhen the driver reuses a destroyed context's address.Not covered: an external
cuDevicePrimaryCtxReset(). cuda.core never resets a primary context, and supporting it needs its own design.Tests
Concurrent
set_current()leaves exactly one retain; lookups andset_current()of a primary context that another owner made current; a green-context lookup leaves no primary retain; the stale-address case.Checklist