ocaml-rs icon indicating copy to clipboard operation
ocaml-rs copied to clipboard

Boxroot assertion failing in multithreaded context despite using mutex

Open fmckeogh opened this issue 3 years ago • 2 comments

Using a lazily initialised global Runtime in a library is resulting in vendor/boxroot/boxroot.c:281: ring_push_back: Assertion '(*target)->hd.class == source->hd.class' failed errors.

With Mutex

Minimal test case with mutex: https://github.com/fmckeogh/ocaml-rust-minimal-failing-segfault/tree/f68ef75ace1d49599c9520e11fa1a4e05d3e94b7

(copied from repo Github Actions https://github.com/fmckeogh/ocaml-rust-minimal-failing-segfault/actions/runs/3260401056/jobs/5353935017)

Run cargo test --workspace --all-features --all-targets --no-fail-fast --release

    Finished release [optimized] target(s) in 0.07s
     Running unittests src/lib.rs (target/release/deps/sail-44467119e94355d2)

running 1 test
error: test failed, to rerun pass '--lib'

Caused by:
  process didn't exit successfully: `/home/runner/work/ocaml-rust-minimal-failing-segfault/ocaml-rust-minimal-failing-segfault/target/release/deps/sail-44467119e94355d2` (signal: 11, SIGSEGV: invalid memory reference)

Without Mutex (possibly different issue)

Minimal test case without: https://github.com/fmckeogh/ocaml-rust-minimal-failing-segfault/tree/d379707f6d35957ce93e28795bed39b858b29003

(Copied from local machine)

pc23430 ~/D/ocaml-rust-minimal-failing-segfault (main|✔) $ cargo test --release
   Compiling sail v0.1.0 (/home/fm208/Documents/ocaml-rust-minimal-failing-segfault)
    Finished release [optimized] target(s) in 0.34s
     Running unittests src/lib.rs (target/release/deps/sail-44467119e94355d2)

running 1 test
sail-44467119e94355d2: vendor/boxroot/boxroot.c:281: ring_push_back: Assertion `(*target)->hd.class == source->hd.class' failed.
sail-44467119e94355d2: vendor/boxroot/boxroot.c:281: ring_push_back: Assertion `(*target)->hd.class == source->hd.class' failed.
error: test failed, to rerun pass '--lib'

Caused by:
  process didn't exit successfully: `/home/fm208/Documents/ocaml-rust-minimal-failing-segfault/target/release/deps/sail-44467119e94355d2` (signal: 6, SIGABRT: process abort signal)

fmckeogh avatar Oct 16 '22 18:10 fmckeogh

Unrelated to the potential boxroot issue, but note that in multithread programs you need to register your threads with the OCaml runtime, otherwise you will run into issues: https://v2.ocaml.org/manual/intfc.html#ss:c-thread-register

You will also need to take care of obtaining and releasing the OCaml runtime lock in each of those threads: https://v2.ocaml.org/manual/intfc.html#ss:parallel-execution-long-running-c-code

My current preferred option (at least until OCaml V5, which will probably require some thinking over the Rust<->OCaml API) is to have a single Rust thread interact with the OCaml runtime.

tizoc avatar Oct 16 '22 18:10 tizoc

Thanks for the report!

I agree with @tizoc that accessing the OCaml runtime from a single thread is the best option right now.

I will find some time to experiment with the code you posted using OCaml 5.0 and see what kind of integration might make sense, but feel free to share any ideas as well!

zshipko avatar Oct 16 '22 18:10 zshipko