# Segfault when jl\_init in R (also python?) with julia 1.6 on macOS

**URL:** <https://discourse.julialang.org/t/segfault-when-jl-init-in-r-also-python-with-julia-1-6-on-macos/58110>\
**Category:** Tooling\
**Tags:** question, embedding\
**Created:** [March 28, 2021, 5:24am UTC](https://discourse.julialang.org/t/segfault-when-jl-init-in-r-also-python-with-julia-1-6-on-macos/58110 "2021-03-28T05:24:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)\
**Post date:** [March 28, 2021, 5:24am UTC](https://discourse.julialang.org/t/segfault-when-jl-init-in-r-also-python-with-julia-1-6-on-macos/58110/1 "2021-03-28T05:24:13Z")

</div>

Hi, I’m debugging an issue for JuliaCall segfault with julia 1.6 on macOS, which embeds julia in R: [https://github.com/Non-Contradiction/JuliaCall/issues/164](https://github.com/Non-Contradiction/JuliaCall/issues/164)  
The issue can be simplified to the following code in R:

```julia
dyn.load("/Applications/Julia-1.6.app/Contents/Resources/julia/lib/libjulia.1.6.dylib")
.C("jl_init__threading")

```

which causes the following segfault in plain R IDE (RStudio just crashes and does not give any message):

```julia
 ***caught segfault***
address 0xfffffffffffffff8, cause 'memory not mapped'

Traceback:
 1: .C("jl_init__threading")

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

```

The same code runs fine with julia 1.5

```julia
dyn.load("/Applications/Julia-1.5.app/Contents/Resources/julia/lib/libjulia.1.5.dylib")
.C("jl_init__threading")

```

doesn’t give any error.

This segfault seems to happen only on macOS but not Windows and Linux. Any clue what causes the segfault? Thanks!

---

<div class="post-metadata">

**Author:** ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)\
**Post date:** [March 28, 2021, 2:16pm UTC](https://discourse.julialang.org/t/segfault-when-jl-init-in-r-also-python-with-julia-1-6-on-macos/58110/2 "2021-03-28T14:16:30Z")

</div>

I also checked PyJulia, and the segfault also happens with julia 1.6 but not 1.5:

```julia
MacBookdeMacBook-Pro:~ macbookpro$ python3
Python 3.8.2 (default, Dec 21 2020, 15:06:04) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import julia
>>> from julia import Julia
>>> Julia(runtime='/Applications/Julia-1.5.app/Contents/Resources/julia/bin/julia')
<julia.core.LegacyJulia object at 0x10324a100>
>>> exit()
MacBookdeMacBook-Pro:~ macbookpro$ python3
Python 3.8.2 (default, Dec 21 2020, 15:06:04) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import julia
>>> from julia import Julia
>>> Julia(runtime='/Applications/Julia-1.6.app/Contents/Resources/julia/bin/julia')
ERROR: Segmentation fault: 11

```

---

<div class="post-metadata">

**Author:** ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)\
**Post date:** [March 28, 2021, 2:58pm UTC](https://discourse.julialang.org/t/segfault-when-jl-init-in-r-also-python-with-julia-1-6-on-macos/58110/3 "2021-03-28T14:58:32Z")

</div>

The segfault error can also be replicated in python simply by

```julia
MacBookdeMacBook-Pro:~ macbookpro$ python3
Python 3.8.2 (default, Dec 21 2020, 15:06:04) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> libjulia = ctypes.PyDLL("/Applications/Julia-1.5.app/Contents/Resources/julia/lib/libjulia.1.5.dylib", ctypes.RTLD_GLOBAL)    
>>> libjulia.jl_init__threading()
775
>>> exit()
MacBookdeMacBook-Pro:~ macbookpro$ python3
Python 3.8.2 (default, Dec 21 2020, 15:06:04) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> libjulia = ctypes.PyDLL("/Applications/Julia-1.6.app/Contents/Resources/julia/lib/libjulia.1.6.dylib", ctypes.RTLD_GLOBAL)
>>> libjulia.jl_init__threading()
ERROR: Segmentation fault: 11
MacBookdeMacBook-Pro:~ macbookpro$ 

```

I will open an issue in julia repo since it seems to be some problem with `jl_init` in julia dll on mac instead of JuliaCall or PyJulia.  
Issue at [https://github.com/JuliaLang/julia/issues/40246](https://github.com/JuliaLang/julia/issues/40246)

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [March 28, 2021, 4:07pm UTC](https://discourse.julialang.org/t/segfault-when-jl-init-in-r-also-python-with-julia-1-6-on-macos/58110/4 "2021-03-28T16:07:22Z")

</div>

Hey @Non-Contradiction - I actually was using your excellent package the other day and ran into this same issue on a Mac I was using. I was in a time crunch and didn’t have time to open an issue. If you want more info on things I tried, let me know. But I can actually confirm the same on Mac - but on my fedora box, if I downgraded R to the version before, Julia 1.6 did work. I could do a little more testing if you have an issue open on your repo. 😃

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [September 3, 2021, 3:20am UTC](https://discourse.julialang.org/t/segfault-when-jl-init-in-r-also-python-with-julia-1-6-on-macos/58110/5 "2021-09-03T03:20:07Z")

</div>

A work-around is to change the process cwd to the `libjulia` path before calling `jl_init*`. This allows the [failing internal dlopen call](https://github.com/JuliaLang/julia/issues/40246#issuecomment-911992488) to succeed, because one of the dlopen search locations is cwd:

```julia
import ctypes, os
libjl = ctypes.CDLL("/opt/Julia-1.6.app/Contents/Resources/julia/lib/libjulia.dylib")

libjl.jl_get_libdir.restype = ctypes.c_char_p

prev = os.getcwd()
os.chdir(libjl.jl_get_libdir())
libjl.jl_init__threading()
os.chdir(prev)

```

(x-ref [https://github.com/JuliaLang/julia/issues/40246#issuecomment-912223287](https://github.com/JuliaLang/julia/issues/40246#issuecomment-912223287) and [https://github.com/JuliaPy/pyjulia/issues/437#issuecomment-912223632](https://github.com/JuliaPy/pyjulia/issues/437#issuecomment-912223632))
