Skip to content

Commit 6c8c120

Browse files
Kristoffer Carlssonjohnnychen94
andauthored
move package loading behind a lock (#339)
* move package loading behind a lock * add threaded loading test only run threaded test on Julia >= 1.3 Co-authored-by: Johnny Chen <johnnychen94@hotmail.com>
1 parent 7345261 commit 6c8c120

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/loadsave.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,15 @@ const fileiofuncs = Dict{Symbol,Function}(:load => load,
192192
:save => save,
193193
:savestreaming => savestreaming)
194194

195+
const require_lock = ReentrantLock()
195196
function action(call::Symbol, libraries::Vector{ActionSource}, @nospecialize(file::Formatted), args...; options...)
196197
issave = call (:save, :savestreaming)
197198
failures = Tuple{Any,ActionSource,Vector}[]
198199
pkgfuncname = Symbol("fileio_", call)
199200
local mod
200201
for library in libraries
201202
try
202-
mod = isa(library, Module) ? library : Base.require(library)
203+
mod = isa(library, Module) ? library : lock(() -> Base.require(library), require_lock)
203204
f = if isdefined(mod, pkgfuncname)
204205
getfield(mod, pkgfuncname)
205206
else

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ struct MimeSaveTestType
1212
end
1313

1414
@testset "FileIO" begin
15+
# This threaded test should be put before `CSVFiles` is loaded
16+
if VERSION >= v"1.3"
17+
# FIXME: threaded file io is still somehow broken in old Julia versions
18+
include("threaded_loading.jl")
19+
end
20+
1521
include("query.jl")
1622
include("loadsave.jl")
1723
include("error_handling.jl")

test/threaded_loading.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@testset "Threaded loading" begin
2+
if Threads.nthreads() > 1
3+
pkgid = Base.PkgId(FileIO.idCSVFiles.second, "CSVFiles")
4+
@assert !Base.root_module_exists(pkgid) "threaded loaidng test requires CSVFiles not loaded"
5+
# When threaded, loading a new backend should be locked
6+
# https://github.com/JuliaIO/FileIO.jl/issues/336
7+
files = map(["data.csv", "file.csv"]) do filename
8+
joinpath(@__DIR__, "files", filename)
9+
end
10+
@test_nowarn Threads.@threads for file in files
11+
load(file)
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)