Segfault when using brms + cmdstanr + compile_model_methods = TRUE

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:

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

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)

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

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:

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