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

Ouverte Adaptée aux débutants
#838 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 4 jours

Personne n'a encore pris cette issue.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
72/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
rails, ruby

Piste de recherche

Lisez le cycle de vie du plugin Puma dans test/integration/puma/plugin_testing.rb et les callbacks d’arrêt de AsyncSupervisor décrits dans l’issue. Exécutez la reproduction fournie avec TARGET_DB=sqlite bin/rails test test/integration/puma/plugin_stop_repro_test.rb ; elle devrait réussir avec le mode async. L’issue mentionne également test/integration/async_processes_lifecycle_test.rb et les tests du plugin Puma comme tests de régression.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
Ruby
Étoiles
2.5k
Forks
271
Merge moyen
5 j 6 h
PR mergées (30 j)
5

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

  • Fournit un Dockerfile ou un fichier Docker Compose
  • Aucun modèle de pull request
  • Aucun guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de rails/solid_queue

Toutes les issues de rails/solid_queue

Issues similaires

Plus d'issues Ruby

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.