Skip to content

Commit 239a1f2

Browse files
committed
loading: stop corrupting memory all over the place
Regressions introduced by #45607
1 parent d0e28af commit 239a1f2

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

base/loading.jl

+15-9
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,9 @@ the form `pkgversion(@__MODULE__)` can be used.
460460
function pkgversion(m::Module)
461461
rootmodule = moduleroot(m)
462462
pkg = PkgId(rootmodule)
463-
pkgorigin = get(pkgorigins, pkg, nothing)
463+
pkgorigin = @lock require_lock begin
464+
get(pkgorigins, pkg, nothing)
465+
end
464466
return pkgorigin === nothing ? nothing : pkgorigin.version
465467
end
466468

@@ -952,8 +954,8 @@ function _tryrequire_from_serialized(modkey::PkgId, build_id::UInt64)
952954
try
953955
modpath = locate_package(modkey)
954956
modpath === nothing && return nothing
957+
set_pkgorigin_version_path(modkey, String(modpath))
955958
loaded = _require_search_from_serialized(modkey, String(modpath), build_id)
956-
get!(PkgOrigin, pkgorigins, modkey).path = modpath
957959
finally
958960
loading = pop!(package_locks, modkey)
959961
notify(loading, loaded, all=true)
@@ -971,7 +973,7 @@ end
971973

972974
# loads a precompile cache file, ignoring stale_cachefile tests
973975
# assuming all depmods are already loaded and everything is valid
974-
function _tryrequire_from_serialized(modkey::PkgId, path::String, depmods::Vector{Any})
976+
function _tryrequire_from_serialized(modkey::PkgId, path::String, sourcepath::String, depmods::Vector{Any})
975977
assert_havelock(require_lock)
976978
loaded = nothing
977979
if root_module_exists(modkey)
@@ -992,6 +994,7 @@ function _tryrequire_from_serialized(modkey::PkgId, path::String, depmods::Vecto
992994
end
993995
package_locks[modkey] = Threads.Condition(require_lock)
994996
try
997+
set_pkgorigin_version_path(modkey, sourcepath)
995998
loaded = _include_from_serialized(modkey, path, depmods)
996999
finally
9971000
loading = pop!(package_locks, modkey)
@@ -1059,7 +1062,7 @@ end
10591062
continue
10601063
end
10611064
modstaledeps = modstaledeps::Vector{Any}
1062-
staledeps[i] = (modkey, modpath_to_try, modstaledeps)
1065+
staledeps[i] = (modpath, modkey, modpath_to_try, modstaledeps)
10631066
modfound = true
10641067
break
10651068
end
@@ -1081,8 +1084,8 @@ end
10811084
for i in 1:length(staledeps)
10821085
dep = staledeps[i]
10831086
dep isa Module && continue
1084-
modkey, modpath_to_try, modstaledeps = dep::Tuple{PkgId, String, Vector{Any}}
1085-
dep = _tryrequire_from_serialized(modkey, modpath_to_try, modstaledeps)
1087+
modpath, modkey, modpath_to_try, modstaledeps = dep::Tuple{String, PkgId, String, Vector{Any}}
1088+
dep = _tryrequire_from_serialized(modkey, modpath_to_try, modpath, modstaledeps)
10861089
if !isa(dep, Module)
10871090
@debug "Rejecting cache file $path_to_try because required dependency $modkey failed to load from cache file for $modpath." exception=dep
10881091
staledeps = true
@@ -1324,7 +1327,9 @@ function unreference_module(key::PkgId)
13241327
end
13251328
end
13261329

1327-
function set_pkgorigin_version_path(pkg, path)
1330+
# whoever takes the package_locks[pkg] must call this function immediately
1331+
function set_pkgorigin_version_path(pkg::PkgId, path::Union{String,Nothing})
1332+
assert_havelock(require_lock)
13281333
pkgorigin = get!(PkgOrigin, pkgorigins, pkg)
13291334
if path !== nothing
13301335
project_file = locate_project_file(joinpath(dirname(path), ".."))
@@ -1337,6 +1342,7 @@ function set_pkgorigin_version_path(pkg, path)
13371342
end
13381343
end
13391344
pkgorigin.path = path
1345+
nothing
13401346
end
13411347

13421348
# Returns `nothing` or the new(ish) module
@@ -1356,13 +1362,13 @@ function _require(pkg::PkgId)
13561362
toplevel_load[] = false
13571363
# perform the search operation to select the module file require intends to load
13581364
path = locate_package(pkg)
1359-
set_pkgorigin_version_path(pkg, path)
13601365
if path === nothing
13611366
throw(ArgumentError("""
13621367
Package $pkg is required but does not seem to be installed:
13631368
- Run `Pkg.instantiate()` to install all recorded dependencies.
13641369
"""))
13651370
end
1371+
set_pkgorigin_version_path(pkg, path)
13661372

13671373
# attempt to load the module file via the precompile cache locations
13681374
if JLOptions().use_compiled_modules != 0
@@ -1436,6 +1442,7 @@ end
14361442

14371443
function _require_from_serialized(uuidkey::PkgId, path::String)
14381444
@lock require_lock begin
1445+
set_pkgorigin_version_path(uuidkey, nothing)
14391446
newm = _tryrequire_from_serialized(uuidkey, path)
14401447
newm isa Module || throw(newm)
14411448
# After successfully loading, notify downstream consumers
@@ -2160,7 +2167,6 @@ end
21602167
@debug "Rejecting cache file $cachefile because dependency $req_key not found."
21612168
return true # Won't be able to fulfill dependency
21622169
end
2163-
set_pkgorigin_version_path(req_key, path)
21642170
depmods[i] = (path, req_key, req_build_id)
21652171
end
21662172
end

0 commit comments

Comments
 (0)