# Embedding julia: jl\_atexit\_hook always segfaults

**URL:** https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722
**Category:** General Usage
**Tags:** embedding
**Created:** [January 12, 2024, 10:57am UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722 "2024-01-12T10:57:52Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![xapkohheh](https://avatars.discourse-cdn.com/v4/letter/x/dbc845/32.png) [@xapkohheh](https://discourse.julialang.org/u/xapkohheh)
#### Post date: [January 12, 2024, 10:57am UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/1 "2024-01-12T10:57:52Z")

</div>

Hello there!  
I’ve been trying to embed julia into a digital content creation software named Houdini (I’ve asked questions about this about two years ago, finally got back at it 🙂 )

So everything seems to work fine, but `jl_atexit_hook(0)` always crashes,  
even if everything my plugin does is just this:

```julia
jl_init();
jl_atexit_hook(0);

```

the trace is always looking like this:

```julia
[551813] signal (11.1): Segmentation fault
in expression starting at none:0
je_arena_mapbitsp_read at /home/prisms/builder-new/WeeklyDevToolsHEAD/dev_tools/src/jemalloc/jemalloc-3.6.0/jemalloc-3.6.0/include/jemalloc/internal/arena.h:525 [inlined]
je_arena_mapbits_get at /home/prisms/builder-new/WeeklyDevToolsHEAD/dev_tools/src/jemalloc/jemalloc-3.6.0/jemalloc-3.6.0/include/jemalloc/internal/arena.h:532 [inlined]
je_arena_mapbits_binind_get at /home/prisms/builder-new/WeeklyDevToolsHEAD/dev_tools/src/jemalloc/jemalloc-3.6.0/jemalloc-3.6.0/include/jemalloc/internal/arena.h:573 [inlined]
je_arena_salloc at /home/prisms/builder-new/WeeklyDevToolsHEAD/dev_tools/src/jemalloc/jemalloc-3.6.0/jemalloc-3.6.0/include/jemalloc/internal/arena.h:991 [inlined]
je_isalloc at /home/prisms/builder-new/WeeklyDevToolsHEAD/dev_tools/src/jemalloc/jemalloc-3.6.0/jemalloc-3.6.0/include/jemalloc/internal/jemalloc_internal.h:849 [inlined]
ifree at /home/prisms/builder-new/WeeklyDevToolsHEAD/dev_tools/src/jemalloc/jemalloc-3.6.0/jemalloc-3.6.0/src/jemalloc.c:1228
uv__finish_close at /workspace/srcdir/libuv/src/unix/core.c:313
uv__run_closing_handles at /workspace/srcdir/libuv/src/unix/core.c:327
uv_run at /workspace/srcdir/libuv/src/unix/core.c:415
ijl_atexit_hook at /cache/build/builder-amdci4-6/julialang/julia-release-1-dot-10/src/init.c:336
execute_native_thread_routine at /lib/libstdc++.so.6 (unknown line)
start_thread at /lib/libc.so.6 (unknown line)
__GI___ clone3 at /lib/libc.so.6 (unknown line)
Allocations: 2906 (Pool: 2897; Big: 9); GC: 0

```

my inexperienced eye tells me that this looks like somehow julia’s allocs get mixed up with main software’s jemalloc…

Does anyone have an insight of what is really going on here, or what can I do to determine the cause and find a solution?

---

<div class="post-metadata">

### Author: ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)
#### Post date: [January 12, 2024, 4:26pm UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/2 "2024-01-12T16:26:24Z")

</div>

I am completely inexperienced with this but from a read of [the source code of jl\_atexit\_hook](https://github.com/JuliaLang/julia/blob/5b6a94da5af35a4aa91759cac2f8db7669a6ec2a/src/init.c#L336C9-L336C45) and your stacktrace, it looks it’s not julia per say but rather the underlying libuv which is calling your `ifree` function, and crashing as a result. Are you internally using libuv in your project? In that case, there is probably a conflict with the libuv julia ships with, which is a custom fork (as mentioned [here](https://github.com/JuliaLang/julia/blob/master/doc/src/devdocs/build/build.md#libuv)).

---

<div class="post-metadata">

### Author: ![xapkohheh](https://avatars.discourse-cdn.com/v4/letter/x/dbc845/32.png) [@xapkohheh](https://discourse.julialang.org/u/xapkohheh)
#### Post date: [January 12, 2024, 4:54pm UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/3 "2024-01-12T16:54:06Z")

</div>

unfortunately, all the problems come from the software i’m writing plugin for, and it is closed source. But from the look of it it does not use libuv, so it’s all on julia, however it does use jemalloc that seem to be causing the problems.

using gdb (which I’m completely inexperienced with) I got a but more detailed trace:

```julia
#0 0x00007f0651075413 in je_arena_mapbitsp_read (mapbitsp=0x8050b0)
    at 0x00004413 (/nix/store/d0821saljv4xafnqyyxmh4rh914wfgzj-houdini-runtime-20.0.506/dsolib/libjemalloc.so.1) in include/jemalloc/internal/arena.h:525
#1 je_arena_mapbits_get (pageind=864, chunk=0x800000) at 0x00004413 (/nix/store/d0821saljv4xafnqyyxmh4rh914wfgzj-houdini-runtime-20.0.506/dsolib/libjemalloc.so.1) in include/jemalloc/internal/arena.h:532
#2 je_arena_mapbits_binind_get (pageind=864, chunk=0x800000)
    at 0x00004413 (/nix/store/d0821saljv4xafnqyyxmh4rh914wfgzj-houdini-runtime-20.0.506/dsolib/libjemalloc.so.1) in include/jemalloc/internal/arena.h:573
#3 je_arena_salloc (demote=false, ptr=0xb60c80) at 0x00004413 (/nix/store/d0821saljv4xafnqyyxmh4rh914wfgzj-houdini-runtime-20.0.506/dsolib/libjemalloc.so.1) in include/jemalloc/internal/arena.h:991
#4 je_isalloc (demote=false, ptr=0xb60c80) at 0x00004413 (/nix/store/d0821saljv4xafnqyyxmh4rh914wfgzj-houdini-runtime-20.0.506/dsolib/libjemalloc.so.1) in include/jemalloc/internal/jemalloc_internal.h:849
#5 ifree (ptr=0xb60c80) at 0x00004413 (/nix/store/d0821saljv4xafnqyyxmh4rh914wfgzj-houdini-runtime-20.0.506/dsolib/libjemalloc.so.1) in src/jemalloc.c:1228
#6 0x00007f05d8343459 in uv__finish_close (handle=0xb60c80) at 0x0011c459 (/home/user/project/dso/julia/lib/julia/libjulia-internal.so.1.10) in src/unix/core.c:313
#7 0x00007f05d834349f in uv__run_closing_handles (loop=0x7f05d87ef2c0 <default_loop_struct>)
    at 0x0011c49f (/home/user/project/dso/julia/lib/julia/libjulia-internal.so.1.10) in src/unix/core.c:327
#8 0x00007f05d8343759 in uv_run (loop=0x7f05d87ef2c0 <default_loop_struct>, mode=UV_RUN_DEFAULT)
    at 0x0011c759 (/home/user/project/dso/julia/lib/julia/libjulia-internal.so.1.10) in src/unix/core.c:415
#9 0x00007f05d828f71e in ijl_atexit_hook (exitcode=<optimized out>)
    at 0x0006871e (/home/user/project/dso/julia/lib/julia/libjulia-internal.so.1.10) in /cache/build/builder-amdci4-6/julialang/julia-release-1-dot-10/src/init.c:336
#10 0x00007f06170a65c3 in execute_native_thread_routine () at 0x000e05c3 (/lib/libstdc++.so.6) in /lib/libstdc++.so.6
#11 0x00007f0616d66084 in start_thread () at 0x0008b084 (/lib/libc.so.6) in /lib/libc.so.6
#12 0x00007f0616de860c in clone3 () at 0x0010d60c (/lib/libc.so.6) in /lib/libc.so.6

```

---

<div class="post-metadata">

### Author: ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)
#### Post date: [January 12, 2024, 5:12pm UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/4 "2024-01-12T17:12:36Z")

</div>

I’m afraid solving the root issue is beyond my level of expertise. But if you do not do any kind of IO from the julia side, then I guess you should not care about libuv. In that case, I would suggest creating your own `jl_atexit_hook_nolibuv` function consisting of the [definition of `jl_atexit_hook`](https://github.com/JuliaLang/julia/blob/5b6a94da5af35a4aa91759cac2f8db7669a6ec2a/src/init.c#L240-L356) with all the parts related to libuv removed. It looks like except these `uv_` calls, all the other ones are `jl_` function calls, which should be exported so accessible from within your code.

Or you could completely drop the call to `jl_atexit_hook`. That’s far from ideal if julia has stuff remaining to be done at this point, but otherwise I would assme that’s fine to do if your program exits immediately after anyway?

---

<div class="post-metadata">

### Author: ![xapkohheh](https://avatars.discourse-cdn.com/v4/letter/x/dbc845/32.png) [@xapkohheh](https://discourse.julialang.org/u/xapkohheh)
#### Post date: [January 12, 2024, 6:15pm UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/5 "2024-01-12T18:15:17Z")

</div>

not calling `jl_atexit_hook` at all causes a different segfault by jealloc when thread is being destroyed (i use julia in a dedicated thread)

I’ll try to make an atexit variant that does not make any uv\_ calls and see.

as a workaround to avoid crashes - i can just to `std::quick_exit` , but since it’s done in software’s exit hooks - i rob other plugins from being able to run exit callbacks.

---

<div class="post-metadata">

### Author: ![xapkohheh](https://avatars.discourse-cdn.com/v4/letter/x/dbc845/32.png) [@xapkohheh](https://discourse.julialang.org/u/xapkohheh)
#### Post date: [January 12, 2024, 7:29pm UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/6 "2024-01-12T19:29:21Z")

</div>

hm… it seems that there is just too much of what is used in original `jl_atexit_hook` is not exported, so i cannot just make my own version of it…

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [January 12, 2024, 7:37pm UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/7 "2024-01-12T19:37:47Z")

</div>

What packages are you using? Do any of them install an at exit hook?

---

<div class="post-metadata">

### Author: ![xapkohheh](https://avatars.discourse-cdn.com/v4/letter/x/dbc845/32.png) [@xapkohheh](https://discourse.julialang.org/u/xapkohheh)
#### Post date: [January 12, 2024, 7:43pm UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/8 "2024-01-12T19:43:55Z")

</div>

no packages, clean new julia

and for this test all i do is

```julia
jl_init();
jl_atexit_hook(0);

```

(but in a dedicated thread)

all julia functional otherwise seem to be working fine, can run some evaluations, gc works, no problem there, just in the exiting

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [January 12, 2024, 8:55pm UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/9 "2024-01-12T20:55:02Z")

</div>

Does the other software also use libuv?

---

<div class="post-metadata">

### Author: ![xapkohheh](https://avatars.discourse-cdn.com/v4/letter/x/dbc845/32.png) [@xapkohheh](https://discourse.julialang.org/u/xapkohheh)
#### Post date: [January 12, 2024, 9:26pm UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/10 "2024-01-12T21:26:47Z")

</div>

i looked through it’s .so files and third party library licenses page - nope, no libuv

---

<div class="post-metadata">

### Author: ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)
#### Post date: [October 19, 2024, 12:32am UTC](https://discourse.julialang.org/t/embedding-julia-jl-atexit-hook-always-segfaults/108722/11 "2024-10-19T00:32:12Z")

</div>

> [@xapkohheh](#):
>
> Does anyone have an insight of what is really going on here, or what can I do to determine the cause and find a solution?

Perhaps see this issue, and specifically [this comment](https://github.com/JuliaLang/julia/issues/46298#issuecomment-1220940156) about a simple workaround to test. It worked for me to get rid of an at-exit memory-related error while trying to use Julia through Python in Blender 😉
