ygrek / ocaml-mysql

OCaml bindings to libmysqlclient
https://ygrek.org.ua/p/ocaml-mysql
GNU Lesser General Public License v2.1
23 stars 11 forks source link

Unsafe use of enter/leave_blocking_section in C finalizers #18

Open gadmm opened 1 year ago

gadmm commented 1 year ago

https://github.com/ygrek/ocaml-mysql/blob/ac1fddf6cfcd9f80c55c11ab8856f9a4810ba5da/mysql_stubs.c#L175-L177

Here and in other finalization functions in this file, it is unsafe to use enter/leave_blocking_section in C finalizers, as per the documentation (this can cause the GC to run). If it worked until now somehow, this will definitely blow up in OCaml 5 which has even tighter constraints.

A possible workaround is to register finalization functions with Gc.finalize instead of the caml_alloc_final/the finalizer field of caml_alloc_custom. In this case one can call a C function that can use enter/leave_blocking_section.

gadmm commented 1 year ago

(see the discussion in ocaml/ocaml#11865)