JuliaInterop / JuliaCall

Embed Julia in R
https://non-contradiction.github.io/JuliaCall/index.html
Other
267 stars 36 forks source link

Segfault in atexit hook on Julia 1.9 #196

Open JackDunnNZ opened 1 year ago

JackDunnNZ commented 1 year ago

I am seeing the following segfault when running the following example on Julia 1.9:

 $ julia -e 'using RCall; R"JuliaCall::julia_setup()"'
┌ Warning: RCall.jl: Julia version 1.9.0 at location /Users/jack/.julia/juliaup/julia-1.9.0+0.x64.apple.darwin14/bin will be used.
│ Loading setup script for JuliaCall...
└ @ RCall ~/.julia/packages/RCall/LWzAQ/src/io.jl:172
┌ Warning: RCall.jl: Finish loading setup script for JuliaCall.
└ @ RCall ~/.julia/packages/RCall/LWzAQ/src/io.jl:172

[48844] signal (11.1): Segmentation fault: 11
in expression starting at none:0
ijl_eh_restore_state at /Users/jack/.julia/juliaup/julia-1.9.0+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
_atexit at ./initdefs.jl:386
jfptr__atexit_44795.clone_1 at /Users/jack/.julia/juliaup/julia-1.9.0+0.x64.apple.darwin14/lib/julia/sys.dylib (unknown line)
ijl_apply_generic at /Users/jack/.julia/juliaup/julia-1.9.0+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
ijl_atexit_hook at /Users/jack/.julia/juliaup/julia-1.9.0+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
jl_repl_entrypoint at /Users/jack/.julia/juliaup/julia-1.9.0+0.x64.apple.darwin14/lib/julia/libjulia-internal.1.9.dylib (unknown line)
Allocations: 6538113 (Pool: 6531359; Big: 6754); GC: 10
[1]    48843 segmentation fault  julia --depwarn=yes -e

If I run on Julia 1.8 or earlier, it works fine.

Based on the atexit in the stacktrace, I tried disabling the atexit hook in JuliaCall here and that made the segfault go away, so presumably there is something in that function that Julia 1.9 does not like. I haven't been able to go any deeper though

Session Info ```r ─ Session info ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── setting value version R version 4.3.0 (2023-04-21) os macOS Ventura 13.1 system x86_64, darwin20 ui X11 language (EN) collate en_NZ.UTF-8 ctype en_NZ.UTF-8 tz America/New_York date 2023-05-12 pandoc 2.11.3.2 @ /usr/local/bin/pandoc ─ Packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── package * version date (UTC) lib source cachem 1.0.8 2023-05-01 [1] CRAN (R 4.3.0) callr 3.7.3 2022-11-02 [1] CRAN (R 4.3.0) cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.0) crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.0) devtools 2.4.5 2022-10-11 [1] CRAN (R 4.3.0) digest 0.6.31 2022-12-11 [1] CRAN (R 4.3.0) ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.3.0) fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) fs 1.6.2 2023-04-25 [1] CRAN (R 4.3.0) glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.0) htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.3.0) htmlwidgets 1.6.2 2023-03-17 [1] CRAN (R 4.3.0) httpuv 1.6.11 2023-05-11 [1] CRAN (R 4.3.0) later 1.3.1 2023-05-02 [1] CRAN (R 4.3.0) lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.3.0) magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) memoise 2.0.1 2021-11-26 [1] CRAN (R 4.3.0) mime 0.12 2021-09-28 [1] CRAN (R 4.3.0) miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.3.0) pkgbuild 1.4.0 2022-11-27 [1] CRAN (R 4.3.0) pkgload 1.3.2 2022-11-16 [1] CRAN (R 4.3.0) prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.3.0) processx 3.8.1 2023-04-18 [1] CRAN (R 4.3.0) profvis 0.3.8 2023-05-02 [1] CRAN (R 4.3.0) promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.3.0) ps 1.7.5 2023-04-18 [1] CRAN (R 4.3.0) purrr 1.0.1 2023-01-10 [1] CRAN (R 4.3.0) R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) Rcpp 1.0.10 2023-01-22 [1] CRAN (R 4.3.0) remotes 2.4.2 2021-11-30 [1] CRAN (R 4.3.0) rlang 1.1.1 2023-04-28 [1] CRAN (R 4.3.0) sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0) shiny 1.7.4 2022-12-15 [1] CRAN (R 4.3.0) stringi 1.7.12 2023-01-11 [1] CRAN (R 4.3.0) stringr 1.5.0 2022-12-02 [1] CRAN (R 4.3.0) urlchecker 1.0.1 2021-11-30 [1] CRAN (R 4.3.0) usethis 2.1.6 2022-05-25 [1] CRAN (R 4.3.0) vctrs 0.6.2 2023-04-19 [1] CRAN (R 4.3.0) xtable 1.8-4 2019-04-21 [1] CRAN (R 4.3.0) [1] /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ```
JackDunnNZ commented 1 year ago

I looked a bit more into it, and it seems like the real issue is that JuliaCall should not be calling jl_atexit_hook in the case where the R session is already embedded in Julia, as Julia should be the one running its own exit hooks in that case. I'm not sure it it's possible to detect this during the initialization and if so, avoid registering the finalizer.

As a "workaround" it is possible to avoid the segfault by running empty!(Base.atexit_hooks) before the top-level Julia process exits - this is obviously a bad idea in general for many reasons, but at least can avoid the segfault if necessary