From 4b24a3cf273f1602a76f6d2355891c7a875da04a Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Tue, 1 Dec 2026 08:15:58 -0700 Subject: [PATCH] Start test workers with eager peer connections Julia 1.11 stdlib Distributed can wait on a thread-affine lazy peer condition when the first worker-to-worker RPC comes from another thread. Use the supported lazy=false setting through one test worker helper. Every active test addition must match that setting because both transports reject mixing lazy and eager workers in one cluster. Production and benchmark defaults stay unchanged. Reuse the existing four-worker Context fixture for a first-peer RPC from thread 2 before loading Dagger on those workers, and always remove them on failure. Native Julia 1.11.9 reproduces the old lazy launcher's exact lock-must-be-held error; eager stdlib and DistributedNext 1.3.2 each pass all five public first-peer checks without Dagger or a GPU loaded. Written by an AI agent on jpsamaroo's review feedback. --- AGENTS.md | 10 ++++++++++ test/fault-tolerance.jl | 8 ++++---- test/processors.jl | 27 ++++++++++++++++++--------- test/runtests.jl | 3 ++- test/scheduler.jl | 2 +- test/scopes.jl | 2 +- test/worker_setup.jl | 25 +++++++++++++++++++++++++ 7 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 test/worker_setup.jl diff --git a/AGENTS.md b/AGENTS.md index ad8c5740e..3c27ee8eb 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1217,3 +1217,13 @@ this section verbatim). Each entry is one bullet starting with `- `. the protocol alone is not enough for them. Test with a remote reader *and* a remote writer, in both orders: in one process every slot is the argument itself, so none of this is visible. + +109. **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 bd2507e59..20e1eaf5b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -242,12 +242,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