Segfault when jl_init in R (also python?) with julia 1.6 on macOS

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
The issue can be simplified to the following code in R:

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):

 *** 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

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!

1 Like

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

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

The segfault error can also be replicated in python simply by

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

1 Like

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. :smiley:

1 Like

A work-around is to change the process cwd to the libjulia path before calling jl_init*. This allows the failing internal dlopen call to succeed, because one of the dlopen search locations is cwd:

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 and https://github.com/JuliaPy/pyjulia/issues/437#issuecomment-912223632)

1 Like