Puma plugin in async mode: jobs that finish while Puma stops go back to the queue

オープン 初心者向け
#838 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 4 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rails, ruby

調査の方向性

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:

  1. Registrable's :deregister comes first. Deregistering the supervisor also deregisters its supervisees, which releases the workers' claimed executions while their jobs are still running.
  2. AsyncSupervisor's after_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

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

  • Dockerfile または Docker Compose ファイルあり
  • プルリクエストのテンプレートなし
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rails/solid_queue のほかの issue

rails/solid_queue の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。