Puma plugin in async mode: jobs that finish while Puma stops go back to the queue
メンテナーはふだん 4 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 72/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
test/integration/puma/plugin_testing.rb にある Puma プラグインのライフサイクルと、issue で説明されている AsyncSupervisor のシャットダウンコールバックを読みます。提供された再現手順を TARGET_DB=sqlite bin/rails test test/integration/puma/plugin_stop_repro_test.rb で実行します。async モードで成功するはずです。issue では、回帰チェックとして test/integration/async_processes_lifecycle_test.rb と Puma プラグインのテストも挙げられています。
索引モデルが issue の本文から書いたものです。
説明
With solid_queue_mode :async, stopping Puma while a job runs lets the job finish. But the job isn't recorded as finished: it goes back to its queue and runs again, either on the next start or, sometimes, right away before the process exits. This happens even when the job finishes well within shutdown_timeout, so it isn't the timeout case that #815 handles.
Reproduction
On main (b9c646a, 1.7.2, which includes #815). This is a test in the style of the existing plugin tests (test/integration/puma/plugin_testing.rb):
# test/integration/puma/plugin_stop_repro_test.rb
require "test_helper"
require_relative "plugin_testing"
ENV["SOLID_QUEUE_SKIP_RECURRING"] = "true"
class PluginAsyncStopReproTest < ActiveSupport::TestCase
include PluginTesting
test "a job finishing within the shutdown timeout while puma stops is recorded as finished" do
active_job = StoreResultJob.perform_later(:stop, pause: 1.second)
job = SolidQueue::Job.find_by!(active_job_id: active_job.job_id)
wait_while_with_timeout(5.seconds) { skip_active_record_query_cache { job.reload.claimed_execution.nil? } }
terminate_process(@pid, signal: :TERM)
skip_active_record_query_cache do
assert_equal 1, JobResult.where(value: :stop, status: "completed").count
assert job.reload.finished?
assert_nil job.ready_execution
end
end
private
def solid_queue_mode = :async
end
TARGET_DB=sqlite bin/rails test test/integration/puma/plugin_stop_repro_test.rb fails in 5 of 5 runs, and the same with TARGET_DB=mysql and TARGET_DB=postgres. Each run ends one of two ways:
finished=0 ready=1: the job ran once but is back in the queue; or- two completed
JobResults: it ran twice before Puma exited.
The same test with solid_queue_mode :fork passes every time. The test dummy's shutdown_timeout is 2 seconds; the job takes 1.
Cause
When Puma stops, the plugin calls stop on the embedded AsyncSupervisor, whose run loop then runs the shutdown callbacks. after_shutdown callbacks run in the order they were defined:
Registrable's:deregistercomes first. Deregistering the supervisor also deregisters its supervisees, which releases the workers' claimed executions while their jobs are still running.AsyncSupervisor'safter_shutdown :terminate_gracefully, unless: :standalone?comes after it, and only then waits for the threads.
By the time a job finishes, its claimed execution is gone. So the job's finished transaction doesn't record it, and the job is already back in the ready list, where a worker thread that hasn't stopped polling yet can claim it again.
The standalone async supervisor (bin/jobs --mode async) isn't affected: on TERM or INT, its signal handling calls terminate_gracefully before shutdown.
Possible fix
Wait for the threads before deregistering. One line:
- after_shutdown :terminate_gracefully, unless: :standalone?
+ before_shutdown :terminate_gracefully, unless: :standalone?
With that change, the reproduction passes 5 of 5 times on each of the three databases. test/integration/async_processes_lifecycle_test.rb and the Puma plugin tests pass too, locally. Happy to open a PR with the test.
- 主要言語
- Ruby
- スター
- 2.5k
- フォーク
- 271
- 平均マージ
- 5日 6時間
- マージ済み PR(30日)
- 5
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
rails/solid_queue のほかの issue
-
難易度 5/5 1週間以上 初心者へのやさしさ 45/100
rails/solid_queue#836 ·
メンテナーはふだん 4 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
rails/solid_queue#834 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
rails/solid_queue#824 · リアクション 1 件 ·
メンテナーはふだん 4 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 42/100
rails/solid_queue#808 · コメント 3 件 ·
メンテナーはふだん 4 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
rails/solid_queue#806 ·
メンテナーはふだん 4 日以内に返信
rails/solid_queue の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
jenkins-infra/jenkins.io#9615 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
fix(ocr): compile the Tesseract-free pipeline with Tokio runtime対応中かも @Goldziher が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
WhatsApp Cloud template sync saves a partial template list when one page of the Graph API response fails対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 60/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 91/100
simp/pupmod-simp-ssh#270 ·