radian icon indicating copy to clipboard operation
radian copied to clipboard

radian segfaults with renv projects on M3 Mac

Open zkamvar opened this issue 1 year ago • 7 comments

If I attempt to load a package with compiled code inside a renv project from radian, then I get a segmentation fault. I get no segmentation fault with the distributed version of R. I don't know if this is on the radian side or the https://github.com/r-lib/rig side. Any help is appreciated.

$ radian --version
radian version: 0.6.13
r executable: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/bin/R
r version: 4.4.1
python executable: /path/to/.pyenv/versions/3.10.14/bin/python3.10
python version: 3.10.14

EDIT, I decided to compare the outputs of renv::diagnostics() for both and I found that there's a different BLAS library used from radian, but that's also in normal radian operation, so I'm not sure what the difference is.

--- radian.log	2024-09-20 14:24:19
+++ base.log	2024-09-20 14:25:09
-BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
+BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 

Steps to reproduce

Create a directory with a single file that loads the rlang package and initialize the renv project (this works with both radian and regular R).

tmp=$(mktemp -d)
echo "library('rlang')" > "$tmp/test.R"
cd "$tmp"
R -e 'renv::init()'

After that, run radian and try to load rlang:

- Project '/private/var/folders/9p/m996p3_55hjf1hc62552cqfr0000gr/T/tmp.1jSSUFIP34' loaded. [renv 1.0.7]
R version 4.4.1 (2024-06-14) -- "Race for Your Life"
Platform: aarch64-apple-darwin20 (64-bit)
r$> library(rlang)

 *** caught bus error ***
address 0x10117e5e8, cause 'invalid alignment'

Traceback:
 1: dyn.load(file, DLLpath = DLLpath, ...)
 2: library.dynam(lib, package, package.lib)
 3: loadNamespace(package, lib.loc)
 4: doTryCatch(return(expr), name, parentenv, handler)
 5: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 6: tryCatchList(expr, classes, parentenv, handlers)
 7: 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)})
 8: library(rlang)

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 1
R is aborting now ...

In base R, it's fine:

R version 4.4.1 (2024-06-14) -- "Race for Your Life"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: aarch64-apple-darwin20

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

- Project '/private/var/folders/9p/m996p3_55hjf1hc62552cqfr0000gr/T/tmp.1jSSUFIP34' loaded. [renv 1.0.7]
> library('rlang')
> 
> 

zkamvar avatar Sep 20 '24 21:09 zkamvar

I cannot reproduce the crash on my macOS. Does it crash without renv? It could be possible that the version of python that you are using is not compatible with the R that you are using. For example, they may be linked to different C runtime.

randy3k avatar Sep 20 '24 23:09 randy3k

Regrading to the BLAS library thing (note: I have the same issue, it is not relevant to your crash). It seems that python on macOS has silently import the Accelerate.framework

# print the shared libraries loaded
# doesn't not work for system python /usr/bin/python3 which is protected by SIP
$ DYLD_PRINT_LIBRARIES=YES python -c '1' 2>&1 | grep BLAS
dyld[59072]: <BF96AF15-B1D2-3701-A1F6-E5EFA27A924D> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
dyld[59072]: <65B3F8D3-EA57-3C22-88A0-DE0FFF2ADB20> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libSparseBLAS.dylib
dyld[59072]: <BF96AF15-B1D2-3701-A1F6-E5EFA27A924D> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
dyld[59072]: <65B3F8D3-EA57-3C22-88A0-DE0FFF2ADB20> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libSparseBLAS.dylib

I am still trying to find a valid solution for it. Setting DYLD_INSERT_LIBRARIES only works for manually installed version of python on macOS and /usr/bin/python3 is protected by System Integrity Protection (SIP).

DYLD_INSERT_LIBRARIES=/Library/Frameworks/R.framework/Resources/lib/libRblas.dylib radian

randy3k avatar Sep 21 '24 00:09 randy3k

I think I'm running into the same problem. Radian segfaults when loading certain packages. So far I've noticed rlang, jsonlite, rmarkdown, and renv are causing it to segfault. Reinstalling the packages does not help.

adamaltmejd avatar Mar 05 '25 17:03 adamaltmejd

Realize it's unrelated to renv for me. Radian just crashes when loading certain packages.

adamaltmejd avatar Mar 07 '25 12:03 adamaltmejd

Same for me, igraph crashes it every time! Very strange how it's only some packages

agbocsardi avatar Mar 30 '25 07:03 agbocsardi

I can reproduce the issue with Renv on my x86 Mac.

warnes avatar May 29 '25 15:05 warnes

I am experiencing the same issue on an M3 Mac. The problem appears to only be with radian because my R runtime is functioning as normal when using the distributed version of R.

Realize it's unrelated to renv for me. Radian just crashes when loading certain packages.

For me, it happens for any package when using radian and renv, and it doesn't happen at all outside of renv project with radian or the regular R environment. If I can add any context that would help with debugging, let me know. renv also works properly in regular R.

ReedMerrill avatar Jun 30 '25 18:06 ReedMerrill