Windows RStudio crashing with RStan model loaded


#1

Hey,

I’m in a course with @betanalpha right now and I think every Windows user is seeing RStudio crash sort of randomly at some point after loading RStan. There are 4 people that I know of with the same issue and at least some of them are on RStan 2.18 (maybe all of them). @bgoodri have you seen anything like this? Are there easy instructions somewhere for trying RStan 2.17.x for these folks? I pointed them to the cloud but the wifi is not working.

@jonah Any progress on RCmdStan? 😂


#2

Are they using threading?


#3

To install a specific version of RStan, this worked for me:

  1. remove.packages("rstan"); if (file.exists(".RData")) file.remove(".RData")
  2. install.packages("devtools")
  3. Create decent Makevars if we don’t have it:
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars.win")
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS=-O3 -march=native",
    "CXX14 = g++ -m$(WIN) -std=c++1y",
    "CXX11FLAGS=-O3 -march=native",
    file = M, sep = "\n", append = TRUE)
  1. Restart R[Studio]
  2. library(devtools); install_version("rstan", version="2.17.4")

#4

Nope, at least not on purpose. From some light experimentation, it seems like it might only happen when using the algebraic solver (possibly only in 2.18).


#5

We had a divided-by-zero problem on windows with mingw that crashed the whole session.


#6

What’s RCmdStan?


#7

Do you have an example of algebraic_solver() that crashes things?


#8
functions {
  vector tail_delta(vector y, vector theta, real[] x_r, int[] x_i) {
    vector[1] deltas;
    deltas[1] = 2 * (normal_cdf(theta[1], 0, exp(y[1])) - 0.5) - 0.99;
    return deltas;
} }
transformed data {
  vector[1] y_guess = [log(5)]';
  vector[1] theta = [15]';
  vector[1] y;
  real x_r[0];
  int x_i[0];
  y = algebra_solver(tail_delta, y_guess, theta, x_r, x_i);
  print("Standard deviation = ", exp(y[1]));
}
generated quantities {
  real sigma = exp(y[1]);
}

#9

I have not been able to replicate this on the Windows computer in my office. If there is anyone who had this problem that wants to stay around at the end of the workshop to recreate it, I could use some more information, such as what was the error message / stack trace and whether they were tuning the compiler to the processor.


#10

Talked with one of the people with the issue and he says the RStudio crash happens about half the time. Got him to run the model without either march=native or mtune=native and with -O2 instead of -O3 and it still crashed RStudio on the 2nd stan call.


#11

OK. I can trigger it if I call it enough times. The backtrace does not help that much though. It looks like something may be getting left on the heap that was supposed to be cleaned up.

SAMPLING FOR MODEL 'crash' NOW (CHAIN 1).
Chain 1: Iteration: 1 / 1 [100%]  (Sampling)
Chain 1:
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                0.003 seconds (Sampling)
Chain 1:                0.003 seconds (Total)
Chain 1:
warning: HEAP[Rterm.exe]:
warning: Heap block at 0000000017A8FCB0 modified at 0000000017A8FCC8 past reques
ted size of 8


Program received signal SIGTRAP, Trace/breakpoint trap.
0x0000000077a69372 in ntdll!EtwEventProviderEnabled ()
   from C:\windows\SYSTEM32\ntdll.dll
(gdb) bt
#0  0x0000000077a69372 in ntdll!EtwEventProviderEnabled ()
   from C:\windows\SYSTEM32\ntdll.dll
#1  0x0000000077a795f1 in ntdll!RtlFindClearBits ()
   from C:\windows\SYSTEM32\ntdll.dll
#2  0x0000000077a40290 in ntdll!longjmp () from C:\windows\SYSTEM32\ntdll.dll
#3  0x0000000077a84869 in ntdll!RtlLogStackBackTrace ()
   from C:\windows\SYSTEM32\ntdll.dll
#4  0x0000000077a45972 in ntdll!longjmp () from C:\windows\SYSTEM32\ntdll.dll
#5  0x000007fefe0010c8 in msvcrt!free () from C:\windows\system32\msvcrt.dll
#6  0x000000006ae46c40 in rstan!effective_sample_size ()
   from C:\Users\Stan\Documents\R\win-library\3.5\rstan\libs\x64\rstan.dll
