R 3.5.1 complains about slot "mode"

I’m on Ubuntu 18.04 LTS with a fresh insatll of R 3.5.1 with rstan (Version 2.17.3, GitRev: 2e1f913d3ca3)

My models that used to work now all return
‘Error in FUN(X[[i]], …) :
trying to get slot “mode” from an object of a basic class (“NULL”) with no slots’
after having completed sampling.

I’m not sure if this is a bug or a feature. Would I need to change something in my stan code for R 3.5.1?

The sampling got messed up. We need more info to figure out why.

Try running with a single chain. The parallel interface can swallow some of error messages.

With just one chain it runs without errors. Only once I start adding chains it will give me that error message.
With 2 chains it gives slightly more info:

Error in sendMaster(try(eval(expr, env), silent = TRUE)) :
write error, closing pipe to the master
Error in FUN(X[[i]], …) :
trying to get slot “mode” from an object of a basic class (“NULL”) with no slots

If it runs on one chain, but fails on two it is possible you are running out of memory and one process is being killed. Can you run:
dmesg | egrep -i 'killed process' after a failure and see if anything comes up?

dmesg does not record anything. Probably because nothing is killed. R keeps on working - it only complains about trying to get a slot when there is none.
I have this problem on two different PC’s running R 3.5.1. Do your models run on that R version?

My models do run on R 3.5.1. When you run multiple threads, R forks off one process for each thread. If one of these gets killed, it doesn’t send anything back to the master process and you can end up with a NULL result in one or more of the chains.

The error you are getting is not that there is nothing in the slot - it’s that you are trying to get the mode attribute on a NULL object.

Try looking into the temporary directory. Each chain writes out some diagnostics to a file in the temporary directory and these may provide additional information. Run tempdir() to see R’s temporary directory and look through all of the files in this directory to see if any of the logs provide additional diagnostic information.

Thanks Aaron, for your help.

I checked my tmp file for error messages, but it remains empty. However, in the meantime I found a workaround:
Once the ‘shinystan’ package is loaded, I can run all my models with multiple chains on multiple threads just like i used to do – if ‘shinystan’ is not loaded, R returns the above mentioned error messages.
I can consistently reproduce this behavior on my laptop and on my work pc.

That seems quite odd. You shouldn’t need to load shinystan to run the models. Would you be able to post a minimal working example?

No problem:

library("rstan") 
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

schools <- '
data {
  int<lower=0> J; // number of schools 
  real y[J]; // estimated treatment effects
  real<lower=0> sigma[J]; // s.e. of effect estimates 
}
parameters {
  real mu; 
  real<lower=0> tau;
  real eta[J];
}
transformed parameters {
  real theta[J];
  for (j in 1:J)
    theta[j] = mu + tau * eta[j];
}
model {
  target += normal_lpdf(eta | 0, 1);
  target += normal_lpdf(y | theta, sigma);  
}
'

schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

tstmod <- stan_model(model_code = schools) 

fit <- sampling(tstmod, data = schools_dat, iter = 1000, chains = 4, verbose = TRUE)

> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 1).

> Gradient evaluation took 0.000116 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 1.16 seconds.
> Adjust your expectations accordingly!


> Iteration:   1 / 1000 [  0%]  (Warmup)
> Iteration: 100 / 1000 [ 10%]  (Warmup)
> Iteration: 200 / 1000 [ 20%]  (Warmup)
> Iteration: 300 / 1000 [ 30%]  (Warmup)
> Iteration: 400 / 1000 [ 40%]  (Warmup)
> Iteration: 500 / 1000 [ 50%]  (Warmup)
> Iteration: 501 / 1000 [ 50%]  (Sampling)
> Iteration: 600 / 1000 [ 60%]  (Sampling)
> Iteration: 700 / 1000 [ 70%]  (Sampling)
> Iteration: 800 / 1000 [ 80%]  (Sampling)
> Iteration: 900 / 1000 [ 90%]  (Sampling)
> Iteration: 1000 / 1000 [100%]  (Sampling)

>  Elapsed Time: 0.032092 seconds (Warm-up)
>     >        0.013621 seconds (Sampling)
>     >        0.045713 seconds (Total)

> Error in sendMaster(try(eval(expr, env), silent = TRUE)) : 
>   write error, closing pipe to the master

> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 2).

> Gradient evaluation took 0.000118 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 1.18 seconds.
> Adjust your expectations accordingly!


> Iteration:   1 / 1000 [  0%]  (Warmup)
> Iteration: 100 / 1000 [ 10%]  (Warmup)
> Iteration: 200 / 1000 [ 20%]  (Warmup)
> Iteration: 300 / 1000 [ 30%]  (Warmup)
> Iteration: 400 / 1000 [ 40%]  (Warmup)
> Iteration: 500 / 1000 [ 50%]  (Warmup)
> Iteration: 501 / 1000 [ 50%]  (Sampling)
> Iteration: 600 / 1000 [ 60%]  (Sampling)
> Iteration: 700 / 1000 [ 70%]  (Sampling)
> Iteration: 800 / 1000 [ 80%]  (Sampling)
> Iteration: 900 / 1000 [ 90%]  (Sampling)
> Iteration: 1000 / 1000 [100%]  (Sampling)

