-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: during using
, define module before include, to solve a minor data-race on UUID
#43257
Open
vtjnash
wants to merge
1
commit into
master
Choose a base branch
from
jn/requirenewmodule
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−198
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -340,7 +340,7 @@ to get the file name part of the path. | |
""" | ||
function pathof(m::Module) | ||
@lock require_lock begin | ||
pkgid = get(module_keys, m, nothing) | ||
pkgid = PkgId(m) | ||
pkgid === nothing && return nothing | ||
origin = get(pkgorigins, pkgid, nothing) | ||
origin === nothing && return nothing | ||
|
@@ -757,6 +757,7 @@ end | |
# or an Exception that describes why it couldn't be loaded | ||
# and it reconnects the Base.Docs.META | ||
function _include_from_serialized(path::String, depmods::Vector{Any}) | ||
assert_havelock(require_lock) | ||
sv = ccall(:jl_restore_incremental, Any, (Cstring, Any), path, depmods) | ||
if isa(sv, Exception) | ||
return sv | ||
|
@@ -950,7 +951,7 @@ Specify whether the file calling this function is precompilable, defaulting to ` | |
If a module or file is *not* safely precompilable, it should call `__precompile__(false)` in | ||
order to throw an error if Julia attempts to precompile it. | ||
""" | ||
@noinline function __precompile__(isprecompilable::Bool=true) | ||
@noinline function Core.__precompile__(isprecompilable::Bool=true) | ||
if !isprecompilable && ccall(:jl_generating_output, Cint, ()) != 0 | ||
throw(PrecompilableError()) | ||
end | ||
|
@@ -1071,37 +1072,29 @@ function require(uuidkey::PkgId) | |
end | ||
|
||
const loaded_modules = Dict{PkgId,Module}() | ||
const module_keys = IdDict{Module,PkgId}() # the reverse | ||
|
||
is_root_module(m::Module) = @lock require_lock haskey(module_keys, m) | ||
root_module_key(m::Module) = @lock require_lock module_keys[m] | ||
function PkgId(m::Module) | ||
name = String(nameof(moduleroot(m))) | ||
uuid = UUID(ccall(:jl_module_uuid, NTuple{2, UInt64}, (Any,), m)) | ||
UInt128(uuid) == 0 ? PkgId(name) : PkgId(uuid, name) | ||
end | ||
|
||
function register_root_module(m::Module) | ||
# n.b. This is called from C after creating a new module in `Base.__toplevel__`, | ||
# instead of adding them to the binding table there. | ||
@lock require_lock begin | ||
key = PkgId(m, String(nameof(m))) | ||
function register_root_module(m::Module, key::PkgId=PkgId(m)) | ||
assert_havelock(require_lock) | ||
if haskey(loaded_modules, key) | ||
oldm = loaded_modules[key] | ||
if oldm !== m | ||
@warn "Replacing module `$(key.name)`" | ||
end | ||
end | ||
loaded_modules[key] = m | ||
module_keys[m] = key | ||
end | ||
nothing | ||
end | ||
|
||
@lock require_lock begin | ||
register_root_module(Core) | ||
register_root_module(Base) | ||
register_root_module(Main) | ||
|
||
# This is used as the current module when loading top-level modules. | ||
# It has the special behavior that modules evaluated in it get added | ||
# to the loaded_modules table instead of getting bindings. | ||
baremodule __toplevel__ | ||
using Base | ||
end | ||
|
||
# get a top-level Module from the given key | ||
|
@@ -1116,9 +1109,8 @@ loaded_modules_array() = @lock require_lock collect(values(loaded_modules)) | |
function unreference_module(key::PkgId) | ||
if haskey(loaded_modules, key) | ||
m = pop!(loaded_modules, key) | ||
# need to ensure all modules are GC rooted; will still be referenced | ||
# in module_keys | ||
end | ||
nothing | ||
end | ||
|
||
# Returns `nothing` or the name of the newly-created cachefile | ||
|
@@ -1192,21 +1184,17 @@ function _require(pkg::PkgId) | |
|
||
# just load the file normally via include | ||
# for unknown dependencies | ||
newm = Module(Symbol(pkg.name), false) | ||
uuid = pkg.uuid | ||
uuid = (uuid === nothing ? (UInt64(0), UInt64(0)) : convert(NTuple{2, UInt64}, uuid)) | ||
old_uuid = ccall(:jl_module_uuid, NTuple{2, UInt64}, (Any,), __toplevel__) | ||
if uuid !== old_uuid | ||
ccall(:jl_set_module_uuid, Cvoid, (Any, NTuple{2, UInt64}), __toplevel__, uuid) | ||
if uuid !== nothing | ||
ccall(:jl_set_module_uuid, Cvoid, (Any, NTuple{2, UInt64}), newm, uuid) | ||
end | ||
register_root_module(newm, pkg) | ||
unlock(require_lock) | ||
try | ||
include(__toplevel__, path) | ||
return | ||
eval(newm, :(baremodule $(nameof(newm)); $include($newm, $path); end)) | ||
finally | ||
lock(require_lock) | ||
if uuid !== old_uuid | ||
ccall(:jl_set_module_uuid, Cvoid, (Any, NTuple{2, UInt64}), __toplevel__, old_uuid) | ||
end | ||
end | ||
finally | ||
toplevel_load[] = last | ||
|
@@ -1362,20 +1350,26 @@ function include_package_for_output(pkg::PkgId, input::String, depot_path::Vecto | |
Base._track_dependencies[] = true | ||
get!(Base.PkgOrigin, Base.pkgorigins, pkg).path = input | ||
append!(empty!(Base._concrete_dependencies), concrete_deps) | ||
uuid_tuple = pkg.uuid === nothing ? (UInt64(0), UInt64(0)) : convert(NTuple{2, UInt64}, pkg.uuid) | ||
|
||
ccall(:jl_set_module_uuid, Cvoid, (Any, NTuple{2, UInt64}), Base.__toplevel__, uuid_tuple) | ||
if source !== nothing | ||
task_local_storage()[:SOURCE_PATH] = source | ||
end | ||
|
||
newm = Module(Symbol(pkg.name), false) | ||
uuid = pkg.uuid | ||
if uuid !== nothing | ||
ccall(:jl_set_module_uuid, Cvoid, (Any, NTuple{2, UInt64}), newm, uuid) | ||
# HACK for preferences | ||
ccall(:jl_set_module_uuid, Cvoid, (Any, NTuple{2, UInt64}), Main, uuid) | ||
end | ||
@lock require_lock register_root_module(newm, pkg) | ||
try | ||
Base.include(Base.__toplevel__, input) | ||
eval(newm, :(baremodule $(nameof(newm)); $include($newm, $input); end)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the extra There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running |
||
catch ex | ||
precompilableerror(ex) || rethrow() | ||
@debug "Aborting `create_expr_cache'" exception=(ErrorException("Declaration of __precompile__(false) not allowed"), catch_backtrace()) | ||
exit(125) # we define status = 125 means PrecompileableError | ||
end | ||
nothing | ||
end | ||
|
||
const PRECOMPILE_TRACE_COMPILE = Ref{String}() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this can stay in pkgid.jl?