#7  0x000000006c7f3ef9 in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#8  0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#9  0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#10 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#11 0x000000006c7f531a in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#12 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#13 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#14 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#15 0x000000006c807e89 in R_forceAndCall ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#16 0x000000006c8eab2a in rwarn_ () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#17 0x000000006c836b77 in do_Rprofmem ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#18 0x000000006c7ed988 in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#19 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#20 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#21 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#22 0x000000006c7f531a in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#23 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#24 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#25 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#26 0x000000006c7f531a in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#27 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#28 0x000000006c8003dc in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#29 0x000000006c7fdf2c in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#30 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#31 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#32 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#33 0x000000006c7f531a in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#34 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#35 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#36 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#37 0x000000006c7ffd02 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#38 0x000000006c8003dc in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#39 0x000000006c7fdf2c in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#40 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#41 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#42 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#43 0x000000006c7ffd02 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#44 0x000000006c7fff06 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#45 0x000000006c802e76 in R_execMethod ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#46 0x000000006c7fff06 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#47 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
---Type <return> to continue, or q <return> to quit---
#48 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#49 0x000000006c7f531a in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#50 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#51 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#52 0x000000006c80265e in R_execMethod ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#53 0x0000000064a42cf2 in methods!R_dispatchGeneric ()
   from C:\Program Files\R\R-3.5.1\library\methods\libs\x64\methods.dll
#54 0x000000006c83b972 in R_set_standardGeneric_ptr ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#55 0x000000006c800062 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#56 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#57 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#58 0x000000006c7f531a in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#59 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#60 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#61 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#62 0x000000006c7f531a in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#63 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#64 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#65 0x000000006c80265e in R_execMethod ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#66 0x0000000064a42cf2 in methods!R_dispatchGeneric ()
   from C:\Program Files\R\R-3.5.1\library\methods\libs\x64\methods.dll
#67 0x000000006c83b972 in R_set_standardGeneric_ptr ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#68 0x000000006c7f7b5d in R_initAssignSymbols ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#69 0x000000006c7ffd41 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#70 0x000000006c801354 in R_cmpfun1 ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#71 0x000000006c8023e2 in Rf_applyClosure ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#72 0x000000006c7ffd02 in Rf_eval () from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#73 0x000000006c861026 in Rf_PrintDefaults ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#74 0x000000006c826337 in Rf_ReplIteration ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#75 0x000000006c826581 in Rf_ReplIteration ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#76 0x000000006c826612 in run_Rmainloop ()
   from C:\PROGRA~1\R\R-35~1.1\bin\x64\R.dll
#77 0x000000000040171c in ?? ()
#78 0x000000000040155a in ?? ()
#79 0x00000000004013e8 in ?? ()
#80 0x000000000040151b in ?? ()
#81 0x00000000777b59cd in KERNEL32!BaseThreadInitThunk ()
   from C:\windows\system32\kernel32.dll
#82 0x0000000077a1385d in ntdll!RtlUserThreadStart ()
   from C:\windows\SYSTEM32\ntdll.dll
#83 0x0000000000000000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb)

#12

Nice, at least it’s replicating. Maybe time to break out Clang’s address sanitizer or something?


#13

See the overall roadmap. We’re going to build a lightweight wrapper along the same lines as the existing PyCmdStan (which we did not build, but which we will build out), which calls CmdStan out of process. This avoids all the C++ entanglements with R and CRAN. One nice side effect is that it won’t crash sessions. On reason we want to do this that we have install problems all the time when teaching.


#14

I have a similar problem. I have recently had my computer upgraded and some of my rstan models cause Rstudio to crash. Rstudio says “R session Aborted R encountered a fata error and the session was terminated”, however the model is able to run in RGui. In the event log it says the error is caused by the .dll file.

I am able to run some other rstan models in Rstudio, although I have a few that cause this error. I have asked colleagues at work to run the scripts that generate the problems and not one person, out of 7, was able to recreate the error I received. One person had exactly the same operating system, versions of Rstudio, Rstan and Rtools as me and was able to run the scripts fine.

I have tried uninstalling everything and reinstalling several times, often with different versions and this problem still occurs.

