Help with PyCall-related segfault

I posted this also here but hoping to get more eyes on this. The MWE is simple, run the following with julia --threads=2:

using PyCall

Threads.@sync begin
    Threads.@spawn GC.gc(true)
    Threads.@spawn GC.gc(true)

On this one particular system, this is a segfault 100% of the time:

Fatal Python error: Segmentation fault

Current thread 0x00002aaaaab43940 (most recent call first):

signal (11): Segmentation fault
in expression starting at /global/u1/m/marius/work/clem/segfault.jl:3
raise at /lib64/ (unknown line)
_IO_funlockfile at /lib64/ (unknown line)
jl_gc_enable at /global/u1/m/marius/src/julia/src/gc.c:2882
jl_uncompress_ir at /global/u1/m/marius/src/julia/src/ircode.c:781

However, commenting out the using PyCall fixes it.

I observe this with Julia 1.5.3 and 1.6-rc1, and Python 3.7.3 [GCC 7.3.0] :: Anaconda, Inc.. However, its just on this one system, so it must be some wierd thing misconfigured there. However, I’m at a loss for how to track this down. Any suggestions from anyone? Thanks.

I can’t reproduce on Linux. And I tried some combination of more threads and more GC.gc. Maybe it’s because I have:

$ python3
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux

Possibly I just got lucky (and you on the other systems) and that depends on something, like me havig lots of cores.

You might find the new package:

pkg> add

intriguing, and a possible replacement (at least if you have still have a problem after upgrading Python). While the author can’t reproduce my segfault issue there it shouldn’t be a problem, unless you were to use together:

Thanks, I tried upgrading to Python 3.8.5 Anaconda on this one bad system and still segfault. I also tried replacing PyCall with Python, and interestingly enough, also still same segfault.

Regardless, that’s a really interesting looking package, thanks for pointing out!

We might need to acquire the Python GIL in the PyObject finalizer when Julia has multiple threads. I filed an issue and help would be welcome: thread safety · Issue #882 · JuliaPy/PyCall.jl · GitHub

1 Like

For future reference, I tracked it down to that on the bad system, something was setting the bash variable PYTHONFAULTHANDLER=1. I have no idea what this relevant package does, but unsetting that variable fixes it. So moral of story if your Julia crashes all the time after loading PyCall when doing threaded stuff, or crashes when trying to interrupt code (this was actually my original problem), see if you have that variable set and unset it.