|
| 1 | +""" |
| 2 | + Aqua.test_persistent_tasks(package; tmax=5) |
| 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 | +
|
| 21 | +```julia |
| 22 | +module PkgA |
| 23 | +const t = Ref{Any}() # to prevent the Timer from being garbage-collected |
| 24 | +__init__() = t[] = Timer(0.1; interval=1) # create a persistent `Timer` `Task` |
| 25 | +end |
| 26 | +``` |
| 27 | +
|
| 28 | +`PkgA` will precompile successfully, because `PkgA.__init__()` does not |
| 29 | +run when `PkgA` is precompiled. However, |
| 30 | +
|
| 31 | +```julia |
| 32 | +module PkgB |
| 33 | +using PkgA |
| 34 | +end |
| 35 | +``` |
| 36 | +
|
| 37 | +fails to precompile: `using PkgA` runs `PkgA.__init__()`, which |
| 38 | +leaves the `Timer` `Task` running, and that causes precompilation |
| 39 | +of `PkgB` to hang. |
| 40 | +
|
| 41 | +# How the test works |
| 42 | +
|
| 43 | +This test works by launching a Julia process that tries to precompile a |
| 44 | +dummy package similar to `PkgB` above, modified to signal back to Aqua when |
| 45 | +`PkgA` has finished loading. The test fails if the gap between loading `PkgA` |
| 46 | +and finishing precompilation exceeds time `tmax`. |
| 47 | +
|
| 48 | +# How to fix failing packages |
| 49 | +
|
| 50 | +Often, the easiest fix is to modify the `__init__` function to check whether the |
| 51 | +Julia process is precompiling some other package; if so, don't launch the |
| 52 | +persistent `Task`s. |
| 53 | +
|
| 54 | +``` |
| 55 | +function __init__() |
| 56 | + # Other setup code here |
| 57 | + if ccall(:jl_generating_output, Cint, ()) == 0 # if we're not precompiling... |
| 58 | + # launch persistent tasks here |
| 59 | + end |
| 60 | +end |
| 61 | +``` |
| 62 | +
|
| 63 | +In more complex cases, you may need to set up independently-callable functions |
| 64 | +to launch the tasks and cleanly shut them down. |
| 65 | +
|
| 66 | +# Arguments |
| 67 | +- `package`: a top-level `Module` or `Base.PkgId`. |
| 68 | +
|
| 69 | +# Keyword Arguments |
| 70 | +- `tmax::Real`: the maximum time (in seconds) to wait between loading the |
| 71 | + package and forced shutdown of the precompilation process. |
| 72 | +""" |
| 73 | +function test_persistent_tasks(package::PkgId; tmax=5, fails::Bool=false) |
| 74 | + @testset "$package persistent_tasks" begin |
| 75 | + result = root_project_or_failed_lazytest(package) |
| 76 | + result isa LazyTestResult && return result |
| 77 | + @test fails ⊻ precompile_wrapper(result, tmax) |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +function test_persistent_tasks(package::Module; kwargs...) |
| 82 | + test_persistent_tasks(PkgId(package); kwargs...) |
| 83 | +end |
| 84 | + |
| 85 | +""" |
| 86 | + Aqua.test_persistent_tasks_deps(package; kwargs...) |
| 87 | +
|
| 88 | +Test all the dependencies of `package` with [`Aqua.test_persistent_tasks`](@ref). |
| 89 | +""" |
| 90 | +function test_persistent_tasks_deps(package::PkgId; fails=Dict{String,Bool}(), kwargs...) |
| 91 | + result = root_project_or_failed_lazytest(package) |
| 92 | + result isa LazyTestResult && return result |
| 93 | + prj = TOML.parsefile(result) |
| 94 | + deps = get(prj, "deps", nothing) |
| 95 | + @testset "$result" begin |
| 96 | + if deps === nothing |
| 97 | + return LazyTestResult("$package", "`$result` does not have `deps`", true) |
| 98 | + else |
| 99 | + for (name, uuid) in deps |
| 100 | + id = PkgId(UUID(uuid), name) |
| 101 | + test_persistent_tasks(id; fails=get(fails, name, false), kwargs...) |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | +end |
| 106 | + |
| 107 | +function test_persistent_tasks_deps(package::Module; kwargs...) |
| 108 | + test_persistent_tasks_deps(PkgId(package); kwargs...) |
| 109 | +end |
| 110 | + |
| 111 | +function precompile_wrapper(project, tmax) |
| 112 | + pkgdir = dirname(project) |
| 113 | + pkgname = basename(pkgdir) |
| 114 | + wrapperdir = tempname() |
| 115 | + wrappername, wrapperuuid = only(Pkg.generate(wrapperdir)) |
| 116 | + Pkg.activate(wrapperdir) |
| 117 | + Pkg.develop(PackageSpec(path=pkgdir)) |
| 118 | + open(joinpath(wrapperdir, "src", wrappername * ".jl"), "w") do io |
| 119 | + println(io, """ |
| 120 | + module $wrappername |
| 121 | + using $pkgname |
| 122 | + # Signal Aqua from the precompilation process that we've finished loading the package |
| 123 | + open(joinpath("$wrapperdir", "done.log"), "w") do io |
| 124 | + println(io, "done") |
| 125 | + end |
| 126 | + end |
| 127 | + """) |
| 128 | + end |
| 129 | + # Precompile the wrapper package |
| 130 | + cmd = `$(Base.julia_cmd()) --project=$wrapperdir -e 'using Pkg; Pkg.precompile()'` |
| 131 | + proc = run(cmd; wait=false) |
| 132 | + while !isfile(joinpath(wrapperdir, "done.log")) |
| 133 | + sleep(0.1) |
| 134 | + end |
| 135 | + # Check whether precompilation finishes in the required time |
| 136 | + t = time() |
| 137 | + while process_running(proc) && time() - t < tmax |
| 138 | + sleep(0.1) |
| 139 | + end |
| 140 | + success = !process_running(proc) |
| 141 | + if !success |
| 142 | + kill(proc) |
| 143 | + end |
| 144 | + return success |
| 145 | +end |
0 commit comments