Skip to content

fix(cuda.bindings): Make param_packer.feed() free-threading-safe via import-time init#2417

Open
clin1234 wants to merge 3 commits into
NVIDIA:mainfrom
clin1234:threading-fixes
Open

fix(cuda.bindings): Make param_packer.feed() free-threading-safe via import-time init#2417
clin1234 wants to merge 3 commits into
NVIDIA:mainfrom
clin1234:threading-fixes

Conversation

@clin1234

Copy link
Copy Markdown

Description

Restructure param_packer.h so the ctypes type pointers and the feeder table are built once in a new init_param_packer(), called at module import (from utils.pxi) while single-threaded. feed() becomes a read-only lookup on a never-mutated std::map, so concurrent kernel launches from multiple host threads no longer race the map or the ctypes lazy-init -- races that were live in the cp315t (Py_MOD_GIL_NOT_USED) wheels. init_param_packer is declared 'except +' so a failed 'import ctypes' surfaces as a Python exception at import instead of std::terminate across the Cython boundary, and feed() is now non-throwing. The ctypes module strong ref is kept deliberately (it keeps the borrowed type pointers valid).

Harden resource_handles.cpp: make mr_dealloc_cb std::atomic with acquire/release ordering, and document the verified single-init of initialize_deferred_cleanup() and the mutation-only lazy path for GraphBox::slot_table (both already safe; no behavior change).

closes #2416

Restructure param_packer.h so the ctypes type pointers and the feeder
table are built once in a new init_param_packer(), called at module import
(from utils.pxi) while single-threaded. feed() becomes a read-only lookup on
a never-mutated std::map, so concurrent kernel launches from multiple host
threads no longer race the map or the ctypes lazy-init -- races that were
live in the cp315t (Py_MOD_GIL_NOT_USED) wheels. init_param_packer is
declared 'except +' so a failed 'import ctypes' surfaces as a Python
exception at import instead of std::terminate across the Cython boundary,
and feed() is now non-throwing. The ctypes module strong ref is kept
deliberately (it keeps the borrowed type pointers valid).

Harden resource_handles.cpp: make mr_dealloc_cb std::atomic with
acquire/release ordering, and document the verified single-init of
initialize_deferred_cleanup() and the mutation-only lazy path for
GraphBox::slot_table (both already safe; no behavior change).
@copy-pr-bot

copy-pr-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added cuda.bindings Everything related to the cuda.bindings module cuda.core Everything related to the cuda.core module labels Jul 23, 2026
@clin1234
clin1234 marked this pull request as ready for review July 24, 2026 16:01
@clin1234 clin1234 changed the title Make param_packer.feed() free-threading-safe via import-time init fix(cuda.bindings): Make param_packer.feed() free-threading-safe via import-time init Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.bindings Everything related to the cuda.bindings module cuda.core Everything related to the cuda.core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Umbrella issues with free-threading and Cython bindings

1 participant