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

fix deadlock in dispatch_loop #496

Closed
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
37 changes: 17 additions & 20 deletions lib/bootsnap/cli/worker_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@ def initialize(jobs)
@pid = nil
end

def write(message, block: true)
def write(message)
payload = Marshal.dump(message)
if block
to_io.write(payload)
true
else
to_io.write_nonblock(payload, exception: false) != :wait_writable
end
to_io.write_nonblock(payload)
end

def close
Expand All @@ -74,7 +69,7 @@ def spawn
@pid = Process.fork do
to_io.close
work_loop
exit!(0)
exit!(true)
end
@pipe_out.close
true
Expand All @@ -84,7 +79,7 @@ def spawn
def initialize(size:, jobs: {})
@size = size
@jobs = jobs
@queue = Queue.new
@queue = Thread::Queue.new
@pids = []
end

Expand All @@ -98,25 +93,27 @@ def spawn

def dispatch_loop
loop do
case job = @queue.pop
when nil
job = @queue.pop
if job
IO.select(nil, @workers).tap do |(_nil, available)|
available.sample.write(job)
end
else
closed = []
@workers.each do |worker|
worker.write([:exit])
worker.close
closed << worker
rescue IO::WaitWritable
next
end
return true
else
unless @workers.sample.write(job, block: false)
free_worker.write(job)
end
@workers.delete_if(&closed.method(:include?))
return if @workers.empty?
IO.select(nil, @workers)
end
end
end

def free_worker
IO.select(nil, @workers)[1].sample
end

def push(*args)
@queue.push(args)
nil
Expand Down
Loading