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

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

Thanks, I tried both ways, both worked, thanks!

My problem solved by Guoqing_Wang’s approach too. Thanks!

I’m getting a very similar problem, though am on linux.
I’m fitting a joint model with rstanarm::stan_jm.
Generally, it works with 1 core and 1 chain, but otherwise I get the “unserialize” error listed above.
Both the stan and parallel example code given above work fine.
I can also implement other parallel procedures (like bootstrap) without a problem.
Have tried chaging the C++ flags in the .R/Makevars as suggested, but it doesn’t seem to help. Any other thoughts to diagnose the issue differently on ubuntu?

R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

This sounds more like the problem of trying to store too much. When you do 1 chain, what is dim(post$stanfit) where post is what is produced by stan_jm?

[1] 1000 1 9371

Hi, @bgoodri,

I seem to have the same issue as @itpetersen described - when I try to fit a model with brms it generates the error.

I ran

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

to produce output.txt (29.6 KB)

I don’t see any errors there, but maybe I’m missing something.

If I run with one chain, I also get the same crashing of the R session.

I’ve tried without -march and -mtune, changing native to corei7, things currently back to square one at:

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

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

Any further clues or help gratefully received.

I am not an expert but I hope I can help by sharing my findings about it.

I had same error last week and for me it was due to trying to store a very large stanfit as pointed out by Ben in the post above. It would be helpful if you could also share your platform details (you can run sessionInfo()) and the output of dim(post$stanfit)

Hi @sam_learner, thanks for the quick response - it does indeed just seem to be a memory issue in the end, so today I managed to get it to run by playing with the memory allocations in R. Thanks though.

1 Like

Hi, I’m a brms user on Mac (11.6 BigSur, MacBook Pro 2019), I don’t have much experience with Stan directly or with c++ compilers. Everything used to work fine on my machine, but now I updated to R 4.1.1, and when I now run brm, I get the error: Error in unserialize(socklist[[n]]) : error reading from connection (this is when I set ‘cores = 4’). When I set ‘cores = 1’, then R crashes upon calling brm().

To deal with the problem, I have re-installed my mac os c++ toolchain using this description:
https://thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/
The installer suggested here:

does not work on my machine, it crashes.

Unfortunately, I can’t read/understand the code in Makevars. Currently, I have this deleted (I tried different versions described above without success).

When I execute
example(stan_model, package = “rstan”, run.dontrun = TRUE)
everything seems fine and I don’t see any errors
stan_example_code_output.txt (8.7 KB)
.
However, running brm doesn’t work. The solutions suggested above did not work for me.

Any help is much appreciated, I really need brms for my daily work.

sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.6

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] brms_2.16.3 Rcpp_1.0.7 designr_0.1.12

=======================================
R-Code that generates the errors on my machine:

library(designr)
library(brms)

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

no error

Create latin-square design

design ←
fixed.factor(“X”, levels=c(“X1”, “X2”)) +
random.factor(“subj”, instances=21) +
random.factor(“item”, instances= 8) +
random.factor(c(“subj”, “item”), groups=c(“X”))
dat ← design.codes(design)
(contrasts(dat$X) ← c(+1, -1))
dat$so ← model.matrix(~X,dat)[,2]

simulate data

dat$ysim ← exp(simLMM(form = ~ 1 + so + (1 + so | subj) + (1 + so | item),
data = dat,
Fixef = c(6, 0.12),
VC_sd = list(sj=c(0.32, 0.22), it=c(0.04, 0.09), resid=0.31),
CP = 0.6,
empirical = TRUE, verbose=TRUE))

priors ← c( set_prior(“normal(6, 0.6)”, class = “Intercept”),
set_prior(“normal(0.12, 0.04)”, class = “b”, coef = “so”),
set_prior(“normal(0, 0.1)”, class = “sd”),
set_prior(“normal(0, 0.5)”, class = “sigma”),
set_prior(“lkj(2)”, class = “cor”) )

brm1 ← brm(ysim ~ so + (so|subj) + (so|item), dat,
family=lognormal(), prior=priors, cores=4,
save_pars = save_pars(all = TRUE),
warmup=2000, iter=10000,
control=list(adapt_delta=0.99, max_treedepth=15))

Ok, installing the development version of rstan solved the problem; see here:

1 Like

Thanks, this solved a similar issue for me with rstan trying to adjust the max_threedepth on my mac (BigSur) laptop!

I’m going to lock this thread since it’s getting a bit overlong. If anyone has a similar issue in the future, please feel free to open a new topic