-
Notifications
You must be signed in to change notification settings - Fork 169
Gracefully fail claimed executions even if the supervisor process was pruned #559
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
jeremy
wants to merge
1
commit into
rails:main
Choose a base branch
from
jeremy:supervisor-sleep-wake-in-dev
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.
+136
−5
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
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class ProcessRecoveryTest < ActiveSupport::TestCase | ||
self.use_transactional_tests = false | ||
|
||
setup do | ||
@pid = nil | ||
JobResult.delete_all | ||
end | ||
|
||
teardown do | ||
terminate_process(@pid) if @pid | ||
JobResult.delete_all | ||
end | ||
|
||
test "supervisor handles missing process record and fails claimed executions properly" do | ||
# Start a supervisor with one worker | ||
@pid = run_supervisor_as_fork(workers: [ { queues: "*", polling_interval: 0.1, processes: 1 } ]) | ||
wait_for_registered_processes(2, timeout: 1.second) # Supervisor + 1 worker | ||
|
||
supervisor_process = SolidQueue::Process.find_by(kind: "Supervisor", pid: @pid) | ||
assert supervisor_process | ||
|
||
worker_process = SolidQueue::Process.find_by(kind: "Worker") | ||
assert worker_process | ||
|
||
# Enqueue a job and manually claim it for the worker to avoid timing races | ||
job = enqueue_store_result_job(42) | ||
claimed_execution = SolidQueue::ReadyExecution.claim("*", 5, worker_process.id).first | ||
assert claimed_execution.present? | ||
assert_equal worker_process.id, claimed_execution.process_id | ||
|
||
# Simulate supervisor process record disappearing | ||
supervisor_process.delete | ||
assert_nil SolidQueue::Process.find_by(id: supervisor_process.id) | ||
|
||
# Terminate the worker process | ||
worker_pid = worker_process.pid | ||
terminate_process(worker_pid, signal: :KILL) | ||
|
||
|
||
# Wait for the supervisor to reap the worker and fail the job | ||
wait_for_failed_executions(1, timeout: 5.seconds) | ||
|
||
# Assert the execution is failed | ||
failed_execution = SolidQueue::FailedExecution.last | ||
assert failed_execution.present? | ||
assert_equal "SolidQueue::Processes::ProcessExitError", failed_execution.exception_class | ||
|
||
# Ensure supervisor replaces the worker (even though its own record was missing) | ||
wait_for_registered_processes(2, timeout: 5.seconds) | ||
assert_operator SolidQueue::Process.where(kind: "Worker").count, :>=, 1 | ||
end | ||
|
||
private | ||
def assert_registered_workers_for(*queues, supervisor_pid: nil) | ||
workers = find_processes_registered_as("Worker") | ||
registered_queues = workers.map { |process| process.metadata["queues"] }.compact | ||
assert_equal queues.map(&:to_s).sort, registered_queues.sort | ||
if supervisor_pid | ||
assert_equal [ supervisor_pid ], workers.map { |process| process.supervisor.pid }.uniq | ||
end | ||
end | ||
|
||
def enqueue_store_result_job(value, queue_name = :default, **options) | ||
StoreResultJob.set(queue: queue_name).perform_later(value, **options) | ||
end | ||
|
||
def assert_no_claimed_jobs | ||
skip_active_record_query_cache do | ||
assert_empty SolidQueue::ClaimedExecution.all | ||
end | ||
end | ||
|
||
def wait_for_claimed_executions(count, timeout: 1.second) | ||
wait_for(timeout: timeout) { SolidQueue::ClaimedExecution.count == count } | ||
end | ||
|
||
def wait_for_failed_executions(count, timeout: 1.second) | ||
wait_for(timeout: timeout) { SolidQueue::FailedExecution.count == count } | ||
end | ||
end | ||
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 |
---|---|---|
|
@@ -185,6 +185,33 @@ class SupervisorTest < ActiveSupport::TestCase | |
end | ||
end | ||
|
||
# Regression test for supervisor failing to handle claimed jobs when its own | ||
# process record has been pruned (NoMethodError in #handle_claimed_jobs_by). | ||
test "handle_claimed_jobs_by fails claimed executions even if supervisor record is missing" do | ||
worker_name = "worker-test-#{SecureRandom.hex(4)}" | ||
|
||
worker_process = SolidQueue::Process.register(kind: "Worker", pid: 999_999, name: worker_name) | ||
|
||
job = StoreResultJob.perform_later(42) | ||
claimed_execution = SolidQueue::ReadyExecution.claim("*", 1, worker_process.id).first | ||
|
||
terminated_fork = Struct.new(:name).new(worker_name) | ||
|
||
DummyStatus = Struct.new(:pid, :exitstatus) do | ||
def signaled? = false | ||
def termsig = nil | ||
end | ||
status = DummyStatus.new(worker_process.pid, 1) | ||
|
||
supervisor = SolidQueue::Supervisor.allocate | ||
|
||
supervisor.send(:handle_claimed_jobs_by, terminated_fork, status) | ||
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 test is terribly synthetic, I'm afraid. Just explicitly constructs the situation that's encountered in the wild (nil Supervisor#process) rather than realistically (actually pruning the process). |
||
|
||
failed = SolidQueue::FailedExecution.find_by(job_id: claimed_execution.job_id) | ||
assert failed.present? | ||
assert_equal "SolidQueue::Processes::ProcessExitError", failed.exception_class | ||
end | ||
|
||
private | ||
def assert_registered_workers(supervisor_pid: nil, count: 1) | ||
assert_registered_processes(kind: "Worker", count: count, supervisor_pid: supervisor_pid) | ||
|
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 test isn't strictly necessary. I was using it to guide debugging and understanding.