Segmentation fault when loading a Flux model with RFFT Plan

Hello,

I’m pretty new to Julia, so this problem could be caused by my inexperience.
I’m trying to train a Flux model that needs to do a Fourier transform. For performance reasons, I wanted the transform to be precomputed and saved in the struct defining the model as a rfft plan. Saving and loading the model in the same Julia session is no problem, but when loading the model in a different session, I get a segmentation fault. Here’s a minimal example:

using Flux
using FFTW
using BSON: @load, @save

struct FFTLayer
my_plan::Any
end

function (f::FFTLayer)(x)
    return f.my_plan * x
end
Flux.@functor FFTLayer

function savemodel()
    plan = plan_rfft(rand(Float32, 100, 100))
    model = FFTLayer(plan)
    @save "mymodel.bson" model
end

function loadmodel()
    @load "mymodel.bson" model
    x = rand(Float32, 100, 100)
    return model(x)
end

Now run savemodel() and then loadmodel() in the same session: Everything works as expected.
Now start another REPL and run loadmodel() again and Julia should crash.
In my case I get the following error:

signal (11): Segmentation fault
in expression starting at REPL[2]:1
fftwf_execute_dft_r2c at /home/lukas/.julia/artifacts/e95ca94c82899616429924e9fdc7eccda275aa38/lib/libfftw3f.so (unknown line)
unsafe_execute! at /home/lukas/.julia/packages/FFTW/sfy1o/src/fft.jl:505 [inlined]
* at /home/lukas/.julia/packages/FFTW/sfy1o/src/fft.jl:871
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
FFTLayer at /home/lukas/Documents/Flux_fftplan/example.jl:10
unknown function (ip: 0x7fee20095ed2)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
loadmodel at /home/lukas/Documents/Flux_fftplan/example.jl:23
unknown function (ip: 0x7fee2008e77f)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1788 [inlined]
do_call at /buildworker/worker/package_linux64/build/src/interpreter.c:126
eval_value at /buildworker/worker/package_linux64/build/src/interpreter.c:215
eval_stmt_value at /buildworker/worker/package_linux64/build/src/interpreter.c:166 [inlined]
eval_body at /buildworker/worker/package_linux64/build/src/interpreter.c:587
jl_interpret_toplevel_thunk at /buildworker/worker/package_linux64/build/src/interpreter.c:731
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:885
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:830
eval_body at /buildworker/worker/package_linux64/build/src/interpreter.c:550
eval_body at /buildworker/worker/package_linux64/build/src/interpreter.c:516
jl_interpret_toplevel_thunk at /buildworker/worker/package_linux64/build/src/interpreter.c:731
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:885
jl_toplevel_eval_in at /buildworker/worker/package_linux64/build/src/toplevel.c:944
eval at ./boot.jl:373 [inlined]
eval_user_input at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:150
repl_backend_loop at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:244
start_repl_backend at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:229
#run_repl#47 at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:362
run_repl at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:349
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
#930 at ./client.jl:394
jfptr_YY.930_32578.clone_1 at /home/lukas/julia-1.7.0/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1788 [inlined]
jl_f__call_latest at /buildworker/worker/package_linux64/build/src/builtins.c:757
#invokelatest#2 at ./essentials.jl:716 [inlined]
invokelatest at ./essentials.jl:714 [inlined]
run_main_repl at ./client.jl:379
exec_options at ./client.jl:309
_start at ./client.jl:495
jfptr__start_43127.clone_1 at /home/lukas/julia-1.7.0/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1788 [inlined]
true_main at /buildworker/worker/package_linux64/build/src/jlapi.c:559
jl_repl_entrypoint at /buildworker/worker/package_linux64/build/src/jlapi.c:701
main at julia (unknown line)
__libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
unknown function (ip: 0x400808)
Allocations: 23397022 (Pool: 23393025; Big: 3997); GC: 15
Segmentation fault (core dumped)

I’m on Julia 1.7.0 and Linux Mint 20.3 x86_64. FFTW is version 1.5.0, BSON 0.3.5, Flux 0.13.3
Does anybody know what’s going on here or what I am doing wrong?

Thanks in advance!
Lukas