-
-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CVRhsFn_wrapper unsafe use of @cfunction($f, ...) #381
Comments
This was referenced Jan 13, 2023
ChrisRackauckas
pushed a commit
that referenced
this issue
Jan 13, 2023
Addresses #381 This is theoretically a breaking change, however any application code that was using the low-level API in this way (passing a Julia function into eg CVodeInit and relying on CVRhsFn_wrapper) would likely segfault randomly due to garbage collection of the wrapper. The problem with CVRhsFn_wrapper and similar is that a temporary Base.CFunction closure is returned by @cfunction($f, ...), which may be garbage collected hence it is not safe to return the enclosed pointer. This is somewhat similar to the problem with NVector addressed by #380, but more fundamental as the RHS function needs to persist for the whole lifetime of the solver use. Fix here is to remove these wrappers (which were only used by a few tests of the low-level interface), and require that the caller either uses the @cfunction(f, ...) form to generate a C pointer (ie f known at compile time and the runtime Julia RHS function passed in to Sundials C code as part of the opaque user data, this is what the high-level interface does), or manages the lifetime of the @cfunction explicitly.
Fixed thanks. I wonder if this will end up fixing the ability to use Sundials' sensitivity analysis stuff too. It would be interesting to try and get https://github.com/SciML/Sundials.jl/blob/master/test/cvodes_dns.jl up and running |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See eg CVRhsFn_wrapper:
Sundials.jl/src/types_and_consts_additions.jl
Lines 9 to 10 in 64c98c8
which is used in
Sundials.jl/lib/libsundials_api.jl
Lines 1668 to 1671 in 64c98c8
The problem is that a temporary Base.CFunction closure is returned by @cfunction($f, ...), which may be garbage collected so it is not safe to return the enclosed pointer (somewhat similar to the problem with NVector addressed by #380, but more fundamental as the RHS function needs to persist for the whole lifetime of the solver use).
Suggested fix is to remove CVRhsFn_wrapper (which is only used by a few tests of the low-level interface), and require that the caller either uses the @cfunction(f, ...) form to generate a C pointer (ie f known at compile time and the runtime Julia RHS function passed in to Sundials C code as part of the opaque user data, this is what the high-level interface does), or manages the lifetime of the @cfunction explicitly.
Addressed by PR #384
The text was updated successfully, but these errors were encountered: