Skip to content

Commit 0dd2d5e

Browse files
committed
fixup! Add a watcher mechanism to detect when Distributed might be in use
1 parent cc9a0e6 commit 0dd2d5e

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/DistributedNext.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,22 @@ export
7272
# Used only by shared arrays.
7373
check_same_host
7474

75-
distributed_module::Union{Module, Nothing} = nothing
76-
77-
function _find_distributed_stdlib()
75+
function _check_distributed_active()
7876
# Find the Distributed module if it's been loaded
7977
distributed_pkgid = Base.PkgId(Base.UUID("8ba89e20-285c-5b6f-9357-94700520ee1b"), "Distributed")
80-
global distributed_module = get(Base.loaded_modules, distributed_pkgid, nothing)
78+
if !haskey(Base.loaded_modules, distributed_pkgid)
79+
return false
80+
end
81+
82+
if isdefined(Base.loaded_modules[distributed_pkgid].LPROC, :cookie) && inited[]
83+
@warn "DistributedNext has detected that the Distributed stdlib may be in use. Be aware that these libraries are not compatible, you should use either one or the other."
84+
return true
85+
else
86+
return false
87+
end
8188
end
8289

8390
function _require_callback(mod::Base.PkgId)
84-
_find_distributed_stdlib()
85-
8691
if Base.toplevel_load[] && myid() == 1 && nprocs() > 1
8792
# broadcast top-level (e.g. from Main) import/using from node 1 (only)
8893
@sync for p in procs()
@@ -132,16 +137,12 @@ function __init__()
132137
# cluster cookie has been set, which is most likely to have been done
133138
# through Distributed.init_multi() being called by Distributed.addprocs() or
134139
# something.
135-
_find_distributed_stdlib()
136140
watcher_task = Threads.@spawn while true
137-
if !isnothing(distributed_module)
138-
if isdefined(distributed_module.LPROC, :cookie) && inited[]
139-
@warn "DistributedNext has detected that the Distributed stdlib may be in use. Be aware that these libraries are not compatible, you should use either one or the other."
140-
return
141-
end
141+
if _check_distributed_active()
142+
return
142143
end
143144

144-
sleep(0.2)
145+
sleep(1)
145146
end
146147
errormonitor(watcher_task)
147148
end

test/distributed_stdlib_detection.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ using DistributedNext
22

33
@testset "Distributed.jl detection" begin
44
# Just loading Distributed should do nothing
5-
cmd = `$test_exename $test_exeflags -e 'using Distributed, DistributedNext; sleep(2)'`
5+
cmd = `$test_exename $test_exeflags -e 'using Distributed, DistributedNext; @assert !DistributedNext._check_distributed_active()'`
66
stderr_buf = IOBuffer()
77
run(pipeline(cmd; stderr=stderr_buf))
88
stderr_str = String(take!(stderr_buf))
99
@test isempty(stderr_str)
1010

1111
# Only one of the two being active should also do nothing
12-
cmd = `$test_exename $test_exeflags -e 'using Distributed, DistributedNext; Distributed.init_multi(); sleep(2)'`
12+
cmd = `$test_exename $test_exeflags -e 'using Distributed, DistributedNext; Distributed.init_multi(); @assert !DistributedNext._check_distributed_active()'`
1313
stderr_buf = IOBuffer()
1414
run(pipeline(cmd; stderr=stderr_buf))
1515
stderr_str = String(take!(stderr_buf))
1616
@test isempty(stderr_str)
1717

1818
# But both being active at the same time should trigger a warning
19-
cmd = `$test_exename $test_exeflags -e 'using Distributed, DistributedNext; Distributed.init_multi(); DistributedNext.init_multi(); sleep(2)'`
19+
cmd = `$test_exename $test_exeflags -e 'using Distributed, DistributedNext; Distributed.init_multi(); DistributedNext.init_multi(); @assert DistributedNext._check_distributed_active()'`
2020
stderr_buf = IOBuffer()
2121
run(pipeline(cmd; stderr=stderr_buf))
2222
stderr_str = String(take!(stderr_buf))

0 commit comments

Comments
 (0)