Random but consistent C stack error on Windows 10

I continue to get the C stack error that crashes R on Windows 10. I have run Stan with nothing in makevars.win and with CXX14FLAGS=-O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 but I continue to get this error.

Models compile fine and I can run the model but after running about 3 - 5 Stan models I’ll get the C stack error that crashes R. Normally this happens at the end of sampling but sometimes it just seems to happen randomly. This seems to be a problem with Rtools and not with Stan per se but is compounded by the fact that I need C++ to run rstan.

Can you try running them a few times and running gc() in between?

Still getting it

This successfully crashes R with the C stack error every time

library(rstan)
options(mc.cores = parallel::detectCores())
N <- 1000
mu <- c(-0.75, 0.75);
sigma <- c(1, 1);
lambda <- 0.4
z <- rbinom(N, 1, lambda) + 1;
y <- rnorm(N, mu[z], sigma[z]);


stan_rdump(c("N", "y"), file="mix.data.R")

input_data <- read_rdump("mix.data.R")
singular_fit <- stan(file='..\\mix_betancourt_fun.stan', data=input_data[1:2],
                     chains = 3, iter = 600, warmup = 200, 
                     control = list(adapt_delta = 0.9),
                     seed = 483892929)
gc()
singular_fit

singular_fit2 <- stan(file='..\\mix_betancourt.stan', data=input_data[1:2],
                     chains = 3, iter = 600, warmup = 200, 
                     control = list(adapt_delta = 0.9),
                     seed = 483892929)
gc()
singular_fit2 

singular_fit <- stan(file='..\\mix_betancourt_case.stan', data=input_data[1:2],
                     chains = 3, iter = 600, warmup = 200, 
                     control = list(adapt_delta = 0.9),
                     seed = 483892929)
gc()

mix_betancourt_case.stan (389 Bytes) mix_betancourt_fun.stan (677 Bytes) mix_betancourt.stan (545 Bytes)

This looks to be the same as an issue currently open on the RStan github: https://github.com/stan-dev/rstan/issues/844

1 Like

To be clear, I get this crash without calling gc() too.

So the following crashes for you?

library(rstan)
options(mc.cores = parallel::detectCores())
N <- 1000
mu <- c(-0.75, 0.75);
sigma <- c(1, 1);
lambda <- 0.4
z <- rbinom(N, 1, lambda) + 1;
y <- rnorm(N, mu[z], sigma[z]);


stan_rdump(c("N", "y"), file="mix.data.R")

input_data <- read_rdump("mix.data.R")
singular_fit <- stan(file='mix_betancourt_fun.stan', data=input_data[1:2],
                     chains = 3, iter = 600, warmup = 200, 
                     control = list(adapt_delta = 0.9),
                     seed = 483892929)

singular_fit

singular_fit2 <- stan(file='mix_betancourt.stan', data=input_data[1:2],
                      chains = 3, iter = 600, warmup = 200, 
                      control = list(adapt_delta = 0.9),
                      seed = 483892929)

singular_fit2 

singular_fit <- stan(file='mix_betancourt_case.stan', data=input_data[1:2],
                     chains = 3, iter = 600, warmup = 200, 
                     control = list(adapt_delta = 0.9),
                     seed = 483892929)

This successfully crashes R. I just need to trigger recompilation by saving a new empty line in the stan file.

library(rstan)
options(mc.cores = parallel::detectCores())
N <- 1000
mu <- c(-0.75, 0.75);
sigma <- c(1, 1);
lambda <- 0.4
z <- rbinom(N, 1, lambda) + 1;
y <- rnorm(N, mu[z], sigma[z]);


stan_rdump(c("N", "y"), file="mix.data.R")
input_data <- read_rdump("mix.data.R")

file_name <- '..\\mix_betancourt_fun.stan'
for (i in 1:10){
  print(i)
  sink(file_name, append = T)
  writeLines("\n")
  sink()
  singular_fit <- stan(file = file_name, data=input_data[1:2],
                       chains = 3, iter = 200, warmup = 100,
                       control = list(adapt_delta = 0.9),
                       seed = 483892929)
  # print(singular_fit)
}

Great, that narrows it down. So the crash only occurs when the model is re-compiled (or when a new model is compiled). The last thing to check is whether the crash occurs when the result of each model is assigned to a different output (rather than overwriting the singular_fit object every time).

Can you test whether the following crashes for you?

library(rstan)
options(mc.cores = parallel::detectCores())
N <- 1000
mu <- c(-0.75, 0.75);
sigma <- c(1, 1);
lambda <- 0.4
z <- rbinom(N, 1, lambda) + 1;
y <- rnorm(N, mu[z], sigma[z]);


stan_rdump(c("N", "y"), file="mix.data.R")
input_data <- read_rdump("mix.data.R")

file_name <- '..\\mix_betancourt_fun.stan'
res = list()
for (i in 1:10){
    print(i)
    sink(file_name, append = T)
    writeLines("\n")
    sink()
    res[[i]] <- stan(file = file_name, data=input_data[1:2],
                         chains = 3, iter = 200, warmup = 100,
                         control = list(adapt_delta = 0.9),
                         seed = 483892929)
    # print(singular_fit)
}
2 Likes

Great, that does not crash my R session.

2 Likes

Great! It looks like the crashing is the same as the previously-linked RStan github issue. So it looks like repeatedly assigning new RStan models to the same output/object causes a crash. Good to have confirmation!

2 Likes