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

未關閉 適合新手
#838 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 4 天內回覆

還沒有人認領這個 Issue。

評估

難度
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 小時
30 天內合併 PR
5

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

  • 提供 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 沒有貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

rails/solid_queue 的其他 Issue

查看 rails/solid_queue 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。