When I use `cmdstanr`

from an R instance within Emacs, compiling is fine but when it samples, the R instances always closes. It is like it gets terminated or crashes. When I run it directly from a terminal, it is fine. Windows 11 pro latest versions of `cmdstanr`

and `cmdstan`

.

Here is the simple code to reproduce the issue.

```
library(cmdstanr)
model <- "
parameters {
real a;
}
model {
a ~ normal(0, 1);
}
"
modfile <- write_stan_file(
model,
dir = getOption("cmdstanr_write_stan_file_dir", tempdir()),
basename = "simplemodel.stan")
test_model <- cmdstan_model(file.path(getOption("cmdstanr_write_stan_file_dir", tempdir()), "simplemodel.stan"))
fit <- test_model$sample()
```

and here is what happens everytime I run it within Emacs:

```
> test_model <- cmdstan_model(file.path(getOption("cmdstanr_write_stan_file_dir", tempdir()), "simplemodel.stan"))
Compiling Stan program...
> fit <- test_model$sample()
Running MCMC with 4 sequential chains...
Chain 1 Iteration: 1 / 2000 [ 0%] (Warmup)
[snip for length]
Chain 4 Iteration: 2000 / 2000 [100%] (Sampling)
Chain 4 finished in 0.0 seconds.
All 4 chains finished successfully.
Mean chain execution time: 0.0 seconds.
Total execution time: 1.4 seconds.
>
Process R finished at Fri Aug 11 11:32:41 2023
```

So its like it all works, but somehow after it finishes sampling, everything just shuts down. On terminal, once it finishes sample I end up back at the `R`

prompt with the posterior samples saved in `fit`

.

I don’t think this is a `cmdstan`

/ `cmdstanr`

problem per se but hoping someone here may have thoughts / ideas. I use Emacs extensively for my analysis and more so I really don’t want to switch to some other interface.

1 Like

Sorry about the delay in getting to this. Emacs and Cmdstanr is not my area but some folks here do that. This thread here What are the features which would help you using cmdstan in Emacs? might help you direct your question to particular person.

Also this will bump the thread to the top so some new eyes can see it.

Indeed a very weird situation. Can you add `options("cmdstanr_verbose" = TRUE)`

to enable a bit more debug info and try again? If you could screenshot if anything weird is printed before crashing, that would be very helpful.

1 Like

My turn to apologise. I tried it out with the latest version as well. Same result.

