With ThreadSanitizer support to be part of OCaml 5.2, we (@otini and I) wanted to let you know about a couple of news on the subject.
If you don’t already know about it, ThreadSanitizer (also known as TSan) is a special instrumentation added to your program to detect data races.
Quick pedantic reminder with the definition of a data race:
A data race is when two or more threads access the same memory location concurrently, and at least one of the accesses is a write. Data races can lead to particularly hard-to-debug problems.
We are pleased to announce that we have backported TSan to OCaml 5.1, so that you can start using it right away. This version benefits from a more up-to-date compiler than the 5.0 backport, and will also perform better as many improvements have been made to the TSan integration. The impact on your program runtime under TSan will be lower.
To get it running on your machine, it’s as simple as:
opam update opam switch create 5.1.0+tsan
We had the opportunity to present TSan at the ICFP 2023 Ocaml Workshop, unfortunately the recording isn’t available yet. But if you’re interested in learning more of about how TSan works, and the challenges we faced in integrating it with the OCaml compiler we’ve written an article at https://tarides.com/blog/2023-10-18-off-to-the-races-using-threadsanitizer-in-ocaml/!
For a more hands-on experience, @jmid has also added a tutorial
Transitioning to Multicore with ThreadSanitizer, which walks you through the steps of installing TSan, using it to detect data races in one example, and addressing them. It’s available at https://www.ocaml.org/docs/multicore-transition.
Happy data race hunting, and have a fun with parallelism!