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 摘要。