randy3k / radian

A 21 century R console
MIT License
2k stars 75 forks source link

reticulate segfaults with renv projects on M3 Mac #491

Open zkamvar opened 1 week ago

zkamvar commented 1 week ago

If I attempt to load a package with compiled code inside a renv project from reticulate, 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')
> 
> 
randy3k commented 1 week ago

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 commented 1 week ago

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