# Help with PyCall-related segfault

**URL:** <https://discourse.julialang.org/t/help-with-pycall-related-segfault/55998>\
**Category:** General Usage\
**Created:** [February 25, 2021, 10:19am UTC](https://discourse.julialang.org/t/help-with-pycall-related-segfault/55998 "2021-02-25T10:19:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 25, 2021, 10:19am UTC](https://discourse.julialang.org/t/help-with-pycall-related-segfault/55998/1 "2021-02-25T10:19:42Z")

</div>

I posted this also [here](https://github.com/JuliaPy/PyCall.jl/issues/881) but hoping to get more eyes on this. The MWE is simple, run the following with `julia --threads=2`:

```julia
using PyCall

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

```

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

```julia
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/libpthread.so.0 (unknown line)
_IO_funlockfile at /lib64/libpthread.so.0 (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.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [February 25, 2021, 10:40am UTC](https://discourse.julialang.org/t/help-with-pycall-related-segfault/55998/2 "2021-02-25T10:40:18Z")

</div>

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

```julia
$ 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:

```julia
pkg> add https://github.com/cjdoris/Python.jl

```

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:

[https://github.com/cjdoris/Python.jl/issues/6](https://github.com/cjdoris/Python.jl/issues/6)

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 25, 2021, 11:35am UTC](https://discourse.julialang.org/t/help-with-pycall-related-segfault/55998/3 "2021-02-25T11:35:26Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 25, 2021, 1:49pm UTC](https://discourse.julialang.org/t/help-with-pycall-related-segfault/55998/4 "2021-02-25T13:49:14Z")

</div>

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](https://github.com/JuliaPy/PyCall.jl/issues/882)

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 26, 2021, 7:54am UTC](https://discourse.julialang.org/t/help-with-pycall-related-segfault/55998/5 "2021-02-26T07:54:03Z")

</div>

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](https://pypi.org/project/faulthandler/) 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.
