-
Notifications
You must be signed in to change notification settings - Fork 247
Honour TERM received during supervisor boot before forking workers #770
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
Open
rafael-pissardo
wants to merge
1
commit into
rails:main
Choose a base branch
from
rafael-pissardo:fix/755-term-during-supervisor-boot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,8 @@ def start | |
| run_start_hooks | ||
|
|
||
| start_processes | ||
| return shutdown if stopped? | ||
|
|
||
| launch_maintenance_task | ||
|
|
||
| supervise | ||
|
|
@@ -65,7 +67,15 @@ def boot | |
| end | ||
|
|
||
| def start_processes | ||
| configuration.configured_processes.each { |configured_process| start_process(configured_process) } | ||
| configuration.configured_processes.each do |configured_process| | ||
| # Honour signals that arrived during boot / start hooks before forking | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would run for any supervisor even if it doesn't fork ( |
||
| # any children. Otherwise a queued TERM is only handled in #supervise, | ||
| # after workers have already been started (see #755). | ||
| process_signal_queue if standalone? | ||
| break if stopped? | ||
|
|
||
| start_process(configured_process) | ||
| end | ||
| end | ||
|
|
||
| def supervise | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| class SupervisorBootSignalTest < ActiveSupport::TestCase | ||
| self.use_transactional_tests = false | ||
|
|
||
| setup do | ||
| @marker_path = Rails.root.join("tmp/solid_queue_start_hook_#{SecureRandom.hex(8)}") | ||
| @release_path = Rails.root.join("tmp/solid_queue_release_start_hook_#{SecureRandom.hex(8)}") | ||
| end | ||
|
|
||
| teardown do | ||
| File.delete(@marker_path) if File.exist?(@marker_path) | ||
| File.delete(@release_path) if File.exist?(@release_path) | ||
| end | ||
|
|
||
| # Regression for https://github.com/rails/solid_queue/issues/755: | ||
| # TERM received while the supervisor is still running start hooks used to sit | ||
| # in the signal queue until after workers were forked. Those workers then | ||
| # inherited the supervisor's enqueue-only trap and kept claiming jobs until | ||
| # SIGKILL. | ||
| test "TERM during supervisor start hooks exits without starting workers" do | ||
| resetting_hooks do | ||
| marker_path = @marker_path | ||
| release_path = @release_path | ||
|
|
||
| SolidQueue.on_start do | ||
| File.write(marker_path, Process.pid.to_s) | ||
| Timeout.timeout(5) { sleep 0.05 until File.exist?(release_path) } | ||
| end | ||
|
|
||
| SolidQueue.on_worker_start do | ||
| JobResult.create!(queue_name: "background", status: "hook_called", value: "worker_started") | ||
| end | ||
|
|
||
| 3.times { |i| StoreResultJob.set(queue: :background).perform_later("should_not_run_#{i}") } | ||
|
|
||
| pid = run_supervisor_as_fork( | ||
| workers: [ { queues: :background, threads: 1, polling_interval: 0.1 } ], | ||
| dispatchers: [] | ||
| ) | ||
|
|
||
| wait_while_with_timeout!(5.seconds) { !File.exist?(marker_path) } | ||
| Process.kill(:TERM, pid) | ||
| File.write(release_path, "1") | ||
|
|
||
| wait_for_process_termination_with_timeout(pid, timeout: SolidQueue.shutdown_timeout + 5.seconds) | ||
|
|
||
| skip_active_record_query_cache do | ||
| assert_equal 0, JobResult.where(value: "worker_started").count | ||
| assert_equal 0, JobResult.where(status: "completed").count | ||
| assert_equal 0, SolidQueue::ClaimedExecution.count | ||
| assert_equal 3, SolidQueue::ReadyExecution.count | ||
| assert SolidQueue::Process.none? | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private | ||
| def resetting_hooks | ||
| reset_hooks(SolidQueue::Supervisor) do | ||
| reset_hooks(SolidQueue::Worker) do | ||
| yield | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def reset_hooks(process) | ||
| exit_hooks = process.lifecycle_hooks[:exit] | ||
| start_hooks = process.lifecycle_hooks[:start] | ||
| stop_hooks = process.lifecycle_hooks[:stop] | ||
| process.lifecycle_hooks[:exit] = [] | ||
| process.lifecycle_hooks[:start] = [] | ||
| process.lifecycle_hooks[:stop] = [] | ||
| yield | ||
| ensure | ||
| process.lifecycle_hooks[:exit] = exit_hooks | ||
| process.lifecycle_hooks[:start] = start_hooks | ||
| process.lifecycle_hooks[:stop] = stop_hooks | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels redundant together with the same call to
register_signal_handlersright after running thebefore_bootcallbacks... (see#boot, which runs first thing as part ofblock.callwhen running as fork). I think one of those needs to go.