stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.02k stars 264 forks source link

Runtime error crashes R session on macOS Sonoma (arm64) #1117

Open ltalluto opened 4 months ago

ltalluto commented 4 months ago

Summary:

Models that contain runtime errors crash my R session across many versions of rstan.

Description:

Any model that contains a runtime error is crashing my R session. Models compile fine. If no runtime errors occur models sample fine in most cases, although I am also experiencing somewhat random crashes. For example using cholesky_decompose will sometimes also produce a crash, this is less reproducible. I have tried the current CRAN versions (2.32.5), development versions
(2.33.1.9000), and have tried downgrading to rstan 2.21.8 with StanHeaders 2.26.28, all of them produce the same crash. My mac is an M1 on Sonoma 14.3.1, R version 4.3.2. An older intel mac on Ventura 13.6.4 has expected behaviour with no crashes. I can confirm that rstan was working normally as of late December, something must have changed since then.

Reproducible Steps:

This code reliably produces a crash on all of the versions mentioned above. Expected output is a runtime error due to the missing 'x' variable in the last line.

library(rstan)
set.seed(123)

stmod = "
data {
    int <lower = 0> n;
    vector [n] x;
}
parameters {
    real mu;
    real <lower = 0> sig;
}
model {
    x ~ normal(mu, sig);
    mu ~ normal(0, 10);
    sig ~ cauchy(0, 5);
}
"

mod = stan_model(model_code = stmod)
samp_works = sampling(mod, data = list(n = 5, x = rnorm(5, 0, 1)), iter = 100, chains = 1)

# missing x in data list, should produce runtime error.
samp_crashes = sampling(mod, data = list(n = 5), iter = 100, chains = 1)

RStan Version:

Tried with 2.33.1.9000, 2.32.5, and 2.21.8.

R Version:

4.3.2

Operating System:

macOS 14.3.1.

ltalluto commented 4 months ago

Updating to add that I've managed to get it working by switching compilers. My .R/Makevars included the line:

CXX=/opt/local/bin/clang++ -fopenmp

This points to clang 14.0.6 installed via macports. Commenting this line out and reinstalling rstan from source via CRAN eliminates the crashes (using the system C++ compiler).

Leaving this issue open in case you want to investigate the crashes further, but happily resolved on my end.