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

I’ve been getting the same error since R 3.5.3 on 64 bit windows 10.

same here:

R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
rstan version: 2.19.2

Had the same problem.

I noticed that I had set : Sys.setenv(LOCAL_CPPFLAGS = '-march=native')
When I commented this out, it all worked well.

For improved execution time, we recommend calling
Sys.setenv(LOCAL_CPPFLAGS = ‘-march=native’)

platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
major 3
minor 6.1
year 2019
month 07
day 05
svn rev 76782
language R
version.string R version 3.6.1 (2019-07-05)
nickname Action of the Toes


Ok my bad I didn’t re-check the installation process.
Just by entering the following command:

M <- file.path(Sys.getenv(“HOME”), “.R”, ifelse(.Platform$OS.type == “windows”, “”, “Makevars”))

I was able to remove march and mtune and now rstan works flawlessly

Can you post the content of your (now working) ‘’ file?

You can just delete any Makevars or files unless you are trying to install RStan from source.

here it is:


But I guess @bgoodri’s answer settles the issue for good.

As a side note, I just realised it took me so long to find the Makevars file because the .R hidden folder was not indexed, and I simply thought there was no file at all.

I can confirm that

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

goes away when I delete my file. My CPU is:

system(‘wmic cpu get caption, deviceid, name, numberofcores’)
Caption DeviceID Name NumberOfCores
Intel64 Family 6 Model 158 Stepping 10 CPU0 Intel® Core™ i7-8700 CPU @ 3.20GHz 6

However, with the following file (replacing ‘native’ by ‘corei7’, the error also goes away and I can install rstan from source.

CXX14=$(BINPREF)g++ -O2 -march=corei7 -mtune=corei7

CXX14FLAGS=-O3 -march=corei7
CXX14=(BINPREF)g++ -m(WIN) -std=c++1y
CXX11FLAGS=-O3 -march=corei7

My solution is to run stanc(), stan_model() sampling() sequentially, rather than simply stan().


I was running into this issue, as well. I tried a few of the solutions highlighted in the thread, but ultimately Guoqing_Wang’s solution was what worked for me.

I’m running into the same issue with brms and haven’t been able to solve it using the techniques above.

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (Version 1607, Build 14393.2906)
rstan version: 2.19.2

Please do

example(stan_model, package = "rstan", run.dontrun = TRUE)

and tell us the line that has error: (with the colon) in it.

Unless I’m missing it, I’m not seeing an error when I run that code. I’m relatively new to stan/brms. See attached for my output, in case it’s helpful.output.txt (21.3 KB)

Thanks very much for your help.

There was no error in that example, which is good. Can you run your brms thing with only 1 chain?

R crashed when I only ran it with one chain (see attached image).

Do you have -march=native and / or -mtune=native in your ~/.R/Makevars{.win} file? Or are you setting those in LOCAL_CPPFLAGS?

Hi, @bgoodri

I got the same error as @itpetersen did when trying to use brms.
I was trying to reproducible the analysis on this project:, which used brm for twice. When I run this rmd file, the first brm function works well, but when it comes to the second, it has this error.

I tested the example code, it worked, without error.
I tried to change the chain to 1, R just stopped without any error message.
Below is the information in my ~/.R/

CXX14=$(BINPREF)g++ -O2 -march=native -mtune=native
CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

This particular model runs fine with one chain when I remove the file and don’t set LOCAL_CPPFLAGS to -march=native. I thought I was also getting an error on another model when I didn’t set those, but I can’t confirm. So, the problem might be related to the -march=native and / or -mtune=native, as others above have suggested. Is there any workaround (or anticipated one) for this? Those particular settings result in considerably faster estimation, so I’d prefer to use them, if possible. But, more important is that it isn’t getting an error, so that’s a good start. Thanks for your help!

I would try it without the -march and -mtune stuff. Or change native to corei7.

1 Like

You might try -mtune=corei7 -march=corei7 instead.