Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixup to Gentle Close PR: Wait until the worker closes itself, before force-closing it in response to the closed pipes. #155

Open
wants to merge 2 commits into
base: npr-gentle-close-workers
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/workers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ mutable struct Worker
process_watch::Task
futures::Dict{UInt64, Future} # Request.id -> Future
@atomic terminated::Bool
gracefully_closed::Bool
end

# used to close Future.value channels when a worker terminates
Expand All @@ -81,6 +82,16 @@ function terminate!(w::Worker, from::Symbol=:manual)
end
empty!(w.futures)
end
# Give some time for the worker to exit gracefully.
if w.gracefully_closed
@static if !Sys.isapple()
# NOTE: THIS CAUSES A HANG ON MACOS! We can remove this if-check once
# the julia issue is resolved:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link to Julia issue #?

sleep(5)
else
wait(w.process)
end
end
signal = Base.SIGTERM
while !process_exited(w.process)
@debug "sending signal $signal to worker $(w.pid)"
Expand Down Expand Up @@ -116,6 +127,7 @@ function Base.close(w::Worker, from::Symbol=:manual)
flush(w.socket)
end
end
w.gracefully_closed = true
wait(w)
return
end
Expand Down Expand Up @@ -173,7 +185,7 @@ function Worker(;
return Sockets.connect(parse(Int, split(port_str, ':')[2]))
end
# create worker
w = Worker(ReentrantLock(), pid, proc, sock, Task(nothing), Task(nothing), Task(nothing), Dict{UInt64, Future}(), false)
w = Worker(ReentrantLock(), pid, proc, sock, Task(nothing), Task(nothing), Task(nothing), Dict{UInt64, Future}(), false, false)
## start a task to watch for worker process termination, notify the event when the task starts
e1 = Threads.Event()
w.process_watch = Threads.@spawn watch_and_terminate!(w, $e1)
Expand Down
3 changes: 2 additions & 1 deletion test/workers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using Test
@testset "clean shutdown ($w)" begin
close(w)
@test !process_running(w.process)
@test w.process.termsignal == Base.SIGTERM
@test w.process.termsignal == 0
@test w.process.exitcode == 0
@test !isopen(w.socket)
@test w.terminated
Expand All @@ -41,6 +41,7 @@ using Test

w = Worker()
@testset "remote_eval/remote_fetch ($w)" begin
@info "starting testset remote_eval/remote_fetch ($w)"
expr = quote
global x
x = 101
Expand Down
Loading