>  Elapsed Time: 0.03342 seconds (Warm-up)
>     >        0.01713 seconds (Sampling)
>     >        0.05055 seconds (Total)


> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 3).

> Gradient evaluation took 5.5e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.55 seconds.
> Adjust your expectations accordingly!


> Iteration:   1 / 1000 [  0%]  (Warmup)
> Iteration: 100 / 1000 [ 10%]  (Warmup)
> Iteration: 200 / 1000 [ 20%]  (Warmup)
> Iteration: 300 / 1000 [ 30%]  (Warmup)
> Iteration: 400 / 1000 [ 40%]  (Warmup)
> Iteration: 500 / 1000 [ 50%]  (Warmup)
> Iteration: 501 / 1000 [ 50%]  (Sampling)
> Iteration: 600 / 1000 [ 60%]  (Sampling)
> Iteration: 700 / 1000 [ 70%]  (Sampling)
> Iteration: 800 / 1000 [ 80%]  (Sampling)
> Iteration: 900 / 1000 [ 90%]  (Sampling)
> Iteration: 1000 / 1000 [100%]  (Sampling)

>  Elapsed Time: 0.025148 seconds (Warm-up)
>     >        0.019178 seconds (Sampling)
>     >        0.044326 seconds (Total)


> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.

> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 4).

> Gradient evaluation took 2.8e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.28 seconds.
> Adjust your expectations accordingly!


> Iteration:   1 / 1000 [  0%]  (Warmup)
> Iteration: 100 / 1000 [ 10%]  (Warmup)
> Iteration: 200 / 1000 [ 20%]  (Warmup)
> Iteration: 300 / 1000 [ 30%]  (Warmup)
> Iteration: 400 / 1000 [ 40%]  (Warmup)
> Iteration: 500 / 1000 [ 50%]  (Warmup)
> Iteration: 501 / 1000 [ 50%]  (Sampling)
> Iteration: 600 / 1000 [ 60%]  (Sampling)
> Iteration: 700 / 1000 [ 70%]  (Sampling)
> Iteration: 800 / 1000 [ 80%]  (Sampling)
> Iteration: 900 / 1000 [ 90%]  (Sampling)
> Iteration: 1000 / 1000 [100%]  (Sampling)

>  Elapsed Time: 0.033636 seconds (Warm-up)
>     >        0.018256 seconds (Sampling)
>     >        0.051892 seconds (Total)

> Error in FUN(X[[i]], ...) : 
>   trying to get slot "mode" from an object of a basic class ("NULL") with no slots

Adding the shinystan package ‘fixes’ my problem:

library('shinystan')

fit <- sampling(tstmod, data = schools_dat, iter = 1000, chains = 4)

> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 1).

> Gradient evaluation took 1.4e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.14 seconds.
> Adjust your expectations accordingly!


> Iteration:   1 / 1000 [  0%]  (Warmup)
> Iteration: 100 / 1000 [ 10%]  (Warmup)
> Iteration: 200 / 1000 [ 20%]  (Warmup)
> Iteration: 300 / 1000 [ 30%]  (Warmup)
> Iteration: 400 / 1000 [ 40%]  (Warmup)
> Iteration: 500 / 1000 [ 50%]  (Warmup)
> Iteration: 501 / 1000 [ 50%]  (Sampling)
> Iteration: 600 / 1000 [ 60%]  (Sampling)
> Iteration: 700 / 1000 [ 70%]  (Sampling)
> Iteration: 800 / 1000 [ 80%]  (Sampling)
> Iteration: 900 / 1000 [ 90%]  (Sampling)
> Iteration: 1000 / 1000 [100%]  (Sampling)

>  Elapsed Time: 0.01924 seconds (Warm-up)
>                0.012036 seconds (Sampling)
>                0.031276 seconds (Total)


> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 2).

> Gradient evaluation took 1.3e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.13 seconds.
> Adjust your expectations accordingly!


> Iteration:   1 / 1000 [  0%]  (Warmup)
> Iteration: 100 / 1000 [ 10%]  (Warmup)
> Iteration: 200 / 1000 [ 20%]  (Warmup)
> Iteration: 300 / 1000 [ 30%]  (Warmup)
> Iteration: 400 / 1000 [ 40%]  (Warmup)
> Iteration: 500 / 1000 [ 50%]  (Warmup)
> Iteration: 501 / 1000 [ 50%]  (Sampling)
> Iteration: 600 / 1000 [ 60%]  (Sampling)
> Iteration: 700 / 1000 [ 70%]  (Sampling)
> Iteration: 800 / 1000 [ 80%]  (Sampling)
> Iteration: 900 / 1000 [ 90%]  (Sampling)
> Iteration: 1000 / 1000 [100%]  (Sampling)

>  Elapsed Time: 0.023172 seconds (Warm-up)
>                0.01713 seconds (Sampling)
>                0.040302 seconds (Total)


> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 3).

