Multicore and fork called from C bindings

Hi OCamlers,

I experienced an issue with ocamlfuse when it’s compiled with OCaml 5 or later. Basically, it crashes when run in the default background mode, but it works fine if run in foreground. Google brought me to this PR in the OCaml repo, where it explains that the problem is caused by a fork() call in the C code (that libfuse calls here to daemonize the process). After the fork, the first call to caml_release_runtime_system crashes the process with SIGABRT. The workaround is to call the internal caml_atfork_hook after fork. Indeed, I tried the workaround and it looks like it’s working fine. The thing that I don’t like very much is that I need to call an internal OCaml function, so I get also the risk of an unexpected breaking change. So I’m asking the community if there is a better way to solve the issue (unfortunately fork is called by the library so I don’t have control on that and I cannot patch it to call Unix.fork). Thanks in advance for any advice.

Cheers,
Alessandro

One possible solution would be to have a main function written in C that “daemonizes” the process if needed, then starts the OCaml code by calling caml_main. This way the OCaml runtime system never “sees” the fork() call done by C in order to daemonize. See OCaml - Interfacing C with OCaml , section 7.4 “Main program in C”, in the OCaml reference manual.

2 Likes

Thanks for the advice! I tested this solution a bit and it seems to work nicely.