```
> fit <- test_model$sample()
Running MCMC with 4 sequential chains...
Running wsl cd /tmp/tmp.fpC6pFLgph "&&" ./simplemodel "id=1" random "seed=636988798" output \
"file=/tmp/tmp.vHkUxrElwq/simplemodel-202309211641-1-2892c6.csv" \
"profile_file=/tmp/tmp.vHkUxrElwq/simplemodel-profile-202309211641-1-4ccd18.csv" "method=sample" "save_warmup=0" \
"algorithm=hmc" "engine=nuts" adapt "engaged=1"
Chain 1 method = sample (Default)
Chain 1 sample
Chain 1 num_samples = 1000 (Default)
Chain 1 num_warmup = 1000 (Default)
Chain 1 save_warmup = 0 (Default)
Chain 1 thin = 1 (Default)
Chain 1 adapt
Chain 1 engaged = 1 (Default)
Chain 1 gamma = 0.050000000000000003 (Default)
Chain 1 delta = 0.80000000000000004 (Default)
Chain 1 kappa = 0.75 (Default)
Chain 1 t0 = 10 (Default)
Chain 1 init_buffer = 75 (Default)
Chain 1 term_buffer = 50 (Default)
Chain 1 window = 25 (Default)
Chain 1 algorithm = hmc (Default)
Chain 1 hmc
Chain 1 engine = nuts (Default)
Chain 1 nuts
Chain 1 max_depth = 10 (Default)
Chain 1 metric = diag_e (Default)
Chain 1 metric_file = (Default)
Chain 1 stepsize = 1 (Default)
Chain 1 stepsize_jitter = 0 (Default)
Chain 1 num_chains = 1 (Default)
Chain 1 id = 1 (Default)
Chain 1 data
Chain 1 file = (Default)
Chain 1 init = 2 (Default)
Chain 1 random
Chain 1 seed = 636988798
Chain 1 output
Chain 1 file = /tmp/tmp.vHkUxrElwq/simplemodel-202309211641-1-2892c6.csv
Chain 1 diagnostic_file = (Default)
Chain 1 refresh = 100 (Default)
Chain 1 sig_figs = -1 (Default)
Chain 1 profile_file = /tmp/tmp.vHkUxrElwq/simplemodel-profile-202309211641-1-4ccd18.csv
Chain 1 num_threads = 1 (Default)
Chain 1 Gradient evaluation took 1e-06 seconds
Chain 1 1000 transitions using 10 leapfrog steps per transition would take 0.01 seconds.
Chain 1 Adjust your expectations accordingly!
Chain 1 Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 1 Iteration: 100 / 2000 [ 5%] (Warmup)
Chain 1 Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 1 Iteration: 300 / 2000 [ 15%] (Warmup)
Chain 1 Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 1 Iteration: 500 / 2000 [ 25%] (Warmup)
Chain 1 Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 1 Iteration: 700 / 2000 [ 35%] (Warmup)
Chain 1 Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 1 Iteration: 900 / 2000 [ 45%] (Warmup)
Chain 1 Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 1 Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 1 Iteration: 1100 / 2000 [ 55%] (Sampling)
Chain 1 Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 1 Iteration: 1300 / 2000 [ 65%] (Sampling)
Chain 1 Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 1 Iteration: 1500 / 2000 [ 75%] (Sampling)
Chain 1 Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 1 Iteration: 1700 / 2000 [ 85%] (Sampling)
Chain 1 Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 1 Iteration: 1900 / 2000 [ 95%] (Sampling)
Chain 1 Iteration: 2000 / 2000 [100%] (Sampling)
Chain 1 Elapsed Time: 0.001 seconds (Warm-up)
Chain 1 0.002 seconds (Sampling)
Chain 1 0.003 seconds (Total)
Chain 1 finished in 0.0 seconds.
Running wsl cd /tmp/tmp.fpC6pFLgph "&&" ./simplemodel "id=2" random "seed=636988798" output \
"file=/tmp/tmp.vHkUxrElwq/simplemodel-202309211641-2-2892c6.csv" \
"profile_file=/tmp/tmp.vHkUxrElwq/simplemodel-profile-202309211641-2-4ccd18.csv" "method=sample" "save_warmup=0" \
"algorithm=hmc" "engine=nuts" adapt "engaged=1"
Chain 2 method = sample (Default)
Chain 2 sample
Chain 2 num_samples = 1000 (Default)
Chain 2 num_warmup = 1000 (Default)
Chain 2 save_warmup = 0 (Default)
Chain 2 thin = 1 (Default)
Chain 2 adapt
Chain 2 engaged = 1 (Default)
Chain 2 gamma = 0.050000000000000003 (Default)
Chain 2 delta = 0.80000000000000004 (Default)
Chain 2 kappa = 0.75 (Default)
Chain 2 t0 = 10 (Default)
Chain 2 init_buffer = 75 (Default)
Chain 2 term_buffer = 50 (Default)
Chain 2 window = 25 (Default)
Chain 2 algorithm = hmc (Default)
Chain 2 hmc
Chain 2 engine = nuts (Default)
Chain 2 nuts
Chain 2 max_depth = 10 (Default)
Chain 2 metric = diag_e (Default)
Chain 2 metric_file = (Default)
Chain 2 stepsize = 1 (Default)
Chain 2 stepsize_jitter = 0 (Default)
Chain 2 num_chains = 1 (Default)
Chain 2 id = 2
Chain 2 data
Chain 2 file = (Default)
Chain 2 init = 2 (Default)
Chain 2 random
Chain 2 seed = 636988798
Chain 2 output
Chain 2 file = /tmp/tmp.vHkUxrElwq/simplemodel-202309211641-2-2892c6.csv
Chain 2 diagnostic_file = (Default)
Chain 2 refresh = 100 (Default)
Chain 2 sig_figs = -1 (Default)
Chain 2 profile_file = /tmp/tmp.vHkUxrElwq/simplemodel-profile-202309211641-2-4ccd18.csv
Chain 2 num_threads = 1 (Default)
Chain 2 Gradient evaluation took 1e-06 seconds
Chain 2 1000 transitions using 10 leapfrog steps per transition would take 0.01 seconds.
Chain 2 Adjust your expectations accordingly!
Chain 2 Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 2 Iteration: 100 / 2000 [ 5%] (Warmup)
Chain 2 Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 2 Iteration: 300 / 2000 [ 15%] (Warmup)
Chain 2 Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 2 Iteration: 500 / 2000 [ 25%] (Warmup)
Chain 2 Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 2 Iteration: 700 / 2000 [ 35%] (Warmup)
Chain 2 Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 2 Iteration: 900 / 2000 [ 45%] (Warmup)
Chain 2 Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 2 Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 2 Iteration: 1100 / 2000 [ 55%] (Sampling)
Chain 2 Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 2 Iteration: 1300 / 2000 [ 65%] (Sampling)
Chain 2 Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 2 Iteration: 1500 / 2000 [ 75%] (Sampling)
Chain 2 Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 2 Iteration: 1700 / 2000 [ 85%] (Sampling)
Chain 2 Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 2 Iteration: 1900 / 2000 [ 95%] (Sampling)
Chain 2 Iteration: 2000 / 2000 [100%] (Sampling)
Chain 2 Elapsed Time: 0.001 seconds (Warm-up)
Chain 2 0.003 seconds (Sampling)
Chain 2 0.004 seconds (Total)
Chain 2 finished in 0.0 seconds.
Running wsl cd /tmp/tmp.fpC6pFLgph "&&" ./simplemodel "id=3" random "seed=636988798" output \
"file=/tmp/tmp.vHkUxrElwq/simplemodel-202309211641-3-2892c6.csv" \
"profile_file=/tmp/tmp.vHkUxrElwq/simplemodel-profile-202309211641-3-4ccd18.csv" "method=sample" "save_warmup=0" \
"algorithm=hmc" "engine=nuts" adapt "engaged=1"
Chain 3 method = sample (Default)
Chain 3 sample
Chain 3 num_samples = 1000 (Default)
Chain 3 num_warmup = 1000 (Default)
Chain 3 save_warmup = 0 (Default)
Chain 3 thin = 1 (Default)
Chain 3 adapt
Chain 3 engaged = 1 (Default)
Chain 3 gamma = 0.050000000000000003 (Default)
Chain 3 delta = 0.80000000000000004 (Default)
Chain 3 kappa = 0.75 (Default)
Chain 3 t0 = 10 (Default)
Chain 3 init_buffer = 75 (Default)
Chain 3 term_buffer = 50 (Default)
Chain 3 window = 25 (Default)
Chain 3 algorithm = hmc (Default)
Chain 3 hmc
Chain 3 engine = nuts (Default)
Chain 3 nuts
Chain 3 max_depth = 10 (Default)
Chain 3 metric = diag_e (Default)
Chain 3 metric_file = (Default)
Chain 3 stepsize = 1 (Default)
Chain 3 stepsize_jitter = 0 (Default)
Chain 3 num_chains = 1 (Default)
Chain 3 id = 3
Chain 3 data
Chain 3 file = (Default)
Chain 3 init = 2 (Default)
Chain 3 random
Chain 3 seed = 636988798
Chain 3 output
Chain 3 file = /tmp/tmp.vHkUxrElwq/simplemodel-202309211641-3-2892c6.csv
Chain 3 diagnostic_file = (Default)
Chain 3 refresh = 100 (Default)
Chain 3 sig_figs = -1 (Default)
Chain 3 profile_file = /tmp/tmp.vHkUxrElwq/simplemodel-profile-202309211641-3-4ccd18.csv
Chain 3 num_threads = 1 (Default)
Chain 3 Gradient evaluation took 1e-06 seconds
Chain 3 1000 transitions using 10 leapfrog steps per transition would take 0.01 seconds.
Chain 3 Adjust your expectations accordingly!
Chain 3 Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 3 Iteration: 100 / 2000 [ 5%] (Warmup)
Chain 3 Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 3 Iteration: 300 / 2000 [ 15%] (Warmup)
Chain 3 Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 3 Iteration: 500 / 2000 [ 25%] (Warmup)
Chain 3 Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 3 Iteration: 700 / 2000 [ 35%] (Warmup)
Chain 3 Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 3 Iteration: 900 / 2000 [ 45%] (Warmup)
Chain 3 Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 3 Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 3 Iteration: 1100 / 2000 [ 55%] (Sampling)
Chain 3 Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 3 Iteration: 1300 / 2000 [ 65%] (Sampling)
Chain 3 Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 3 Iteration: 1500 / 2000 [ 75%] (Sampling)
Chain 3 Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 3 Iteration: 1700 / 2000 [ 85%] (Sampling)
Chain 3 Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 3 Iteration: 1900 / 2000 [ 95%] (Sampling)
Chain 3 Iteration: 2000 / 2000 [100%] (Sampling)
Chain 3 Elapsed Time: 0.001 seconds (Warm-up)
Chain 3 0.003 seconds (Sampling)
Chain 3 0.004 seconds (Total)
Chain 3 finished in 0.0 seconds.
Running wsl cd /tmp/tmp.fpC6pFLgph "&&" ./simplemodel "id=4" random "seed=636988798" output \
"file=/tmp/tmp.vHkUxrElwq/simplemodel-202309211641-4-2892c6.csv" \
"profile_file=/tmp/tmp.vHkUxrElwq/simplemodel-profile-202309211641-4-4ccd18.csv" "method=sample" "save_warmup=0" \
"algorithm=hmc" "engine=nuts" adapt "engaged=1"
Chain 4 method = sample (Default)
Chain 4 sample
Chain 4 num_samples = 1000 (Default)
Chain 4 num_warmup = 1000 (Default)
Chain 4 save_warmup = 0 (Default)
Chain 4 thin = 1 (Default)
Chain 4 adapt
Chain 4 engaged = 1 (Default)
Chain 4 gamma = 0.050000000000000003 (Default)
Chain 4 delta = 0.80000000000000004 (Default)
Chain 4 kappa = 0.75 (Default)
Chain 4 t0 = 10 (Default)
Chain 4 init_buffer = 75 (Default)
Chain 4 term_buffer = 50 (Default)
Chain 4 window = 25 (Default)
Chain 4 algorithm = hmc (Default)
Chain 4 hmc
Chain 4 engine = nuts (Default)
Chain 4 nuts
Chain 4 max_depth = 10 (Default)
Chain 4 metric = diag_e (Default)
Chain 4 metric_file = (Default)
Chain 4 stepsize = 1 (Default)
Chain 4 stepsize_jitter = 0 (Default)
Chain 4 num_chains = 1 (Default)
Chain 4 id = 4
Chain 4 data
Chain 4 file = (Default)
Chain 4 init = 2 (Default)
Chain 4 random
Chain 4 seed = 636988798
Chain 4 output
Chain 4 file = /tmp/tmp.vHkUxrElwq/simplemodel-202309211641-4-2892c6.csv
Chain 4 diagnostic_file = (Default)
Chain 4 refresh = 100 (Default)
Chain 4 sig_figs = -1 (Default)
Chain 4 profile_file = /tmp/tmp.vHkUxrElwq/simplemodel-profile-202309211641-4-4ccd18.csv
Chain 4 num_threads = 1 (Default)
Chain 4 Gradient evaluation took 1e-06 seconds
Chain 4 1000 transitions using 10 leapfrog steps per transition would take 0.01 seconds.
Chain 4 Adjust your expectations accordingly!
Chain 4 Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 4 Iteration: 100 / 2000 [ 5%] (Warmup)
Chain 4 Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 4 Iteration: 300 / 2000 [ 15%] (Warmup)
Chain 4 Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 4 Iteration: 500 / 2000 [ 25%] (Warmup)
Chain 4 Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 4 Iteration: 700 / 2000 [ 35%] (Warmup)
Chain 4 Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 4 Iteration: 900 / 2000 [ 45%] (Warmup)
Chain 4 Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 4 Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 4 Iteration: 1100 / 2000 [ 55%] (Sampling)
Chain 4 Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 4 Iteration: 1300 / 2000 [ 65%] (Sampling)
Chain 4 Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 4 Iteration: 1500 / 2000 [ 75%] (Sampling)
Chain 4 Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 4 Iteration: 1700 / 2000 [ 85%] (Sampling)
Chain 4 Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 4 Iteration: 1900 / 2000 [ 95%] (Sampling)
Chain 4 Iteration: 2000 / 2000 [100%] (Sampling)
Chain 4 Elapsed Time: 0.001 seconds (Warm-up)
Chain 4 0.002 seconds (Sampling)
Chain 4 0.003 seconds (Total)
Chain 4 finished in 0.0 seconds.
All 4 chains finished successfully.
Mean chain execution time: 0.0 seconds.
Total execution time: 1.7 seconds.
>
Process R finished at Thu Sep 21 16:42:00 2023
```

The verbose isn’t helping directly but perhaps indirectly. University is busy at the moment but I think I’ll explore whether I can turn more debugging on in `R`

. Not sure if crash dump logs possible but will look into it and try to report my findings as I get to them.

1 Like