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 #3228] idle-timeout not working in cluster mode #3235

Merged

Conversation

joshuay03
Copy link
Contributor

@joshuay03 joshuay03 commented Sep 27, 2023

Description

Closes #3228.

Ensures that a worker kills the master process if its server shut down due the the idle timeout elapsing. The master will gracefully shut down the rest of the workers and then itself.

I wonder if we should backport this to https://github.com/puma/puma/tree/v6.4.0 πŸ€”

Stupid question πŸ€¦πŸ½β€β™‚οΈ

Your checklist for this pull request

  • I have reviewed the guidelines for contributing to this repository.
  • I have added (or updated) appropriate tests if this PR fixes a bug or adds a feature.
  • My pull request is 100 lines added/removed or less so that it can be easily reviewed.
  • If this PR doesn't need tests (docs change), I added [ci skip] to the title of the PR.
  • If this closes any issues, I have added "Closes #issue" to the PR description or my commit messages.
  • I have updated the documentation accordingly.
  • All new and existing tests passed, including Rubocop.

@joshuay03 joshuay03 force-pushed the idle-timeout-not-working-with-workers branch from 3afcab8 to cd57b82 Compare September 27, 2023 06:26
@@ -146,6 +146,8 @@ def run
# Invoke any worker shutdown hooks so they can prevent the worker
# exiting until any background operations are completed
@config.run_hooks(:before_worker_shutdown, index, @log_writer, @hook_data)

Process.kill "SIGTERM", master if server.idle_timeout_reached
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trapped here:

puma/lib/puma/cluster.rb

Lines 323 to 341 in 252890c

master_pid = Process.pid
Signal.trap "SIGTERM" do
# The worker installs their own SIGTERM when booted.
# Until then, this is run by the worker and the worker
# should just exit if they get it.
if Process.pid != master_pid
log "Early termination of worker"
exit! 0
else
@launcher.close_binder_listeners
stop_workers
stop
@events.fire_on_stopped!
raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
exit 0 # Clean exit, workers were stopped
end
end

@joshuay03 joshuay03 force-pushed the idle-timeout-not-working-with-workers branch from cd57b82 to 9d51bd3 Compare September 27, 2023 06:59
Comment on lines +233 to +235
assert_raises Errno::ECONNREFUSED, "Connection refused" do
connect
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before:
Screenshot 2023-09-27 at 4 27 24β€―pm

@joshuay03 joshuay03 force-pushed the idle-timeout-not-working-with-workers branch 2 times, most recently from 991d0de to a412009 Compare September 27, 2023 11:50
@joshuay03 joshuay03 force-pushed the idle-timeout-not-working-with-workers branch from a412009 to 53d73d8 Compare September 27, 2023 12:11
@nateberkopec
Copy link
Member

I wonder if we should backport this to https://github.com/puma/puma/tree/v6.4.0 πŸ€”

Sorry, what do you mean?

@joshuay03
Copy link
Contributor Author

I wonder if we should backport this to v6.4.0 πŸ€”

Sorry, what do you mean?

Sorry, I might be misunderstanding how releases work...

What I'm trying to say is, since the feature was included in https://github.com/puma/puma/releases/tag/v6.4.0, is there a way to include this patch in the same release? πŸ€”

@dentarg
Copy link
Member

dentarg commented Sep 28, 2023

We (Nate) will have to put out a patch release, 6.4.1.

@nateberkopec nateberkopec merged commit 8d1f77b into puma:master Oct 3, 2023
59 checks passed
@davidalejandroaguilar
Copy link

Thanks for the great and quick work on this! πŸ₯‡

@joshuay03 joshuay03 deleted the idle-timeout-not-working-with-workers branch October 5, 2023 21:07
@nateberkopec
Copy link
Member

See #3261

@bensheldon
Copy link

Sorry for the question here. Am I understanding that if any single worker hits the idle timeout, it shuts down everything?

I was trying this locally (bundle exec puma -C config/puma.rb -w 10 --idle-timeout 10) and it seems like if the server gets 1 request every 5 seconds (less than the 10 second idle timeout), it will still shutdown. That doesn't seem right.

It seems like the the Puma::Cluster should be checking across all of the workers and then terminating if idle.

@dentarg
Copy link
Member

dentarg commented Nov 26, 2023

That sounds like a bug to me indeed, and worthy of its own issue so it can be picked up! (Disclaimer: I haven't tested anything myself in regards to this feature)

@bensheldon
Copy link

I opened #3282

@joshuay03
Copy link
Contributor Author

joshuay03 commented Nov 26, 2023

Am I understanding that if any single worker hits the idle timeout, it shuts down everything?

Yes that is how this currently works. But,

It seems like the the Puma::Cluster should be checking across all of the workers and then terminating if idle.

this does make much more sense to me.

I may have jumped the gun on this one before clarifying the exact implementation based off of #3228 (comment) and #3228 (comment).

I'll tackle #3282 sometime today/tomorrow if no one's picked it up by then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

idle-timeout not shutting down when workers are set
5 participants