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

cluster - worker shutdown - use WNOHANG with nil return tests #1741

Merged
merged 2 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ rvm:
- 2.2.10
- 2.3.8
- 2.4.5
- 2.5.3
- 2.6
- 2.5.5
- 2.6.2
- ruby-head

matrix:
Expand All @@ -35,13 +35,12 @@ matrix:
env: RUBYOPT="--jit"
- rvm: 2.3.8
os: osx
- rvm: 2.5.3
- rvm: 2.5.5
os: osx
- rvm: jruby-9.2.6.0
- rvm: jruby-head

allow_failures:
- rvm: 2.6
- rvm: ruby-head
- rvm: ruby-head
env: RUBYOPT="--jit"
Expand Down
20 changes: 19 additions & 1 deletion lib/puma/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,25 @@ def stop_workers
@workers.each { |x| x.term }

begin
@workers.each { |w| Process.waitpid(w.pid) }
if RUBY_VERSION < '2.6'
@workers.each { |w| Process.waitpid(w.pid) }
else
# below code is for a bug in Ruby 2.6+, above waitpid call hangs
t_st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
pids = @workers.map(&:pid)
loop do
pids.reject! do |w_pid|
if Process.waitpid(w_pid, Process::WNOHANG)
log " worker status: #{$?}"
true
end
end
break if pids.empty?
sleep 0.5
end
t_end = Process.clock_gettime(Process::CLOCK_MONOTONIC)
log format(" worker shutdown time: %6.2f", t_end - t_st)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

There are two cycles instead of one.

You can use redo.

See #1738 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

@AlexWayfer

Thanks. As mentioned, I did it this way so it would loop thru all the workers on first pass, then go back for the nil returns. I'd rather use Process.waitpid(-1, Process::WNOHANG), as that's a bit cleaner, but it seems to repeatedly fail on trunk JIT, I never tried with 2.6.x JIT...

Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned, I did it this way so it would loop thru all the workers on first pass, then go back for the nil returns.

It's strange algorithm.

Option 1, with redo:

  1. Go through all workers
  2. Sleep and redo on the first fail (wait every first fail)

Option 2:

  1. Go through all workers and attempt to stop them
  2. Delete from array in success, wait after all if any failed
  3. Retry if there are any remained

Option 3, your choice, as I see:

  1. Go through all workers
  2. Delete in success, and retry (go further)
  3. Don't do anything in fail, just sleep in the end
  4. Retry until there are any

I think, the first option is simplest (but may be longest), the second is most optimal, and the third is strange.

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed the logic to use reject!

Copy link
Contributor

Choose a reason for hiding this comment

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

I changed the logic to use reject!

You can use loop do instead of while true do. 😉

Copy link
Member Author

Choose a reason for hiding this comment

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

I often look here:
https://msp-greg.github.io/ruby_trunk/file.control_expressions.html

loop do isn't mentioned there. Time to review kernel methods again...

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, about two cycles (and map): do we need for @workers after successful run of this code?

If no — we can use @workers.reject!.

rescue Interrupt
log "! Cancelled waiting for workers"
end
Expand Down