Creating fully self-contained and pre-compiled library

Hi everyone, we’re now using JuliaC.jl to create a shared library, with all dependencies bundled – awesome! Thanks a lot to the folks making this possible!

Now, two points are still a bit tricky, and we did run into problems with (most probably) them.

First, thread safety of the JuliaC-generated shared library. When embedding julia, one has to take care of only using the jl_init-ing or julia-spawned threads for all further jl_* calls (Embedding Julia · The Julia Language). How does this transfer over to a JuliaC-generate shared lib? Shall only a single thread load the library, and then handle all the calls to the Base.@ccallable functions? We’re getting segfaults after around 70 calls when we call from different threads, but none when calling from a single thread.

Is this documented somewhere and we just missed it?

Second, a low-level detail on memory handling and the julia-side GC. I first had something like this:

buf_size = io.size # io is an IOBuffer holding an encoded protobuf-message
out_ptr = Ptr{UInt8}(Libc.malloc(buf_size + 4))
unsafe_copyto!(out_ptr, pointer(vcat(sizeinfo_bytes, e.io.data[1:buf_size])), buf_size + 4)
return out_ptr

Now, my AI-coding assistant mentioned (when working on the first issue) that the julia GC might collect the temporary array created via vcat, while the copying-process was still ongoing. The proposed solution is:

payload = take!(e.io)
buf_size = length(payload)
out_ptr = Ptr{UInt8}(Libc.malloc(buf_size + 4))
# directly write size information
unsafe_store!(out_ptr,     UInt8((buf_size >> 24) & 0xff))
unsafe_store!(out_ptr + 1, UInt8((buf_size >> 16) & 0xff))
unsafe_store!(out_ptr + 2, UInt8((buf_size >> 8)  & 0xff))
unsafe_store!(out_ptr + 3, UInt8(buf_size & 0xff))
# Copy payload from Julia vector
GC.@preserve payload begin
    Base.unsafe_copyto!(out_ptr + 4, pointer(payload), buf_size)
end
return out_ptr

Is this actually “better”? Is it actually required to avoid any unsafe behaviour?

Thanks a lot in advance!

9 Likes