Error in (un)serialize rstan for Windows

I am experiencing a fairly odd error. I am having a hard time finding references to it that actually capture what I’m observing here. When I fit my model, I get the following output/error:

>testfit <- stan(file=“testmodel.stan”, data=dat, iter=100, warmup=50, chains=3)
recompiling to avoid crashing R session
In file included from C:/Users/dan/Documents/R/win-library/3.4/BH/include/boost/config.hpp:39:0,
from C:/Users/dan/Documents/R/win-library/3.4/BH/include/boost/math/tools/config.hpp:13,
from C:/Users/dan/Documents/R/win-library/3.4/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from C:/Users/dan/Documents/R/win-library/3.4/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from C:/Users/dan/Documents/R/win-library/3.4/StanHeaders/include/stan/math/rev/core.hpp:12,
from C:/Users/dan/Documents/R/win-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:4,
from C:/Users/dan/Documents/R/win-library/3.4/StanHeaders/include/stan/math.hpp:4,
from C:/Users/dan/Documents/R/win-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file163072da6f1d.cpp:8:
C:/Users/dan/Documents/R/win-library/3.4/BH/include/boost/config/compiler/gcc.hpp:186:0: warning: “BOOST_NO_CXX11_RVALUE_REFERENCES” redefined
:0:0: note: this is the location of the previous definition
Error in unserialize(socklist[[n]]) : error reading from connection
Error in serialize(data, node$con, xdr = FALSE) :
error writing to connection

If I use chains=1, R just crashes and RStudio restarts. So that’s odd. Well… let’s go to the code of “testmodel.stan” so you can see what I did wrong that is causing this problem:


real<lower=0> sigma_sev;
real freq_params[3];
real beta_n_sites;
real beta_closed;
real beta_no_spread;
real beta_discovered;
real beta_time_open;
real beta_depth;
real beta_zero_depth;
real beta_interc;


That’s the whole program. I had a much larger model file, but after I kept getting this error I decided I’d create a test model and keep deleting elements until the error went away. And here we are!

If I remove the “<lower=0>” specification from the sigma_sev variable, it compiles and runs. If I remove any single parameter, it compiles and runs (apparently even with 1500 iterations and 500 warmup steps, transformed data, vectors storing intermediate values in the model block, and large vector in the “generated quantities” block!) But a single additional parameter and the whole thing stops working.

I’m kind of at my wit’s end on this. My computer has 8GB of RAM. I’ve been watching resource monitor, and it my computer doesn’t seem to ever go above about 50% RAM usage. If I change the number of chains to 1, it compiles and then crashes, forcing Rstudio to restart.

Before anyone asks, I am using


The data I’m passing R is a list with about 12 columns and 4,238 rows. Not enormous. But even if I reduce the size of that file to 100 rows, it gives me the same error. Unless I make one of the changes I mention above (i.e., removing <lower=0> or removing a parameter, such as reducing the size of the array from 3 to 2.)

Operating System: Windows 10

Output of writeLines(readLines(file.path(Sys.getenv(“HOME”), “.R/Makevars”))):

CXXFLAGS=-O3 -Wno-unused-variable -Wno-unused-function -Wno-unused-local-typedefs

CXXFLAGS=-O3 -mtune=native -Wno-unused-variable -Wno-unused-function

CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function

Output of sessioninfo:

R version 3.4.2 Patched (2017-10-12 r73554)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

attached base packages:

other attached packages:
[1] rstan_2.16.2

loaded via a namespace (and not attached):
[1] Rcpp_0.12.13 codetools_0.2-15 matrixStats_0.52.2 mvtnorm_1.0-6 lattice_0.20-35 grDevices_3.4.2
[7] MASS_7.3-47 grid_3.4.2 plyr_1.8.4 rethinking_1.59 gtable_0.2.0 stats4_3.4.2
[13] coda_0.19-1 datasets_3.4.2 StanHeaders_2.16.0-1 scales_0.5.0 ggplot2_2.2.1 rlang_0.1.4
[19] utils_3.4.2 lazyeval_0.2.1 rstudioapi_0.7 graphics_3.4.2 base_3.4.2 tools_3.4.2
[25] loo_1.1.0 munsell_0.4.3 parallel_3.4.2 compiler_3.4.2 stats_3.4.2 inline_0.3.14
[31] colorspace_1.3-2 methods_3.4.2 gridExtra_2.3 tibble_1.3.4

Get rid of -mtune=native -march=native (on Windows only). And recompile the model.

1 Like

Boy, is my face red. I actually tried that before, didn’t have any luck, and reverted it to include those flags thinking I had done something wrong.

I must have been making a different mistake at the time, because now my model (both the test model and the full model I referred to earlier) appears to also be compiling properly with that change. Thank you for the very prompt reply!

Well, I am getting the same error
Error in unserialize(socklist[[n]]) : error reading from connection
Error in serialize(data, node$con, xdr = FALSE) :
error writing to connection
on Win 10. Strangely enough model was running before then I added few predictors and model stopped running. After I removed those predictors model started to run again. I have experienced this program before but then I switched to cmdstan. In this particular model those predictors are not statistically significant (credible interval includes 0) which may have caused instability I guess, I am using rstan
Version: 2.17.3
Date: 2018-01-15.
The model that is running is attached. The predictors which cause problems are commented.
ablation.stan (1.4 KB)

Thanks for any insight,

Fixed itself after removing native tune & arch.


1 Like

I am sorry but how exactly i should remove them?
Problably it appears that i have the same error…