Error in unserialize(socklist[[n]]) : error reading from connection

Hi all,

I have updated R, Rstudio, and rstan to the latest version, following the guideline here.

After installing, for the first time of running, rstan asks for C++
`C++14 standard requested but CXX14 is not defined Stan’

I did a google search and change the Makevars file from

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native


CXX14 = C:/RBuildTools/3.5/mingw_64/bin/g++
CXX14FLAGS = -O3 -Wno-unused-variable -Wno-unused-function

After this changing, Stan runs properly for several examples (I make to check). The first example is very simple: I generate Y from N(mu=0, sigma=1) and estimate these parameters.

However, when I run for a bigger model, involving multi_normal and matrix_exp, Stan has an error

Error in unserialize(socklist[[n]]) : error reading from connection
Error in serialize(data, node$con, xdr = FALSE) :
error writing to connection

Note that this model I can run before with the previous versions of R, Rstudio, and rstan.

Could you give me a help?

P/S: I upload the Rcode and Stan program here for the simple model. I cannot send the Stan program for the one that Stan has error since we are still working on that before publishing.

Trung Dung.model.stan (179 Bytes)
Rcodes.R (1.2 KB)

Try it with only one chain to see the real error message.

@bgoodri: Thank you for your reply!

However, after changing to 1 chain, R session crashed as it says

“R Session Aborted
R encountered a fatal error.
The session was terminated”.

OK. The matrix exponential can be a bit problematic. If you do tempdir() before calling stan with 1 chain, you should be able to look for .txt files in that directory after it crashes, and one of them might contain more information about the crash.

@bgoodri: I see one .txt file as I have uploaded.

I also put it here

In file included from C:/Users/u0105873/Documents/R/win-library/3.6/BH/include/boost/random/detail/integer_log2.hpp:19:0,
from C:/Users/u0105873/Documents/R/win-library/3.6/BH/include/boost/random/detail/int_float_pair.hpp:26,
from C:/Users/u0105873/Documents/R/win-library/3.6/BH/include/boost/random/exponential_distribution.hpp:27,
from C:/Users/u0105873/Documents/R/win-library/3.6/BH/include/boost/random/gamma_distribution.hpp:25,
from C:/Users/u0105873/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_rng.hpp:5,
from C:/Users/u0105873/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:276,
from C:/Users/u0105873/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/u0105873/Documents/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/u0105873/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file1c8865804d76.cpp:8:
C:/Users/u0105873/Documents/R/win-library/3.6/BH/include/boost/pending/integer_log2.hpp:7:89: note: #pragma message: This header is deprecated. Use <boost/integer/integer_log2.hpp> instead.

Execution halted

Maybe something is wrong here but I have no idea.

file1c8865804d76.cpp.err.txt (1.5 KB)

OK, that turned out to not have anything useful. Can you try it again replacing matrix_exp with a few terms of the Taylor series expansion of the matrix exponential function just to see if we can narrow down where the error is coming from?

@bgoodri: I have tried two options:

  1. Replace matrix_exp(M) by I+M (the first and the second term), and
  2. Remove all lines with matrix_exp (the model does not have statistical meaning but it is still syntactically correct).

However, in both cases, R crashes and gives the error as I provided above.

OK. Can you come up with any model where the difference between it crashing and not crashing can be explained by the presence or absence or content of a single line?

@bgoodri: I am doing as you suggest and I will report here to you when I reach that point.

I have noticed one thing. I am wondering whether this problem is associated with memory or CPU.

I open both Rstudio and Task Manager and see that when memory is about 75%, R crashes.

Please see the picture in the attachment.

Also possible that your RAM card is going bad. Or it could be it gets to 75% and the request for another 30% crashes it but is never reflected in the total.

@bgoodri: I have tried with a smaller dataset and have some parameters removed. R now runs properly with one and also three chains.

I also check Taks Manager and it is always less than 70% of memory. So maybe RAM is the problem at this moment.

Just a final question: The thing that I changed in Makevars file is just a connection between R and c++. Is it correct?

If I do not use Makevars as mentioned in the guideline and I have to change it into

Is it correct? (please just a confirmation) (I am afraid if it is not correct, it may change Stan algorithm or something like that and may provide wrong samples).

All that Makevars stuff is unlikely to be related to the crash (which I also suspect is due to RAM). If it can compile, then it is usually fine. The only thing on Windows is whether to compile with -mtune=native -march=native, which causes errors or crashes on some processors but not others.

@bgoodri: I have not tried with the full model containing matrix_exp but for others I do not have any problem at this moment.

Thank you for your help! If you do not see any update here, it means that later I can run with matrix exponential.


install.packages("rstan", repos = c("", getOption("repos")))

I had an old version of rstan (version 2.21.2 from CRAN), and it was showing the error. A new version, rstan version 2.26.3 (Stan version 2.26.1), doesn’t show the error.