Skip to content
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

Cannot serialize during __init__ #56916

Open
willow-ahrens opened this issue Dec 28, 2024 · 3 comments
Open

Cannot serialize during __init__ #56916

willow-ahrens opened this issue Dec 28, 2024 · 3 comments
Labels
bug Indicates an unexpected problem or unintended behavior packages Package management and loading regression 1.11 Regression in the 1.11 release stdlib Julia's standard library

Comments

@willow-ahrens
Copy link
Contributor

On Version 1.11.2, serialization during __init__ results in a KeyError. Consider the following package:

module Foo

using Serialization

struct Bar
    x::Int
end

function __init__()
    mktemp() do path, io
        serialize(io, Bar(42))
    end
end

end # module Foo

Running using Foo results in:

ERROR: InitError: KeyError: key Foo not found
Stacktrace:
  [1] getindex
    @ ./iddict.jl:98 [inlined]
  [2] macro expansion
    @ ./lock.jl:273 [inlined]
  [3] root_module_key
    @ ./loading.jl:2420
  [4] serialize_mod_names
    @ ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/Serialization/src/Serialization.jl:390
  [5] serialize
    @ ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/Serialization/src/Serialization.jl:402 [inlined]
  [6] serialize_type_data
    @ ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/Serialization/src/Serialization.jl:595
  [7] serialize_type
    @ ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/Serialization/src/Serialization.jl:627
  [8] serialize_any
    @ ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/Serialization/src/Serialization.jl:0
  [9] serialize
    @ ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/Serialization/src/Serialization.jl:680 [inlined]
 [10] serialize
    @ ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/Serialization/src/Serialization.jl:807 [inlined]
 [11] #1
    @ ~/Projects/Foo/src/Foo.jl:11 [inlined]
 [12] mktemp
    @ ./file.jl:790
 [13] mktemp
    @ ./file.jl:788 [inlined]
 [14] __init__()
    @ Foo ~/Projects/Foo/src/Foo.jl:10
 [15] run_module_init(mod::Module, i::Int64)
    @ Base ./loading.jl:1378
 [16] register_restored_modules(sv::Core.SimpleVector, pkg::Base.PkgId, path::String)
    @ Base ./loading.jl:1366
 [17] _include_from_serialized(pkg::Base.PkgId, path::String, ocachepath::String, depmods::Vector{Any}, ignore_native::Nothing; register::Bool)
    @ Base ./loading.jl:1254
 [18] _include_from_serialized (repeats 2 times)
    @ ./loading.jl:1210 [inlined]
 [19] _require_search_from_serialized(pkg::Base.PkgId, sourcepath::String, build_id::UInt128, stalecheck::Bool; reasons::Dict{String, Int64}, DEPOT_PATH::Vector{String})
    @ Base ./loading.jl:2057
 [20] _require(pkg::Base.PkgId, env::String)
    @ Base ./loading.jl:2527
 [21] __require_prelocked(uuidkey::Base.PkgId, env::String)
    @ Base ./loading.jl:2388
 [22] #invoke_in_world#3
    @ ./essentials.jl:1089 [inlined]
 [23] invoke_in_world
    @ ./essentials.jl:1086 [inlined]
 [24] _require_prelocked(uuidkey::Base.PkgId, env::String)
    @ Base ./loading.jl:2375
 [25] macro expansion
    @ ./loading.jl:2314 [inlined]
 [26] macro expansion
    @ ./lock.jl:273 [inlined]
 [27] __require(into::Module, mod::Symbol)
    @ Base ./loading.jl:2271
 [28] #invoke_in_world#3
    @ ./essentials.jl:1089 [inlined]
 [29] invoke_in_world
    @ ./essentials.jl:1086 [inlined]
 [30] require(into::Module, mod::Symbol)
    @ Base ./loading.jl:2260
during initialization of module Foo
@willow-ahrens
Copy link
Contributor Author

willow-ahrens commented Dec 28, 2024

I think this may be related to #56329 or perhaps #56077

@nsajko nsajko added stdlib Julia's standard library bug Indicates an unexpected problem or unintended behavior packages Package management and loading regression 1.11 Regression in the 1.11 release labels Dec 28, 2024
@nsajko
Copy link
Contributor

nsajko commented Dec 28, 2024

