diff --git a/AGENTS.md b/AGENTS.md index 65af69798..5d55f69c8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1253,3 +1253,13 @@ this section verbatim). Each entry is one bullet starting with `- `. A launcher can therefore be instrumented while the extension exercised by every rank reports zero hits. Forward the enabled mode/path and merge the PID-qualified rank reports. + +113. **Threaded test RPCs need ready peer connections.** Julia 1.11's stdlib + `Distributed` creates its lazy peer condition on the worker's initial + thread; the first peer RPC from another thread can throw `lock must be + held`. Start test workers with the supported `lazy=false` option and + exercise their first peer RPC before other tests warm it. This configures + the fixture; the cold lazy path remains an upstream stdlib issue, and + production transport defaults must stay unchanged. Every test worker addition + must use the same setting: both transports reject mixing lazy and eager + workers in one cluster. diff --git a/test/fault-tolerance.jl b/test/fault-tolerance.jl index 160e3efb1..634de1ddc 100644 --- a/test/fault-tolerance.jl +++ b/test/fault-tolerance.jl @@ -35,7 +35,7 @@ d = delayed(kill_func)(b, c) @test collect(d) == 6 - addprocs(2) + add_test_workers(Distributed, 2) using Dagger setup_funcs(workers() |> last) @@ -44,7 +44,7 @@ c = delayed(kill_func)(a, b) @test collect(c) == 6 - addprocs(2) + add_test_workers(Distributed, 2) using Dagger setup_funcs(workers() |> last) @@ -56,7 +56,7 @@ c = delayed(kill_func)(b1, b2) @test collect(c) == 8 - addprocs(2) + add_test_workers(Distributed, 2) using Dagger setup_funcs(workers() |> last) @@ -66,7 +66,7 @@ d = delayed(kill_func)(b, c) @test_throws AssertionError collect(d; options=Dagger.Sch.SchedulerOptions(single=first(workers()))) - addprocs(1) + add_test_workers(Distributed, 1) using Dagger setup_funcs(workers() |> last) end diff --git a/test/processors.jl b/test/processors.jl index 3d0564be6..5ffe0e83c 100644 --- a/test/processors.jl +++ b/test/processors.jl @@ -70,18 +70,27 @@ end end @testset "Modify workers in Context" begin - ps = addprocs(4, exeflags=`--project=$(Base.active_project())`) - @everywhere ps using Dagger + ps = add_test_workers(Distributed, 4; threads=2) + try + # The first worker-to-worker RPC must be safe from another thread, + # before Dagger loading or an earlier call can warm that connection. + @everywhere ps include($(joinpath(@__DIR__, "worker_setup.jl"))) + first_peer = remotecall_fetch(threaded_test_peer, ps[1], Distributed, ps[2]) + @test first_peer.value == 7 + @test first_peer.rpc_tid == last(first_peer.default_tids) + @test first_peer.rpc_tid != first_peer.entry_tid + @everywhere ps using Dagger - ctx = Context(ps[1:2]) + ctx = Context(ps[1:2]) - Dagger.addprocs!(ctx, ps[3:end]) - @test map(p -> p.pid, procs(ctx)) == ps + Dagger.addprocs!(ctx, ps[3:end]) + @test map(p -> p.pid, procs(ctx)) == ps - Dagger.rmprocs!(ctx, ps[3:end]) - @test map(p -> p.pid, procs(ctx)) == ps[1:2] - - wait(rmprocs(ps)) + Dagger.rmprocs!(ctx, ps[3:end]) + @test map(p -> p.pid, procs(ctx)) == ps[1:2] + finally + wait(rmprocs(ps)) + end end @testset "Callable as Thunk function" begin diff --git a/test/runtests.jl b/test/runtests.jl index ec05f23b5..b00ee914a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -243,12 +243,13 @@ end using Dagger const Distributed = parentmodule(Dagger.myid) using .Distributed +include("worker_setup.jl") if additional_workers > 0 # We put this inside a branch because addprocs() takes a minimum of 1s to # complete even if doing nothing, which is annoying. # Pkg.test runs in a temporary project containing the test-only backends. # Workers must use that same resolution, rather than the repository project. - addprocs(additional_workers; exeflags=`--project=$(Base.active_project()) -t $worker_threads`) + add_test_workers(Distributed, additional_workers; threads=worker_threads) end include("imports.jl") diff --git a/test/scheduler.jl b/test/scheduler.jl index 4d7637f77..5511904e2 100644 --- a/test/scheduler.jl +++ b/test/scheduler.jl @@ -312,7 +312,7 @@ end @testset "Remove all workers throws" begin ps = [] try - ps1 = addprocs(2, exeflags=`--project=$(Base.active_project())`) + ps1 = add_test_workers(Distributed, 2) append!(ps, ps1) @everywhere vcat(ps1, myid()) $setup diff --git a/test/scopes.jl b/test/scopes.jl index c448bf2ca..95f1128a5 100644 --- a/test/scopes.jl +++ b/test/scopes.jl @@ -1,5 +1,5 @@ @testset "Chunk Scopes" begin - wid1, wid2 = addprocs(2, exeflags=["-t2", "--project=$(Base.active_project())"]) + wid1, wid2 = add_test_workers(Distributed, 2; threads=2) @everywhere [wid1,wid2] using Dagger Dagger.addprocs!(Dagger.Sch.eager_context(), [wid1,wid2]) fetch(Dagger.@spawn 1+1) # Force scheduler to pick up new workers diff --git a/test/worker_setup.jl b/test/worker_setup.jl new file mode 100644 index 000000000..71157237e --- /dev/null +++ b/test/worker_setup.jl @@ -0,0 +1,25 @@ +# Test workers must have their peer connections before a test issues RPCs from +# another thread. Julia 1.11's stdlib lazy connection condition is thread-affine; +# eager setup avoids that cold wait without changing production defaults. +function add_test_workers(transport::Module, count::Integer; + project=Base.active_project(), threads::Integer=1) + return transport.addprocs(count; lazy=false, + exeflags=`--project=$project -t $threads`) +end + +# Invoke the first peer RPC on a different thread from the worker's RPC entry +# task. This uses only public transport/thread APIs and does not load Dagger. +function threaded_test_peer(transport::Module, target::Integer) + @assert Threads.nthreads() >= 2 + entry_tid = Threads.threadid() + rpc_tid = Ref(0) + value = Ref(0) + Threads.@threads :static for i in 1:Threads.nthreads() + if i == Threads.nthreads() + rpc_tid[] = Threads.threadid() + value[] = transport.remotecall_fetch(identity, target, 7) + end + end + return (; entry_tid, rpc_tid=rpc_tid[], value=value[], + default_tids=collect(Threads.threadpooltids(:default))) +end