Simple Python Program with Juliacall Segfaults

This simple Python program calling a Julia function crashes sporadically with segfault.

# juliacall_compute_square.py
from juliacall import Main as jl
print(f"julia version : {jl.VERSION}")
numstring = input("Enter a number\n")
num = jl.parse(jl.Int, numstring)
print(num**2)

I tried running it 10 times in Linux Bash shell:

for i in {1..10}; do
    echo 50 | python juliacall_compute_square.py
done

I see 4 segfaults out of the 10 calls. I have Python 3.13.3, juliacall 0.9.28, Julia 1.12.1 with versioninfo() printout below:

Commit ba1e628ee49 (2025-10-17 13:02 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 22 × Intel(R) Core(TM) Ultra 9 185H
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, alderlake)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 22 virtual cores)

Setting the environment variable PYTHON_JULIACALL_THREADS=1 seems to help.

2 Likes

I seem to recall that newer versions of Python used to give difficulties for PythonCall.jl (though I also think it got fixed in the end). On Python 3.8.3 I get no issues, despite indeed being warned about potential multithreading issues.

UserWarning: Julia was started with multiple threads but multithreading support is experimental in JuliaCall. It is recommended to restart Python with the environment variable PYTHON_JULIACALL_HANDLE_SIGNALS=yes set, otherwise you may experience segfaults or other crashes. Note however that this interferes with Python's own signal handling, so for example Ctrl-C will not raise KeyboardInterrupt. See https://juliapy.github.io/PythonCall.jl/stable/faq/#Is-PythonCall/JuliaCall-thread-safe? for further information. You can suppress this warning by setting PYTHON_JULIACALL_HANDLE_SIGNALS=no.

So perhaps downgrading to an older Python version is an option?

Version
  • Python 3.8.3
  • juliacall 0.9.26
  • Julia versioninfo():
    Julia Version 1.11.3
    Commit d63adeda50d (2025-01-21 19:42 UTC)
    Build Info:
      Official https://julialang.org/ release
    Platform Info:
      OS: Linux (x86_64-linux-gnu)
      CPU: 20 × Intel(R) Core(TM) i9-10900X CPU @ 3.70GHz
      WORD_SIZE: 64
      LLVM: libLLVM-16.0.6 (ORCJIT, cascadelake)
    Threads: 20 default, 0 interactive, 10 GC (on 20 virtual cores)
    Environment:
      JULIA_NUM_THREADS = auto
    

It would help if you posted the full error message.

Here’s what I see:

#> for i in {1..5}; do echo 50 | python juliacall_compute_square.py; done
julia version : 1.12.1
Enter a number
result: 2500
julia version : 1.12.1
Enter a number
result: 2500
Segmentation fault         (core dumped) echo 50 | python juliacall_compute_square.py
julia version : 1.12.1
Enter a number
result: 2500
Segmentation fault         (core dumped) echo 50 | python juliacall_compute_square.py
julia version : 1.12.1
Enter a number
result: 2500
Segmentation fault         (core dumped) echo 50 | python juliacall_compute_square.py
julia version : 1.12.1
Enter a number
result: 2500

I got segfaults at the end of the 2nd, 3rd and 4th runs above, though all runs printed out the correct result 2500.