using Foo throws on v1.11, but not on v1.10 or v1.12. Perhaps a backportable fix exists on v1.12?

@willow-ahrens
Copy link
Contributor Author

Ah, but on 1.12 we cannot read the file that is written. Consider:

module Foo

using Serialization

struct Bar
    x::Int
end

function __init__()
    if isfile("./hello")
        open("./hello", "r") do io
            println(deserialize(io))
        end
    end
    open("./hello", "w") do io
        serialize(io, Bar(42))
    end
end

end # module Foo

On the second run, this yields:

ERROR: InitError: KeyError: key Base.PkgId(Base.UUID("af919380-6608-446a-b46d-163e99a4cdff"), "Foo") not found
Stacktrace:
  [1] getindex
    @ ./dict.jl:477 [inlined]
  [2] macro expansion
    @ ./lock.jl:294 [inlined]
  [3] root_module(key::Base.PkgId)
    @ Base ./loading.jl:2563
  [4] deserialize_module(s::Serialization.Serializer{IOStream})
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:1019
  [5] handle_deserialize(s::Serialization.Serializer{IOStream}, b::Int32)
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:921
  [6] deserialize(s::Serialization.Serializer{IOStream})
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:839
  [7] deserialize_datatype(s::Serialization.Serializer{IOStream}, full::Bool)
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:1508
  [8] handle_deserialize(s::Serialization.Serializer{IOStream}, b::Int32)
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:892
  [9] deserialize(s::Serialization.Serializer{IOStream})
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:839
 [10] handle_deserialize(s::Serialization.Serializer{IOStream}, b::Int32)
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:899
 [11] deserialize(s::Serialization.Serializer{IOStream})
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:839
 [12] handle_deserialize(s::Serialization.Serializer{IOStream}, b::Int32)
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:945
 [13] deserialize
    @ ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:839 [inlined]
 [14] deserialize(s::IOStream)
    @ Serialization ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Serialization/src/Serialization.jl:826
 [15] (::Foo.var"#__init__##0#__init__##1")(io::IOStream)
    @ Foo ~/Projects/Foo/src/Foo.jl:12
 [16] open(::Foo.var"#__init__##0#__init__##1", ::String, ::Vararg{String}; kwargs::@Kwargs{})
    @ Base ./io.jl:410
 [17] open
    @ ./io.jl:407 [inlined]
 [18] __init__()
    @ Foo ~/Projects/Foo/src/Foo.jl:11
 [19] run_module_init(mod::Module, i::Int64)
    @ Base ./loading.jl:1419
 [20] register_restored_modules(sv::Core.SimpleVector, pkg::Base.PkgId, path::String)
    @ Base ./loading.jl:1407
 [21] _include_from_serialized(pkg::Base.PkgId, path::String, ocachepath::String, depmods::Vector{Any}, ignore_native::Nothing; register::Bool)
    @ Base ./loading.jl:1295
 [22] _include_from_serialized (repeats 2 times)
    @ ./loading.jl:1246 [inlined]
 [23] _require_search_from_serialized(pkg::Base.PkgId, sourcepath::String, build_id::UInt128, stalecheck::Bool; reasons::Dict{String, Int64}, DEPOT_PATH::Vector{String})
    @ Base ./loading.jl:2121
 [24] __require_prelocked(pkg::Base.PkgId, env::String)
    @ Base ./loading.jl:2621
 [25] _require_prelocked(uuidkey::Base.PkgId, env::String)
    @ Base ./loading.jl:2484
 [26] macro expansion
    @ ./loading.jl:2412 [inlined]
 [27] macro expansion
    @ ./lock.jl:294 [inlined]
 [28] __require(into::Module, mod::Symbol)
    @ Base ./loading.jl:2369
 [29] #invoke_in_world#2
    @ ./essentials.jl:1091 [inlined]
 [30] invoke_in_world
    @ ./essentials.jl:1087 [inlined]
 [31] require(into::Module, mod::Symbol)
    @ Base ./loading.jl:2361
 [32] top-level scope
    @ REPL[1]:1
during initialization of module Foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior packages Package management and loading regression 1.11 Regression in the 1.11 release stdlib Julia's standard library
Projects
None yet
Development

No branches or pull requests

2 participants