# Problem with segmentation fault running C threads

**URL:** <https://discuss.ocaml.org/t/problem-with-segmentation-fault-running-c-threads/8466>\
**Category:** Learning\
**Created:** [September 10, 2021, 4:33pm UTC](https://discuss.ocaml.org/t/problem-with-segmentation-fault-running-c-threads/8466 "2021-09-10T16:33:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Casper\_Schipper](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/casper_schipper/32/2990_2.png) [@Casper\_Schipper](https://discuss.ocaml.org/u/Casper_Schipper)\
**Post date:** [September 10, 2021, 4:33pm UTC](https://discuss.ocaml.org/t/problem-with-segmentation-fault-running-c-threads/8466/1 "2021-09-10T16:33:00Z")

</div>

Ocaml beginner here!

I am trying to write a program that will process both Midi and OSC (open sound control) messages. The idea is to generate midi algorithmically and controlling the parameters of the program through OSC messages.

For Midi I/O, I have a [wrapper](https://github.com/casperschipper/ocaml-cisp/blob/cisp2/src/jackMidi/caml_jack_midi.c) around “jackd” C API and for OSC I use [liblo package](https://opam.ocaml.org/packages/lo/). Both of these work without issue when they are used separately.

However, I run into problems if I try to combine the two in parallel (using Thread.create & Thread.join).  
I think I need threads because both functions are blocking (the OSC receive function (S.recv) blocks until it receives a message, so I cannot call it directly in the Midi generating callback).

```auto

let receive_osc_thread () =
  let port = 7777 in
  let s = S.create port myHandler in
  while true do
    S.recv s (* this blocks until a message is received, therefore I need the Thread. *)
  done

let () =
  let t1 = Thread.create midi_thread () in
  let t2 = Thread.create receive_osc_thread () in
  List.iter Thread.join [t2;t1]

```

It tends to crash after some time with a “segmentation fault: 11”. I have tried acquiring and releasing the caml runtime (as in: [Chapter&nbsp;20&nbsp;&nbsp;Interfacing C with OCaml](https://www.cs.cornell.edu/courses/cs3110/2020sp/manual-4.8/intfc.html#sec483)) in various locations of the C code of my Jack Midi client, but cannot get rid of the segfaults.

My question: why do segfaults occur when I combine two threads? Would there be a better way to combine two blocking C calls like this? I have been reading [Threads](https://ocaml.github.io/ocamlunix/threads.html), but feel that maybe my general approach is just not right :-).

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [September 10, 2021, 4:54pm UTC](https://discuss.ocaml.org/t/problem-with-segmentation-fault-running-c-threads/8466/2 "2021-09-10T16:54:02Z")

</div>

> [@Casper\_Schipper](#):
>
> It tends to crash after some time with a “segmentation fault: 11”. I have tried acquiring and releasing the caml runtime (as in: [Chapter 20 Interfacing C with OCaml](https://www.cs.cornell.edu/courses/cs3110/2020sp/manual-4.8/intfc.html#sec483)) in various locations of the C code of my Jack Midi client, but cannot get rid of the segfaults.

As a general remark, one likely reason for a segfault is a bug in your C bindings. When releasing the runtime lock, you must make sure that the runtime is not accessed in any way by any other thread.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![Casper\_Schipper](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/casper_schipper/32/2990_2.png) [@Casper\_Schipper](https://discuss.ocaml.org/u/Casper_Schipper)\
**Post date:** [September 12, 2021, 6:17pm UTC](https://discuss.ocaml.org/t/problem-with-segmentation-fault-running-c-threads/8466/3 "2021-09-12T18:17:45Z")

</div>

> [@nojb](#):
>
> you must make sure that the runtime is not accessed in any way by any other thread.

Thank you for this advice, I sieved through the C part once again, and after some adjustments, the segfaults have gone away! I’ll keep testing for a few hours to make sure. 😅

Inspired by what I saw in this [portaudio package](https://github.com/savonet/ocaml-portaudio/blob/master/src/portaudio_stubs.c) I inserted a call to caml\_c\_thread\_register() at the start of my process callback. “Process” is the function that fills the midi buffer, called by jackd itself at regular intervals. I further added a runtime lock/unlock around a critical section in this process function and this appears to do the trick. I will certainly not claim that I know why or how these additions exactly make it work, but if I remove or change any part of it and the segfaults are back. So for now, I assume this is how it should be done, and burn my time on more enjoyable things, like programming in ocaml itself 😉 .

---

<div class="post-metadata">

**Author:** ![gadmm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gadmm/32/877_2.png) [@gadmm](https://discuss.ocaml.org/u/gadmm)\
**Post date:** [September 13, 2021, 3:37pm UTC](https://discuss.ocaml.org/t/problem-with-segmentation-fault-running-c-threads/8466/4 "2021-09-13T15:37:20Z")

</div>

> ```c
> caml_register_global_root(&midi_msg_array_out); // protect this pointer from the Ocaml garbage collector
> 
> ```

I am still worried about this making a global root out of a variable on the stack, without any deregistration call. You already have it as a local root, and it should be enough. I am surprised it does not crash.

---

<div class="post-metadata">

**Author:** ![Casper\_Schipper](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/casper_schipper/32/2990_2.png) [@Casper\_Schipper](https://discuss.ocaml.org/u/Casper_Schipper)\
**Post date:** [September 13, 2021, 6:43pm UTC](https://discuss.ocaml.org/t/problem-with-segmentation-fault-running-c-threads/8466/5 "2021-09-13T18:43:29Z")

</div>

good catch: I was able to remove these two `caml_register_global_root` calls without consequence.

---

<div class="post-metadata">

**Author:** ![mbacarella](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mbacarella/32/6765_2.png) [@mbacarella](https://discuss.ocaml.org/u/mbacarella)\
**Post date:** [September 14, 2021, 1:42pm UTC](https://discuss.ocaml.org/t/problem-with-segmentation-fault-running-c-threads/8466/6 "2021-09-14T13:42:01Z")

</div>

> [@Casper\_Schipper](#):
>
> For Midi I/O, I have a [wrapper](https://github.com/casperschipper/ocaml-cisp/blob/cisp2/src/jackMidi/caml_jack_midi.c) around “jackd” C API and for OSC I use [liblo package](https://opam.ocaml.org/packages/lo/). Both of these work without issue when they are used separately.

I’m not quite sure of your exact requirements, but I thought I should mention OCaml [portmidi library](https://github.com/mbacarella/portmidi) bindings now exist and will be released to opam in the next few weeks.
