# Segfault when using brms + cmdstanr + compile\_model\_methods = TRUE

**URL:** <https://discourse.mc-stan.org/t/segfault-when-using-brms-cmdstanr-compile-model-methods-true/33771>\
**Category:** brms\
**Tags:** compiler, cmdstanr\
**Created:** [January 9, 2024, 2:33pm UTC](https://discourse.mc-stan.org/t/segfault-when-using-brms-cmdstanr-compile-model-methods-true/33771 "2024-01-09T14:33:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guido\_Biele](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/guido_biele/32/3414_2.png) [@Guido\_Biele](https://discourse.mc-stan.org/u/Guido_Biele)\
**Post date:** [January 9, 2024, 2:33pm UTC](https://discourse.mc-stan.org/t/segfault-when-using-brms-cmdstanr-compile-model-methods-true/33771/1 "2024-01-09T14:33:24Z")

</div>

I am trying to make `model_methods` in cmdstanr available. This works on Windows, but if I try it on a Linux server and first load `brms` (2.20.4) and then `cmdstanr` i get a segfault (see below for the full error message).

In particular, this code can be exectuted:

```no-highlight
library(cmdstanr)
fn_exe = paste0(cmdstan_path(),"/examples/bernoulli/bernoulli")
file.remove(paste0(fn_exe))
stan_file = paste0(fn_exe,".stan")
mod = cmdstan_model(stan_file, compile_model_methods = TRUE)

```

But when I first load `brms` as in

```no-highlight
library(brms)
library(cmdstanr)
...

```

I get the error message below.

When I first load only compile the model and then load brms, I get a similar error (see bottom of this post).

I am glad for any hint for a solution.

**Error message**

> \*\*\* caught segfault \*\*\*  
> address 0x7f2462b41008, cause ‘invalid permissions’
> 
> Traceback:  
> 1: dyn.load(“/tmp/RtmpO7dtxn/sourceCpp-x86\_64-pc-linux-gnu-1.0.8.3/sourcecpp\_a9ee2291f98d/sourceCpp\_2.so”)  
> 2: eval(ei, envir)  
> 3: eval(ei, envir)  
> 4: withVisible(eval(ei, envir))  
> 5: source(scriptPath, local = env)  
> 6: Rcpp::sourceCpp(code = code, env = env, verbose = verbose)  
> 7: force(code)  
> 8: force(code)  
> 9: with\_envvar(c(R\_MAKEVARS\_USER = makevars\_file), { set\_makevars(new, path, makevars\_file, assignment = assignment) force(code)})  
> 10: withr::with\_makevars(c(USE\_CXX14 = 1, PKG\_CPPFLAGS = ifelse(cmdstan\_version() \<= “2.30.1”, “-DCMDSTAN\_JSON”, “”), PKG\_CXXFLAGS = paste0(cxxflags, cmdstanr\_includes, collapse = " "), PKG\_LIBS = libs), Rcpp::sourceCpp(code = code, env = env, verbose = verbose))  
> 11: force(code)  
> 12: withr::with\_path(paste0(cmdstan\_path(), lib\_paths), withr::with\_makevars(c(USE\_CXX14 = 1, PKG\_CPPFLAGS = ifelse(cmdstan\_version() \<= “2.30.1”, “-DCMDSTAN\_JSON”, “”), PKG\_CXXFLAGS = paste0(cxxflags, cmdstanr\_includes, collapse = " "), PKG\_LIBS = libs), Rcpp::sourceCpp(code = code, env = env, verbose = verbose)))  
> 13: rcpp\_source\_stan(code, env, verbose)  
> 14: expose\_model\_methods(env = private$model\_methods\_env\_, verbose = !quiet, hessian = compile\_hessian\_method)  
> 15: self$compile(…)  
> 16: initialize(…)  
> 17: CmdStanModel$new(stan\_file = stan\_file, exe\_file = exe\_file, compile = compile, …)  
> 18: cmdstan\_model(stan\_file, compile\_model\_methods = TRUE)

**Session-info:**

> R version 4.2.0 (2022-04-22)  
> Platform: x86\_64-pc-linux-gnu (64-bit)  
> Running under: Red Hat Enterprise Linux
> 
> Matrix products: default  
> BLAS/LAPACK: /cluster/software/EASYBUILD/FlexiBLAS/3.0.4-GCC-11.2.0/lib64/libflexiblas.so.3.0
> 
> locale:  
> [1] LC\_CTYPE=en\_US.UTF-8 LC\_NUMERIC=C  
> [3] LC\_TIME=en\_US.UTF-8 LC\_COLLATE=en\_US.UTF-8  
> [5] LC\_MONETARY=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8  
> [7] LC\_PAPER=en\_US.UTF-8 LC\_NAME=C  
> [9] LC\_ADDRESS=C LC\_TELEPHONE=C  
> [11] LC\_MEASUREMENT=en\_US.UTF-8 LC\_IDENTIFICATION=C
> 
> attached base packages:  
> [1] stats graphics grDevices utils datasets methods base
> 
> other attached packages:  
> [1] cmdstanr\_0.7.0 brms\_2.20.4 Rcpp\_1.0.8.3
> 
> loaded via a namespace (and not attached):  
> [1] Brobdingnag\_1.2-7 jsonlite\_1.8.8 QuickJSR\_1.0.9  
> [4] gtools\_3.9.2 RcppParallel\_5.1.5 StanHeaders\_2.26.28  
> [7] threejs\_0.3.3 shiny\_1.7.1 assertthat\_0.2.1  
> [10] posterior\_1.5.0 distributional\_0.3.2 stats4\_4.2.0  
> [13] tensorA\_0.36.2 pillar\_1.9.0 backports\_1.4.1  
> [16] lattice\_0.20-45 glue\_1.6.2 digest\_0.6.33  
> [19] promises\_1.2.0.1 checkmate\_2.3.1 colorspace\_2.1-0  
> [22] htmltools\_0.5.2 httpuv\_1.6.5 Matrix\_1.4-1  
> [25] plyr\_1.8.7 dygraphs\_1.1.1.6 pkgconfig\_2.0.3  
> [28] rstan\_2.32.3 purrr\_0.3.4 xtable\_1.8-4  
> [31] mvtnorm\_1.1-3 scales\_1.3.0 processx\_3.8.3  
> [34] later\_1.3.0 tibble\_3.2.1 bayesplot\_1.9.0  
> [37] generics\_0.1.3 farver\_2.1.1 ggplot2\_3.4.4  
> [40] ellipsis\_0.3.2 DT\_0.22 shinyjs\_2.1.0  
> [43] cli\_3.6.2 crayon\_1.5.1 magrittr\_2.0.3  
> [46] mime\_0.12 ps\_1.7.5 fansi\_1.0.6  
> [49] nlme\_3.1-157 xts\_0.12.1 pkgbuild\_1.3.1  
> [52] colourpicker\_1.1.1 prettyunits\_1.1.1 tools\_4.2.0  
> [55] loo\_2.5.1 lifecycle\_1.0.4 matrixStats\_1.2.0  
> [58] stringr\_1.4.0 V8\_4.1.0 munsell\_0.5.0  
> [61] callr\_3.7.0 compiler\_4.2.0 rlang\_1.1.2  
> [64] grid\_4.2.0 ggridges\_0.5.3 htmlwidgets\_1.5.4  
> [67] crosstalk\_1.2.0 igraph\_1.3.1 miniUI\_0.1.1.1  
> [70] base64enc\_0.1-3 codetools\_0.2-18 gtable\_0.3.4  
> [73] curl\_4.3.2 inline\_0.3.19 abind\_1.4-5  
> [76] DBI\_1.1.2 markdown\_1.1 reshape2\_1.4.4  
> [79] R6\_2.5.1 gridExtra\_2.3 rstantools\_2.2.0  
> [82] zoo\_1.8-10 knitr\_1.38 bridgesampling\_1.1-2  
> [85] dplyr\_1.0.8 fastmap\_1.1.0 utf8\_1.2.4  
> [88] shinystan\_2.6.0 shinythemes\_1.2.0 stringi\_1.7.6  
> [91] parallel\_4.2.0 vctrs\_0.6.5 xfun\_0.30  
> [94] tidyselect\_1.1.2 coda\_0.19-4

**Error when first compiling the model and then loading brms:**

> \*\*\* caught segfault \*\*\*  
> address 0x9710a, cause ‘memory not mapped’
> 
> Traceback:  
> 1: dyn.load(file, DLLpath = DLLpath, …)  
> 2: library.dynam(lib, package, package.lib)  
> 3: loadNamespace(name)  
> 4: doTryCatch(return(expr), name, parentenv, handler)  
> 5: tryCatchOne(expr, names, parentenv, handlers[[1L]])  
> 6: tryCatchList(expr, classes, parentenv, handlers)  
> 7: tryCatch(loadNamespace(name), error = function(e) { tr ← Sys.getenv(“_R\_NO\_REPORT\_MISSING\_NAMESPACES_”) if (tr == “false” || (where != “” && !nzchar(tr))) { warning(gettextf(“namespace %s is not available and has been replaced\nby .GlobalEnv when processing object %s”, sQuote(name)[1L], sQuote(where)), domain = NA, call. = FALSE, immediate. = TRUE) if (nzchar(Sys.getenv(“_R\_CALLS\_MISSING\_NAMESPACES_”))) print(sys.calls()) } .GlobalEnv})  
> 8: …getNamespace(c(“rstan”, “2.21.5”), “”)  
> 9: get(classMetaName(cl), where)  
> 10: methods::cacheMetaData(ns, TRUE, ns)  
> 11: loadNamespace(j ← i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]])  
> 12: asNamespace(ns)  
> 13: namespaceImportFrom(ns, loadNamespace(j ← i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]), i[[2L]], from = package)  
> 14: loadNamespace(package, lib.loc)  
> 15: doTryCatch(return(expr), name, parentenv, handler)  
> 16: tryCatchOne(expr, names, parentenv, handlers[[1L]])  
> 17: tryCatchList(expr, classes, parentenv, handlers)  
> 18: tryCatch({ attr(package, “LibPath”) ← which.lib.loc ns ← loadNamespace(package, lib.loc) env ← attachNamespace(ns, pos = pos, deps, exclude, include.only)}, error = function(e) { P ← if (!is.null(cc ← conditionCall(e))) paste(" in", deparse(cc)[1L]) else “” msg ← gettextf(“package or namespace load failed for %s%s:\n %s”, sQuote(package), P, conditionMessage(e)) if (logical.return && !quietly) message(paste(“Error:”, msg), domain = NA) else stop(msg, call. = FALSE, domain = NA)})  
> 19: library(brms)

---

<div class="post-metadata">

**Author:** ![Guido\_Biele](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/guido_biele/32/3414_2.png) [@Guido\_Biele](https://discourse.mc-stan.org/u/Guido_Biele)\
**Post date:** [January 23, 2024, 2:09pm UTC](https://discourse.mc-stan.org/t/segfault-when-using-brms-cmdstanr-compile-model-methods-true/33771/3 "2024-01-23T14:09:50Z")

</div>

I just deleted a premature post saying the problem was solved with the newest cmdstan version. the problem is still there.

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [January 23, 2024, 4:22pm UTC](https://discourse.mc-stan.org/t/segfault-when-using-brms-cmdstanr-compile-model-methods-true/33771/4 "2024-01-23T16:22:44Z")

</div>

As far as I can reproduce it’s a linux-specific issue. I don’t know the exact cause yet, but a workaround appears to be compiling your cmdstan models with `STAN_THREADS=true`:

```r
cmdstan_make_local(cpp_options = list(STAN_THREADS = TRUE))
rebuild_cmdstan()

```