Does anybody have any ideas as to what is causing this problem?

I am using
Operating system: Windows 10
R : 3.5.1
rstan: rstan_2.18.2
Rtools: 3.4
Rstudio: Version:1.0 1.1.463

The following model is one of the models that causes Rstudio to break:

data{
  int N_obs; // number of obs
  int N_obsG; // number of obs
  int N; // number of species
  int Time; // years
  int nGear; // number of gears
  // observartions
  int spec[N_obs]; //species number
  int year[N_obs]; // year of catch
  vector [N_obs] obs_y; // number of obs there
  vector [N_obs] freq; // number of obs there
  // Gaussian only
  int specG[N_obsG]; //species number
  int yearG[N_obsG]; // year of catch
  vector [N_obsG] obs_yG; // number of obs there
  vector [N_obsG] freqG; // number of obs there
  vector [N_obsG] s_yG; // number of obs there
  // gears
  vector [nGear] obs_g;
  vector [nGear] freq_g;
  vector [nGear] s_g;
  // priors
  vector [N] initial_m;
  vector [N] initial_sd;
  vector [N] mu_0;
  vector [N] mu_0_v;
}
parameters{
  vector [N] mu_raw[Time-1];
  vector <lower=0> [N] sigma_mu_sq;
  vector <lower=0> [N] sigma_dyn_sq;
  vector [nGear] mu_g;
  vector <lower=0> [nGear] sigma_g_sq;
  vector <lower=0,upper=1> [N] p_a1;
  ordered [2] mu_l[N];
  vector <lower=0> [N] sigma_mu_l_sq;
  // 
}
transformed parameters{
  vector [N] mu[Time];
  vector <lower=0> [N] sigma_mu = sqrt(sigma_mu_sq);
  vector <lower=0> [N] sigma_dyn = sqrt(sigma_dyn_sq);
  vector <lower=0> [N] sigma_mu_l = sqrt(sigma_mu_l_sq);
  vector <lower=0> [nGear] sigma_g = sqrt(sigma_g_sq);
  mu[1] = to_vector(mu_l[,2]);
  for(t in 2:Time)
  {
    mu[t] = mu[t-1] + sigma_dyn .* mu_raw[t-1];
  }
}
model{
  //priors
  sigma_mu_sq ~ inv_gamma(2,2);
  sigma_g_sq ~ cauchy(0,1);
  sigma_dyn_sq ~ inv_gamma(10,0.01);
  mu_g ~ normal(0,10);
  // the prior for the size
  for  (i in 1:N)
  {
    mu_l[i] ~ normal(0,10);
  }
  //mu_l[1] ~ normal(mu_0,mu_0_v);
  sigma_mu_l_sq ~ inv_gamma(10,0.1);
  // the dynamics for the median length
  for (t in 1:(Time-1))
  {
    mu_raw[t] ~ normal(0,1);
  }
  for (i in 1:N_obsG)
  {
    {
      obs_yG[i] ~ normal(mu[year[i],specG[i]],sigma_mu[specG[i]] / sqrt(freqG[i])); // for each sepecies for each year
      if (s_yG[i] > 0)
      {
        (s_yG[i] * (freqG[i] - 1.0) / sigma_mu_sq[specG[i]]) ~ chi_square(freqG[i] - 1.0); // variance for each species, for each year
      }
    }
  }

  for (i in 1:nGear)
  {
    obs_g[i] ~ normal(mu_g[i],sigma_g[i] / sqrt(freq_g[i])); // for each gear
    if (freq_g[i] > 1)
    {
      (s_g[i] * (freq_g[i]-1.0) / sigma_g_sq[i]) ~ chi_square(freq_g[i] - 1.0);
    }
  }
}

#15

That is weird. Do you have old .rds files for the models sitting in the working directory?


#16

I found a .rds file in the folder which I deleted and I then compiled the model and successfully ran it in Rstudio. I then slightly changed the .stan model, recompiled the model and then when I ran the new model I got the same error as before, with Rstudio crashing out.

I have checked the folder now and I cannot see any .rds

Here’s the script to do this files.all_diff_test.r (2.3 KB)

The data is too large to send, sorry.