JuliaIO / VideoIO.jl

Reading and writing of video files in Julia via ffmpeg
https://juliaio.github.io/VideoIO.jl/stable
Other
125 stars 53 forks source link

Segfault at exit involving multithreading and av_packet_free_side_data #381

Open Octogonapus opened 1 year ago

Octogonapus commented 1 year ago

I have been experiencing a segfault that occurs when Julia exits while a background thread is encoding a video. I can't reproduce the segfault when exiting from the encoding thread; this happens only when exiting from one thread while encoding from another. I also can't get you an rr trace since this requires two threads.

MWE:

using VideoIO, Mmap, ThreadPools

nframes = 500
resolution = [2000, 1500]

path, io = mktemp()
mat = Mmap.mmap(io, Array{UInt8,3}, (resolution..., nframes))
for i = 1:nframes
    mat[:, :, i] .= rand(UInt8, resolution...)
end
Mmap.sync!(mat)
close(io)
mat = nothing
GC.gc(true)

ThreadPools.@tspawnat 2 begin
    open(path) do io
        vidmat = Mmap.mmap(io, Array{UInt8,3}, (resolution..., nframes))
        open_video_out(
            "/tmp/out.mp4",
            view(vidmat, :, :, 1),
            framerate=55,
            encoder_options=(color_range=2, crf=0, preset="ultrafast"),
            scanline_major=true,
        ) do video_io
            i = 1
            while i <= nframes
                VideoIO.write(video_io, view(vidmat, :, :, i))
                i += 1
            end
        end
    end
end

sleep(5)
exit(0)
julia -t auto --project --startup-file=no reproduce.jl

signal (11): Segmentation fault
in expression starting at /home/salmon/Documents/code/reproduce_julia_segfault_with_tasks_at_exit/reproduce.jl:36
av_packet_free_side_data at /home/salmon/.julia/artifacts/b409c0eafb4254a980f9e730f6fbe56867890f6a/lib/libavcodec.so (unknown line)
unknown function (ip: (nil))
Allocations: 10866309 (Pool: 10859613; Big: 6696); GC: 44

VideoIO v1.0.3 ThreadPools v2.1.1

julia> versioninfo()
Julia Version 1.8.3
Commit 0434deb161e (2022-11-14 20:14 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 16 on 16 virtual cores

Linux 6.0.12-200.fc36.x86_64