# Floop threading and Juliacall produce segmentation fault

**URL:** <https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717>\
**Category:** General Usage\
**Tags:** question, parallel, python, juliacall\
**Created:** [June 22, 2023, 9:50pm UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717 "2023-06-22T21:50:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![CarlosContrerasQ12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carloscontrerasq12/32/43287_2.png) [@CarlosContrerasQ12](https://discourse.julialang.org/u/CarlosContrerasQ12)\
**Post date:** [June 22, 2023, 9:50pm UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/1 "2023-06-22T21:50:25Z")

</div>

Hi! I’m trying to speed up some calculations using julia to call from python. I’m trying to use juliacall as follows:

1. I have a julia threaded function to produce samples

```julia
#"file.jl"
using FLoops

function my_func(N)
    samples=Array{Any}(undef,N)
    @floop for i in 1:N
        samples[i]=sum(ones(100000)*i)
    end
    return samples
end

```

1. I try to call it from python using

```julia
from juliacall import Main as jl
jl.include("/home/carlos/Documentos/Trabajo de grado/Tesis/New code/file.jl")
sa=jl.my_func(10000)
print(sa)

```

but it produces a segmentation fault error, even if it works with the original julia code in the REPL. Someone knows how to fix it?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 22, 2023, 11:13pm UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/2 "2023-06-22T23:13:40Z")

</div>

You have to disable the garbage collection while running the threaded Julia code. Something like this:

```python
def mddf(*args, **kwargs) : 
     jl.GC.enable(False) 
     result = jl.cm.mddf(*args, **kwargs) # this is multi-threaded in Julia 
     jl.GC.enable(True) 
     return result

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [June 23, 2023, 5:41am UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/3 "2023-06-23T05:41:07Z")

</div>

Why is this necessary?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 23, 2023, 7:30am UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/4 "2023-06-23T07:30:54Z")

</div>

> <https://github.com/JuliaPy/PythonCall.jl/issues/219#issuecomment-1242355616>
>
> I have this MWE, where I get segmentation faults (frequently, but not determinis…tically), when trying to run some script that uses multi-threading on the Julia side. 
> 
> I have used before launching \`ipython3\`:
> 
> \`\`\`bash
> export JULIA\_NUM\_THREADS=4
> \`\`\`
> 
> (my computer has 4 cores - 8 threads).
> 
> The MWE is:
> 
> \`\`\`python
> Python 3.10.4 (main, Jun 29 2022, 12:14:53) \[GCC 11.2.0\]
> Type 'copyright', 'credits' or 'license' for more information
> IPython 7.31.1 -- An enhanced Interactive Python. Type '?' for help.
> 
> In \[1\]: from juliacall import Main as jl
> 
> In \[2\]: import numpy as np
> 
> In \[3\]: jl.seval("""
> ...: function test(x)
> ...: partial = zeros(Threads.nthreads())
> ...: Threads.@threads for i in 1:Threads.nthreads()
> ...: for j in i:Threads.nthreads():length(x)
> ...: partial\[i\] += x\[j\]
> ...: end
> ...: end
> ...: return sum(partial)
> ...: end
> ...: """)
> Out\[3\]: test (generic function with 1 method)
> 
> In \[4\]: x = np.random.random((10\_000,))
> 
> In \[5\]: %timeit jl.test(x)
> 72.2 µs ± 35.9 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
> 
> In \[6\]: %timeit jl.test(x)
> Segmentation fault (core dumped)
> 
> \`\`\`
> 
> Here I have emulated the error using the \`%timeit\` macro from \`ipython\`, but my actual error I get after some runs of a function of my package:
> 
> \`\`\`python
> In \[1\]: from juliacall import Main as jl
> 
> In \[2\]: jl.seval("using CellListMap")
> 
> In \[3\]: import numpy as np
> 
> In \[5\]: x = np.random.random((10\_000,3))
> 
> In \[6\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[7\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[8\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[9\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[10\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[11\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[12\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[13\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[14\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[15\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[16\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[17\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[18\]: nb = jl.neighborlist(x.transpose(), 0.05)
> 
> In \[19\]: nb = jl.neighborlist(x.transpose(), 0.05)
> Segmentation fault (core dumped)
> \`\`\`
> 
> \`%timeit\` runs the function multiple times, there seems to be some memory corruption, or memory overflow, causing the error.
> 
> Anyway, even if you have only some hint on how to debug this, I will be very thankful. 
> 
> (even in the simplest example above, the segfaults only occur with multi-threading).

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [June 23, 2023, 8:01am UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/5 "2023-06-23T08:01:47Z")

</div>

Is this a problem even if you aren’t passing any python object to Julia? Does PyCall & Co have the same limitation? This sounds like a severe limitation in JuliaCall and PythonCall 😕

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 23, 2023, 11:02am UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/6 "2023-06-23T11:02:17Z")

</div>

It seems that there is something similar, although I didn’t go into the details to see if it is the same thing: [thread safety · Issue #882 · JuliaPy/PyCall.jl · GitHub](https://github.com/JuliaPy/PyCall.jl/issues/882)

---

<div class="post-metadata">

**Author:** ![CarlosContrerasQ12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carloscontrerasq12/32/43287_2.png) [@CarlosContrerasQ12](https://discourse.julialang.org/u/CarlosContrerasQ12)\
**Post date:** [June 23, 2023, 11:37am UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/7 "2023-06-23T11:37:50Z")

</div>

Surprisingly it works without much effort using PyJulia, without disabling the garbage collector. However, I haven’t tested it works well enough yet, without bugs.

---

<div class="post-metadata">

**Author:** ![CarlosContrerasQ12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carloscontrerasq12/32/43287_2.png) [@CarlosContrerasQ12](https://discourse.julialang.org/u/CarlosContrerasQ12)\
**Post date:** [June 23, 2023, 3:58pm UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/8 "2023-06-23T15:58:04Z")

</div>

Sorry, it didn’t work, segmentation fault is still there

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 23, 2023, 5:43pm UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/9 "2023-06-23T17:43:03Z")

</div>

Uhm… it worked here, but note that your function allocates _a lot_, so I cannot run it here without having the GC turned on. Thus, I modified it such that it does not allocate as much. In that case, it worked:

```python
In [1]: from juliacall import Main as jl

