Skip to content

Commit 2124a51

Browse files
mhenrixonrosa
authored andcommitted
Guard signal_all against incrementing semaphore beyond its limit
`Semaphore#signal` (the single-job path) already caps its increment with `value < limit`, but the batch `signal_all` path — used when releasing concurrency locks in bulk, e.g. when discarding ready jobs — did not. That let a semaphore's value climb above the concurrency limit, effectively leaking permits and admitting more concurrent executions than allowed. Group the jobs by their concurrency limit and cap each group's increment with `value < limit`, matching the single-job path.
1 parent 42db567 commit 2124a51

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎app/models/solid_queue/semaphore.rb‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ def create_unique_by(attributes)
3232

3333
class Proxy
3434
def self.signal_all(jobs)
35-
Semaphore.where(key: jobs.map(&:concurrency_key)).update_all("value = value + 1")
35+
# Guard against incrementing a semaphore's value beyond its limit. Jobs can
36+
# have different limits, so group them and cap each group with `value < limit`.
37+
jobs.group_by { |job| job.concurrency_limit || 1 }.each do |limit, grouped_jobs|
38+
Semaphore.where(key: grouped_jobs.map(&:concurrency_key))
39+
.where(value: ...limit)
40+
.update_all("value = value + 1")
41+
end
3642
end
3743

3844
def initialize(job)

‎test/models/solid_queue/semaphore_test.rb‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,40 @@ class SolidQueue::SemaphoreTest < ActiveSupport::TestCase
108108
assert_equal 0, SolidQueue::BlockedExecution.where(concurrency_key: concurrency_key).count
109109
end
110110

111+
test "signal_all does not increment semaphore value beyond limit" do
112+
# Create a job with concurrency limit of 1
113+
NonOverlappingUpdateResultJob.perform_later(@result)
114+
job = SolidQueue::Job.last
115+
concurrency_key = job.concurrency_key
116+
117+
# Semaphore should exist with value=0 (one slot taken)
118+
semaphore = SolidQueue::Semaphore.find_by(key: concurrency_key)
119+
assert_equal 0, semaphore.value
120+
121+
# Manually set semaphore to its limit (simulating the slot was already returned)
122+
semaphore.update!(value: 1)
123+
124+
# signal_all should NOT increment beyond the limit
125+
SolidQueue::Semaphore.signal_all([ job ])
126+
127+
assert_equal 1, semaphore.reload.value, "signal_all should not increment semaphore beyond its limit"
128+
end
129+
130+
test "signal_all increments semaphore when below limit" do
131+
# Create a job with concurrency limit of 1
132+
NonOverlappingUpdateResultJob.perform_later(@result)
133+
job = SolidQueue::Job.last
134+
concurrency_key = job.concurrency_key
135+
136+
# Semaphore should exist with value=0
137+
assert_equal 0, SolidQueue::Semaphore.find_by(key: concurrency_key).value
138+
139+
# signal_all should increment from 0 to 1
140+
SolidQueue::Semaphore.signal_all([ job ])
141+
142+
assert_equal 1, SolidQueue::Semaphore.find_by(key: concurrency_key).value
143+
end
144+
111145
private
112146
def skip_on_sqlite
113147
skip "Row-level locking not supported on SQLite" if SolidQueue::Record.connection.adapter_name.downcase.include?("sqlite")

0 commit comments

Comments
 (0)