|
| 1 | +""" |
| 2 | + Aqua.test_persistent_tasks(package) |
| 3 | +
|
| 4 | +Test whether loading `package` creates persistent `Task`s |
| 5 | +which may block precompilation of dependent packages. |
| 6 | +
|
| 7 | +# Motivation |
| 8 | +
|
| 9 | +Julia 1.10 and higher wait for all running `Task`s to finish |
| 10 | +before writing out the precompiled (cached) version of the package. |
| 11 | +One consequence is that a package that launches |
| 12 | +`Task`s in its `__init__` function may precompile successfully, |
| 13 | +but block precompilation of any packages that depend on it. |
| 14 | +
|
| 15 | +# Example |
| 16 | +
|
| 17 | +Let's create a dummy package, `PkgA`, that launches a persistent `Task`: |
| 18 | +
|
| 19 | +```julia |
| 20 | +module PkgA |
| 21 | +const t = Ref{Any}() # to prevent the Timer from being garbage-collected |
| 22 | +__init__() = t[] = Timer(0.1; interval=1) # create a persistent `Timer` `Task` |
| 23 | +end |
| 24 | +``` |
| 25 | +
|
| 26 | +`PkgA` will precompile successfully, because `PkgA.__init__()` does not |
| 27 | +run when `PkgA` is precompiled. However, |
| 28 | +
|
| 29 | +```julia |
| 30 | +module PkgB |
| 31 | +using PkgA |
| 32 | +end |
| 33 | +``` |
| 34 | +
|
| 35 | +fails to precompile: `using PkgA` runs `PkgA.__init__()`, which |
| 36 | +leaves the `Timer` `Task` running, and that causes precompilation |
| 37 | +of `PkgB` to hang. |
| 38 | +
|
| 39 | +# How the test works |
| 40 | +
|
| 41 | +This test works by launching a Julia process that tries to precompile a |
| 42 | +dummy package similar to `PkgB` above, modified to signal back to Aqua when |
| 43 | +`PkgA` has finished loading. The test fails if the gap between loading `PkgA` |
| 44 | +and finishing precompilation exceeds time `tmax`. |
| 45 | +
|
| 46 | +# How to fix failing packages |
| 47 | +
|
| 48 | +Often, the easiest fix is to modify the `__init__` function to check whether the |
| 49 | +Julia process is precompiling some other package; if so, don't launch the |
| 50 | +persistent `Task`s. |
| 51 | +
|
| 52 | +``` |
| 53 | +function __init__() |
| 54 | + # Other setup code here |
| 55 | + if ccall(:jl_generating_output, Cint, ()) == 0 # if we're not precompiling... |
| 56 | + # launch persistent tasks here |
| 57 | + end |
| 58 | +end |
| 59 | +``` |
| 60 | +
|
| 61 | +In more complex cases, you may need to set up independently-callable functions |
| 62 | +to launch the tasks and set conditions that allow them to cleanly exit. |
| 63 | +
|
| 64 | +# Arguments |
| 65 | +- `package`: a top-level `Module` or `Base.PkgId`. |
| 66 | +
|
| 67 | +# Keyword Arguments |
| 68 | +- `broken::Bool = false`: If true, it uses `@test_broken` instead of |
| 69 | + `@test`. |
| 70 | +- `tmax::Real = 5`: the maximum time (in seconds) to wait after loading the |
| 71 | + package before forcibly shutting down the precompilation process (triggering |
| 72 | + a test failure). |
| 73 | +""" |
| 74 | +function test_persistent_tasks(package::PkgId; broken::Bool = false, kwargs...) |
| 75 | + if broken |
| 76 | + @test_broken !has_persistent_tasks(package; kwargs...) |
| 77 | + else |
| 78 | + @test !has_persistent_tasks(package; kwargs...) |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +function test_persistent_tasks(package::Module; kwargs...) |
| 83 | + test_persistent_tasks(PkgId(package); kwargs...) |
| 84 | +end |
| 85 | + |
| 86 | +function has_persistent_tasks(package::PkgId; tmax = 10) |
| 87 | + result = root_project_or_failed_lazytest(package) |
| 88 | + result isa LazyTestResult && error("Unable to locate Project.toml") |
| 89 | + return !precompile_wrapper(result, tmax) |
| 90 | +end |
| 91 | + |
| 92 | +""" |
| 93 | + Aqua.find_persistent_tasks_deps(package; broken = Dict{String,Bool}(), kwargs...) |
| 94 | +
|
| 95 | +Test all the dependencies of `package` with [`Aqua.test_persistent_tasks`](@ref). |
| 96 | +On Julia 1.10 and higher, it returns a list of all dependencies failing the test. |
| 97 | +These are likely the ones blocking precompilation of your package. |
| 98 | +
|
| 99 | +Any additional kwargs (e.g., `tmax`) are passed to [`Aqua.test_persistent_tasks`](@ref). |
| 100 | +""" |
| 101 | +function find_persistent_tasks_deps(package::PkgId; kwargs...) |
| 102 | + result = root_project_or_failed_lazytest(package) |
| 103 | + result isa LazyTestResult && error("Unable to locate Project.toml") |
| 104 | + prj = TOML.parsefile(result) |
| 105 | + deps = get(prj, "deps", Dict{String,Any}()) |
| 106 | + filter!(deps) do (name, uuid) |
| 107 | + id = PkgId(UUID(uuid), name) |
| 108 | + return has_persistent_tasks(id; kwargs...) |
| 109 | + end |
| 110 | + return [name for (name, _) in deps] |
| 111 | +end |
| 112 | + |
| 113 | +function find_persistent_tasks_deps(package::Module; kwargs...) |
| 114 | + find_persistent_tasks_deps(PkgId(package); kwargs...) |
| 115 | +end |
| 116 | + |
| 117 | +function precompile_wrapper(project, tmax) |
| 118 | + prev_project = Base.active_project() |
| 119 | + isdefined(Pkg, :respect_sysimage_versions) && Pkg.respect_sysimage_versions(false) |
| 120 | + try |
| 121 | + pkgdir = dirname(project) |
| 122 | + pkgname = get(TOML.parsefile(project), "name", nothing) |
| 123 | + if isnothing(pkgname) |
| 124 | + @error "Unable to locate package name in $project" |
| 125 | + return false |
| 126 | + end |
| 127 | + wrapperdir = tempname() |
| 128 | + wrappername, _ = only(Pkg.generate(wrapperdir)) |
| 129 | + Pkg.activate(wrapperdir) |
| 130 | + Pkg.develop(PackageSpec(path = pkgdir)) |
| 131 | + statusfile = joinpath(wrapperdir, "done.log") |
| 132 | + open(joinpath(wrapperdir, "src", wrappername * ".jl"), "w") do io |
| 133 | + println( |
| 134 | + io, |
| 135 | + """ |
| 136 | +module $wrappername |
| 137 | +using $pkgname |
| 138 | +# Signal Aqua from the precompilation process that we've finished loading the package |
| 139 | +open("$(escape_string(statusfile))", "w") do io |
| 140 | + println(io, "done") |
| 141 | + flush(io) |
| 142 | +end |
| 143 | +end |
| 144 | +""", |
| 145 | + ) |
| 146 | + end |
| 147 | + # Precompile the wrapper package |
| 148 | + cmd = `$(Base.julia_cmd()) --project=$wrapperdir -e 'using Pkg; Pkg.precompile()'` |
| 149 | + proc = run(cmd; wait = false) |
| 150 | + while !isfile(statusfile) && process_running(proc) |
| 151 | + sleep(0.5) |
| 152 | + end |
| 153 | + if !isfile(statusfile) |
| 154 | + @error "Unexpected error: $statusfile was not created, but precompilation exited" |
| 155 | + return false |
| 156 | + end |
| 157 | + # Check whether precompilation finishes in the required time |
| 158 | + t = time() |
| 159 | + while process_running(proc) && time() - t < tmax |
| 160 | + sleep(0.1) |
| 161 | + end |
| 162 | + success = !process_running(proc) |
| 163 | + if !success |
| 164 | + kill(proc) |
| 165 | + end |
| 166 | + return success |
| 167 | + finally |
| 168 | + isdefined(Pkg, :respect_sysimage_versions) && Pkg.respect_sysimage_versions(true) |
| 169 | + Pkg.activate(prev_project) |
| 170 | + end |
| 171 | +end |
0 commit comments