In [2]: jl.seval("using FLoops")

In [3]: jl.seval("""
   ...: function my_func(N)
   ...: samples=Array{Float64}(undef,N)
   ...: @floop for i in 1:N
   ...: samples[i]=sum(i for i in 1:10^4)
   ...: end
   ...: return samples
   ...: end
   ...: """)
Out[3]: my_func (generic function with 1 method)

In [4]: jl.GC.enable(False)
Out[4]: True

In [5]: jl.my_func(10000)
Out[5]: 
10000-element Vector{Float64}:
 5.0005e7
...
 5.0005e7

In [9]: jl.my_func(10000)
Out[9]: 
10000-element Vector{Float64}:
 5.0005e7
...
 5.0005e7

In [10]: jl.my_func(10000)
Out[10]: 
10000-element Vector{Float64}:
 5.0005e7
...
 5.0005e7

In [11]: jl.my_func(10000)
Out[11]: 
10000-element Vector{Float64}:
 5.0005e7
...
 5.0005e7

In [12]: jl.my_func(10000)
Out[12]: 
10000-element Vector{Float64}:
 5.0005e7
...
 5.0005e7

In [13]: %timeit jl.my_func(10000)
43.4 µs ± 2.72 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [14]: jl.GC.enable(True)
Out[14]: False

In [15]: %timeit jl.my_func(10000)
Segmentation fault (core dumped)

```

I ran the function several times without GC, and didn’t get the segmentation fault, and then with it I get it. The `%timeit` macro (from ipython3) runs the function several times.

If I run your original function the section gets Killed because of lack of memory, but that’s another, albeit related, issue.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 23, 2023, 5:55pm UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/10 "2023-06-23T17:55:45Z")

</div>

That is, this limitation does require that the threaded code is minimally allocating, or one can have memory issues given that GC is off on the Julia side.

Anyway, that is a workaround, it may not work in every case, and I’m not a specialist on it.

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [June 23, 2023, 10:48pm UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/11 "2023-06-23T22:48:18Z")

</div>

Does Python replace/override the fault handlers of Julia?

When multi-threaded GC uses read-protected pages to implement safepoints (e.g. to ask other threads to stop doing work). This causes a benign segmentation fault, but it must be handled by the Julia segmentation handler.

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [June 23, 2023, 10:49pm UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/12 "2023-06-23T22:49:36Z")

</div>

This would do it.

> <https://github.com/cjdoris/PythonCall.jl/blob/e374e2503b7b843c457d30596a2428ebfc26bb0b/pysrc/juliacall/ __init__.py#L132>

---

<div class="post-metadata">

**Author:** ![CarlosContrerasQ12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carloscontrerasq12/32/43287_2.png) [@CarlosContrerasQ12](https://discourse.julialang.org/u/CarlosContrerasQ12)\
**Post date:** [June 26, 2023, 3:16am UTC](https://discourse.julialang.org/t/floop-threading-and-juliacall-produce-segmentation-fault/100717/13 "2023-06-26T03:16:40Z")

</div>

Sorry, can you be a bit more explicit? How can I change it? What would be different?/
