JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.73k stars 5.48k forks source link

GC-safe `ccall`s #51574

Open kpamnany opened 1 year ago

kpamnany commented 1 year ago

It is sometimes the case that a call to an external C library can take a long time; solvers are a good example. We've observed such a call taking tens of minutes, and while that was happening, another Julia thread triggered GC. At this point, all threads stop, except for the thread running the solver call and the program froze until the ccall returned, many minutes later.

If you know that the call to the external library does not manipulate Julia memory, then you can ccall jl_gc_safe_enter and jl_gc_safe_leave before and after the external call.

So:

  1. Is there another solution to this problem?
  2. If not, why is this solution not defined in Base.GC (like GC.safepoint) and documented?
  3. Does it make sense to add a parameter to ccall that allows for it to be a GC safe region automatically?
vchuravy commented 1 year ago

This should be addressed by #49933