Skip to content

Commit

Permalink
fixup! fix concurrent module loading return value
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Jun 24, 2024
1 parent 9d7d27b commit e78a342
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
17 changes: 8 additions & 9 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2460,28 +2460,27 @@ function _require(pkg::PkgId, env=nothing)
end
# spawn off a new incremental pre-compile task for recursive `require` calls
loaded = maybe_cachefile_lock(pkg, path) do
# double-check now that we have lock
# double-check the search now that we have lock
m = _require_search_from_serialized(pkg, path, UInt128(0), true)
m isa Module && return m
return compilecache(pkg, path; reasons)
end
loaded isa Module && return loaded
cachefile = loaded
if isnothing(cachefile) # maybe_cachefile_lock returns nothing if it had to wait for another process
if isnothing(loaded) # maybe_cachefile_lock returns nothing if it had to wait for another process
@goto load_from_cache # the new cachefile will have the newest mtime so will come first in the search
elseif isa(cachefile, Exception)
if precompilableerror(cachefile)
elseif isa(loaded, Exception)
if precompilableerror(loaded)
verbosity = isinteractive() ? CoreLogging.Info : CoreLogging.Debug
@logmsg verbosity "Skipping precompilation due to precompilable error. Importing $(repr("text/plain", pkg))." exception=m
@logmsg verbosity "Skipping precompilation due to precompilable error. Importing $(repr("text/plain", pkg))." exception=loaded
else
@warn "The call to compilecache failed to create a usable precompiled cache file for $(repr("text/plain", pkg))" exception=m
@warn "The call to compilecache failed to create a usable precompiled cache file for $(repr("text/plain", pkg))" exception=loaded
end
# fall-through to loading the file locally if not incremental
else
cachefile, ocachefile = cachefile::Tuple{String, Union{Nothing, String}}
cachefile, ocachefile = loaded::Tuple{String, Union{Nothing, String}}
loaded = _tryrequire_from_serialized(pkg, cachefile, ocachefile)
if !isa(loaded, Module)
@warn "The call to compilecache failed to create a usable precompiled cache file for $(repr("text/plain", pkg))" exception=m
@warn "The call to compilecache failed to create a usable precompiled cache file for $(repr("text/plain", pkg))" exception=loaded
else
return loaded
end
Expand Down
17 changes: 11 additions & 6 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,11 @@ end
empty!(Base.DEPOT_PATH)
append!(Base.DEPOT_PATH, original_depot_path)

module loaded_pkgid1 end
module loaded_pkgid2 end
module loaded_pkgid3 end
module loaded_pkgid4 end

@testset "loading deadlock detector" begin
pkid1 = Base.PkgId("pkgid1")
pkid2 = Base.PkgId("pkgid2")
Expand All @@ -1221,25 +1226,25 @@ append!(Base.DEPOT_PATH, original_depot_path)
t1 = @async begin
@test nothing === @lock Base.require_lock Base.start_loading(pkid2) # @async module pkgid2; using pkgid1; end
notify(e)
@test "loaded_pkgid1" == @lock Base.require_lock Base.start_loading(pkid1)
@lock Base.require_lock Base.end_loading(pkid2, "loaded_pkgid2")
@test loaded_pkgid1 == @lock Base.require_lock Base.start_loading(pkid1)
@lock Base.require_lock Base.end_loading(pkid2, loaded_pkgid2)
end
wait(e)
reset(e)
t2 = @async begin
@test nothing === @lock Base.require_lock Base.start_loading(pkid3) # @async module pkgid3; using pkgid2; end
notify(e)
@test "loaded_pkgid2" == @lock Base.require_lock Base.start_loading(pkid2)
@lock Base.require_lock Base.end_loading(pkid3, "loaded_pkgid3")
@test loaded_pkgid2 == @lock Base.require_lock Base.start_loading(pkid2)
@lock Base.require_lock Base.end_loading(pkid3, loaded_pkgid3)
end
wait(e)
reset(e)
@test_throws(ConcurrencyViolationError("deadlock detected in loading pkgid3 -> pkgid2 -> pkgid1 -> pkgid3 && pkgid4"),
@lock Base.require_lock Base.start_loading(pkid3)).value # try using pkgid3
@test_throws(ConcurrencyViolationError("deadlock detected in loading pkgid4 -> pkgid4 && pkgid1"),
@lock Base.require_lock Base.start_loading(pkid4)).value # try using pkgid4
@lock Base.require_lock Base.end_loading(pkid1, "loaded_pkgid1") # end
@lock Base.require_lock Base.end_loading(pkid4, "loaded_pkgid4") # end
@lock Base.require_lock Base.end_loading(pkid1, loaded_pkgid1) # end
@lock Base.require_lock Base.end_loading(pkid4, loaded_pkgid4) # end
wait(t2)
wait(t1)
end
Expand Down

0 comments on commit e78a342

Please sign in to comment.