Skip to content

Commit 328fb87

Browse files
hannesfostieclaude
authored andcommitted
Release in-flight jobs when the async supervisor's shutdown times out
When a job outlives shutdown_timeout during graceful termination, the async supervisor ends with exit!, which skips the shutdown callbacks: neither the supervisor nor its threads deregister, and their claimed jobs stay claimed until a later supervisor prunes the stale registrations and fails them with ProcessPrunedError. On a TERM, which is what deploys send, the job is failed instead of going back to its queue as it does in fork mode. Worse, the supervisor and each worker wait for that same timeout, the supervisor starting slightly earlier, so it usually runs out first and calls exit! while the worker is still inside its own deregistration transaction, rolling back the release it had started. When the timeout runs out with threads still alive, deregister the supervisor before exit!, as the fork supervisor does after quitting its forks: the threads still running are deregistered with it and their claimed jobs go back to their queues. Deregister from a fresh copy of the registration, since the threads share theirs with the supervisor's in memory and a thread that already deregistered leaves a frozen record behind. QUIT is unchanged: it still exits without cleanup, as the async lifecycle test documents. The async lifecycle test for TERM past the timeout now expects the in-flight job back in its queue and a clean termination, like its fork counterpart, instead of accepting either outcome. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent ec42a7e commit 328fb87

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎lib/solid_queue/async_supervisor.rb‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,26 @@ def perform_graceful_termination
3838
process_instances.values.each(&:stop)
3939

4040
Timer.wait_until(SolidQueue.shutdown_timeout, -> { all_processes_terminated? })
41+
42+
# The exit! that follows skips the shutdown callbacks, so release the jobs
43+
# of the threads still running now instead of leaving them to be pruned
44+
attempt_to_deregister unless all_processes_terminated?
4145
end
4246

4347
def perform_immediate_termination
4448
exit!
4549
end
4650

51+
def attempt_to_deregister
52+
wrap_in_app_executor do
53+
# Not our own copy: it shares the threads' registrations in memory, and
54+
# those of threads that already deregistered are destroyed and frozen
55+
SolidQueue::Process.find_by(id: process_id)&.deregister
56+
end
57+
rescue StandardError => error
58+
handle_thread_error(error)
59+
end
60+
4761
def all_processes_terminated?
4862
process_instances.values.none?(&:alive?)
4963
end

‎test/integration/async_processes_lifecycle_test.rb‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,11 @@ class AsyncProcessesLifecycleTest < ActiveSupport::TestCase
173173
"registered processes: #{SolidQueue::Process.all.map { |p| { id: p.id, kind: p.kind, pid: p.pid, last_heartbeat_at: p.last_heartbeat_at } }.inspect}"
174174
end
175175

176-
# After shutdown, the pause job may be either:
177-
# - claimed (exit! called, no cleanup) OR
178-
# - ready (graceful exit, job released back to queue)
179-
# Both are valid outcomes depending on the timing race between supervisor and worker timeouts.
180-
skip_active_record_query_cache do
181-
job = SolidQueue::Job.find_by(active_job_id: pause.job_id)
182-
assert job.claimed? || job.ready?, "Expected pause job to be claimed or ready, but was neither"
183-
end
176+
# Workers were shutdown without a chance to terminate orderly, but
177+
# since they're linked to the supervisor, the supervisor deregistering
178+
# also deregistered them and released claimed jobs
179+
assert_job_status(pause, :ready)
180+
assert_clean_termination
184181
end
185182

186183
test "process some jobs that raise errors" do

0 commit comments

Comments
 (0)