Cannot compile with `STAN_THREADS=true` without `CXXFLAGS += -fsanitize=undefined`

Hello everyone, I’m having a small issue where compiling a Stan model file with STAN_THREADS=true raises an error if I do not compile it with CXXFLAGS += -fsanitize=undefined.
This happens in my model as well as with the provided example Bernoulli model file.

Here are the steps to reproduce the bug:

  1. Clone the cmdstan Github repository ;
  2. Add the lines STAN_THREADS=true to make/local;
  3. Compile the example Bernoulli file;
  4. Get GCC error.

The error log is fairly large (almost 82k lines) because it seems to be repeating the same block over and over again.
I will not place it here, becaue it includes the symbol “`”, which makes it render poorly.
If you are interested, I have compressed the output (both STDOUT and STDERR) and compressed it into a tarball, which I will share below:
error.tar.gz.txt (572.9 KB)
(NOTE: In order to get it to upload to this forum I had to cheat the system and pretend this is a .txt file… it is actually not! It’s a compressed tarball!).

However, if I add a new step, where I compile the model file with a new line saying CXXFLAGS += -fsanitize=undefined on the file make/local, then all goes well.

One additional thing I would like to say is that even though I am compiling with the STAN_THREADS=true flag, running the sampler with 4 chains and 4 threads in parallel still outputs to different output files.
Could this be a symptom of something else going wrong?

I should say that I stated this as a bug because 1) I don’t see any reason why this should be a requirement and 2) it’s not written in the documentation that catching undefined behavior is a requirement to run the code in parallel.


This was tested with cmdstan versoin 2.30.1.

This is true on two system which I have tested, one of them is my laptop:

  • 5.4.225-1-MANJARO #1 SMP PREEMPT Sat Nov 26 00:40:25 UTC 2022 x86_64 GNU/Linux
  • gcc (GCC) 11.3.0

And the other is a desktop:

  • 6.0.8-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 10 Nov 2022 21:14:24 +0000 x86_64 GNU/Linux
  • gcc (GCC) 12.2.0

no, that’s the correct behavior w/r/t Stan output files. I can’t answer your Linux/gcc questions, maybe another Stan dev can - @rok_cesnovar ?

Oh you are correct, I didn’t read the documentation correctly, thanks.

Huh, I am using gcc 11.3.0 on Ubuntu 22.04 and do not have this behavior. It certainly isn’t a requirement of our code. I wonder if different distributions are compiling GCC with different things enabled by default?

A couple more questions:

Did you do anything different when running make build?

Do you have LDFLAGS or CXXFLAGS set to anything in your environment?

This does not happen when you don’t enable threading?

If you run a make clean-all first, does the same output get generated?

I’m using the pre-built binary provided directly from the Github repository.


No it does not. Without threading everything compiles just fine.

As I mentioned before, I’m using the pre-built binary.

I will try the following (as soon as I have the time to do so):

  1. Re-clone the repository to see if I accidentally made a change somewhere;
  2. Compile the binary for my system to see if the error goes away.

I believe the only pre-built binaries we provide on GitHub are specifically for use on Google Collab machines