Fix #1641: Handle cleanup when exceptions are thrown#1669
Open
mdboom wants to merge 2 commits intoNVIDIA:mainfrom
Open
Fix #1641: Handle cleanup when exceptions are thrown#1669mdboom wants to merge 2 commits intoNVIDIA:mainfrom
mdboom wants to merge 2 commits intoNVIDIA:mainfrom
Conversation
Contributor
|
Auto-sync is disabled for ready for review pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
Contributor
Author
|
/ok to test |
|
Contributor
Author
|
/ok to test |
leofang
reviewed
Feb 21, 2026
Comment on lines
-1038
to
-1045
| cdef _HelperInputVoidPtrStruct cycallbackHelper | ||
| cdef void* cycallback = _helper_input_void_ptr(callback, &cycallbackHelper) | ||
| cdef _HelperInputVoidPtrStruct cypayloadHelper | ||
| cdef void* cypayload = _helper_input_void_ptr(payload, &cypayloadHelper) | ||
| with nogil: | ||
| err = cynvrtc.nvrtcSetFlowCallback(cyprog, cycallback, cypayload) | ||
| _helper_input_void_ptr_free(&cycallbackHelper) | ||
| _helper_input_void_ptr_free(&cypayloadHelper) |
Member
There was a problem hiding this comment.
Q: Maybe we should encapsulate _helper_input_void_ptr and _helper_input_void_ptr_free as a C++ class (not Cython cdef class, which was the previous approach IIRC)?
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.
In driver, runtime and nvrtc, the generated code follows the following pattern:
For certain argument types, $precall and $postcall are required to be pairs, such as
malloc/free. If an exception is thrown when parsing one of the other arguments in$precall,$postcallwill not get called, leaking memory or other resources.This reorganizes the code (whenever
$postcallis non-empty) to:This diff is larger than it otherwise might need to be since
Cythononly allowscdefdeclarations at the top level. So all thecdef's that used to be part of$precallneed to be moved to$init.Performance impact
try/finallyis implemented by Cython withgotos. It generates two copies of thefinallyclause: one for success and one for failure, so there is a cost in overall code size. (This is similar to CPython's bytecode implementation oftry/finally). However, the runtime performance penalty is below the noise threshold in the #659 benchmark. There is a small performance penalty of around 200ns for the error case, but that's to be expected -- the old implementation leaked memory.Alternatives considered
We could use C++ RAII to automatically free memory. In fact, some of our code in these files already does that in the use of
std::vector. However, this is not generally applicable as a solution, since it's not possible to implement a custom RAII struct Cython that is not also a PyObject. This would make theHelperVoidPtrStructoptimization impossible.We could require that $init does all validation (but not any resource allocation), and the $precall would never raise, so $postcall would always be guaranteed to run. It seems like this may have been part of the original design, but then has not been strictly enforced everywhere over time. Even if we could get to that, it has two problems. (1) Some exceptions in $precall are unavoidable, even if the inputs are valid, such as
mallocfailing. (2) Doing a separate validation and conversion pass on all the arguments is never going to be as performant as a single pass.