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


I am having quite a lot of trouble using brms to fit a very simple mixed effects model on my desktop Mac (OS version: 10.14.6, R version: 4.1.1, brms version: 2.16.1). The model includes 2 fixed effects parameters and 2 random intercepts.

Specifically, I keep receiving the following error message: “Error in unserialize(socklist[[n]]) : error reading from connection”. I have attached a photo of the error as it most recently appeared in RStudio.

Some points about when the error message does and does not appear:

First, kindly note that in the attached photo I have “cores = 4.” If I use the default core number (=1), then RStudio simply crashes and no error message appears (also, e.g., Error in unserialize(socklist[[n]]) : error reading from connection). So, I know I can only use multiple cores to reliably obtain the error message.

Second, the error seems to appear only when I include the “control = list()” argument of the “brm” function. That is, the error does not appear if I run brm using the default values of the control parameters for the sampler; in this case, the sampler runs normally, without error. So, I know I can run my model if I exclude the “control = list()” argument.

Third, when using the same desktop computer the same error appears when I run a different dataset and script that includes a “control = list()” command. So, I know the issue is not somehow unique to the dataset or script I am working with.

Fourth, when using my personal laptop the error does not appear when I run the identical script (Mac OS version: 10.15.7, R version: 4.0.5, brms version: 2.16.1). So, I know the error is somehow arising from the specific desktop computer I am working with.

Some prior attempts at resolving this issue include:

I have tried uninstalling and reinstalling R, RStudio, brms and the various packages associated with it (e.g., rstan, rstanarm) and Xcode and deleted preexisting Makevars files (following Error in unserialize(socklist[[n]]) : error reading from connection Error in serialize(data, node$con, xdr = FALSE) : error writing to connection - #27 by bgoodri). In my most recent attempt at reinstalling rstan, I have followed the following instructions verbatim: RStan Getting Started · stan-dev/rstan Wiki · GitHub. However, none of these approaches has resolved the issue.

The best that I can discern is that there is some issue with converting the brms commands into C++ code. However, this may be wrong and, even if it is right, I have run out of ideas about how to proceed with resolving this issue.

Any help at all would be greatly appreciated, thank you.

1 Like

Can you share which version of rstan are you using? The version on CRAN is a bit outdated, so installing more recent version via Repository for distributing (some) stan-dev R packages | r-packages might help. Usually those errors are associated with not recompiling something after an update - so you could also check that you install all Stan-related packages from source and that you don’t have any compiled models stored (usually in .rda files on my Windows PC, not sure if the extension is the same on Mac)

You may also try using cmdstanr (Getting started with CmdStanR • cmdstanr) which tends to be easier to run and maintain (and doesn’t crash R sessions) - after specifying options(brms.backend = "cmdstanr") brms will compile models with cmdstanr instead of rstan

Hope you can get the problem resovled soon.

1 Like

This error has been popping up recently with brms and adapt_delta, and is still being looked into. For now I would definitely second @martinmodrak’s suggestion to use the cmdstanr backend with brms

On a side note, the max_treedepth = 50 setting is likely to be very excessive and result in very slow running times. If you receive max_treedepth warnings with the default setting (10), then raising the max to 15 usually addresses these

1 Like

Thank you! I followed the package install code for rstan in the first link you suggested, and that solved the issue. I was initially using the version of rstan currently available on CRAN, i.e., 2.21.2; this version was associated with the error message above. The version I just installed (2.26.3) runs without error.

1 Like