The problem appears to be in writeFunction callback modifying caml_local_roots without acquiring runtime lock first. writeFunction.diff fixes this problem. Most probably other callbacks need the same fix as well.
The code from #2433039 (https://sourceforge.net/tracker/?func=detail&aid=2433039&group_id=72276&atid=533973) running in multiple threads results in segfaults or lockups. See attached file.