> Gradient evaluation took 1.4e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.14 seconds.
> Adjust your expectations accordingly!


> Iteration:   1 / 1000 [  0%]  (Warmup)
> Iteration: 100 / 1000 [ 10%]  (Warmup)
> Iteration: 200 / 1000 [ 20%]  (Warmup)
> Iteration: 300 / 1000 [ 30%]  (Warmup)
> Iteration: 400 / 1000 [ 40%]  (Warmup)
> Iteration: 500 / 1000 [ 50%]  (Warmup)
> Iteration: 501 / 1000 [ 50%]  (Sampling)
> Iteration: 600 / 1000 [ 60%]  (Sampling)
> Iteration: 700 / 1000 [ 70%]  (Sampling)
> Iteration: 800 / 1000 [ 80%]  (Sampling)
> Iteration: 900 / 1000 [ 90%]  (Sampling)
> Iteration: 1000 / 1000 [100%]  (Sampling)

>  Elapsed Time: 0.023496 seconds (Warm-up)
>                0.015783 seconds (Sampling)
>                0.039279 seconds (Total)


> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 4).

> Gradient evaluation took 4.1e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.41 seconds.
> Adjust your expectations accordingly!


> Iteration:   1 / 1000 [  0%]  (Warmup)
> Iteration: 100 / 1000 [ 10%]  (Warmup)
> Iteration: 200 / 1000 [ 20%]  (Warmup)
> Iteration: 300 / 1000 [ 30%]  (Warmup)
> Iteration: 400 / 1000 [ 40%]  (Warmup)
> Iteration: 500 / 1000 [ 50%]  (Warmup)
> Iteration: 501 / 1000 [ 50%]  (Sampling)
> Iteration: 600 / 1000 [ 60%]  (Sampling)
> Iteration: 700 / 1000 [ 70%]  (Sampling)
> Iteration: 800 / 1000 [ 80%]  (Sampling)
> Iteration: 900 / 1000 [ 90%]  (Sampling)
> Iteration: 1000 / 1000 [100%]  (Sampling)

>  Elapsed Time: 0.034431 seconds (Warm-up)
>                0.014595 seconds (Sampling)
>                0.049026 seconds (Total)

I can call fit now, no problem

> print(fit)
Inference for Stan model: 9c2009747cc2401f4a9818c1e89252e6.
4 chains, each with iter=1000; warmup=500; thin=1; 
post-warmup draws per chain=500, total post-warmup draws=2000.

           mean se_mean   sd   2.5%    25%    50%    75%  97.5% n_eff Rhat
mu         7.86    0.15 5.01  -2.03   4.62   7.69  11.02  18.42  1188    1
tau        6.63    0.17 5.11   0.31   2.73   5.59   9.29  19.63   907    1
eta[1]     0.41    0.02 0.91  -1.45  -0.20   0.46   1.03   2.11  2000    1
eta[2]     0.04    0.02 0.86  -1.59  -0.55   0.02   0.61   1.80  1885    1
eta[3]    -0.20    0.02 0.91  -1.89  -0.83  -0.20   0.44   1.56  2000    1
etc....

Thanks for sharing. Unfortunately, I’m not able to replicate the example.

Does it work if you only use a single core. Eg, leave out the “options(mc.cores = parallel::detectCores())” option.

yes, it does work if I leave out the “options(mc.cores = parallel::detectCores())”

I encountered this same issue today on a fresh install of ubuntu 18.04.1 and R 3.5.1. Loading shinystan also solved my problem, so I looked at its dependencies to figure out what it was doing. I managed to trace the solution back to loading the “later” package: if I load the later package, then I can run multiple cores. If not, multiple cores fail.

I installed all my packages as binaries from rrutter’s ppa, which is my best guess as to the cause of the issue (I usually install packages from source). I tried installing rstan and StanHeaders from source after that, but it didn’t help. Maybe I need to install some others from source.

I’m not looking for a solution here because I don’t think there is an obvious one, but maybe this info helps.

I have no issues running this model on Debian 9.5 Stretch with the same R and Stan setup as the original post.

I don’t expect many people to be able to replicate this, but there are at least two of us. Following the “load shinystan” solution beyond the later package (see my previous post), I found that the problem can be solved via a single function

later:::ensureInitialized()

which seems to do something related to registering threads and allocating memory. The underlying C++ code for later:::ensureInitialized() is around line 146 here:

Maybe somebody with better C++ skills than me has an idea of how that could influence rstan on multiple cores.

I now also get this error in Debian 10 with only rstan installed.

Reproducible bugs are best reported on the issue tracker for the relevant repository. For RStan, that’s

That will get the attention of all of the RStan devs, provide a way to track fixes, and most importantly, make sure we don’t forgt about it.

1 Like

Very interesting to see this come up again. If you run “gc()” before sampling, do you still get the error?

Apparently resolved through package updates to Debian 10. Presumably patches for rolling distros are forthcoming.

I’m not clear what rstan or R depends on that changed. Knowledgeable parties can review the changes here: https://lists.debian.org/debian-testing-changes/2018/09/threads.html