Hi,
On Sat, 4 Mar 2023 at 18:34,
lgau...@gmail.com <
lgau...@gmail.com> wrote:
> Is there guidance about calling non-reentrant C libraries through cffi since the GIL is released? To my knowledge no way to tell cffi that a C call should be made without releasing the GIL.
>
> At the moment I am using a `threading.RLock` context to make calls. Does it seem sound?
Yes, that's the right approach. In CFFI the answer is often: "what
would you do in C? Do the same thing." If you want to use a library
that is not thread-safe inside a wider context where threads exist,
the common solution is to wrap it manually with locks.
> In addition to the RLock, I am unsure about how many backdoors through C allow an unwanted concurrent use of non-reentrant C functions. One I am thinking about is through a level of recursion: a C function calling an embedded Python itself calling that C function through cffi. I am not sure about how to best address that. Does anyone have recommendations?
Depending on the library, you may want to wrap it using
``_thread.allocate_lock()`` or the equivalent from ``threading``,
instead of ``RLock``. This would deadlock if the Python programmer
does something like what you are describing, but that's a lot better
than crashing, and typing Ctrl-C should give a nice traceback. You
can also use more advanced forms of locks that detect deadlock
attempts and complain.
A bientôt,
Armin