Skip to content

Commit 89514c0

Browse files
fix(sidekiq): Report error when retry limit is below attempt_threshold (#2940)
* fix(sidekiq): Report error when retry limit is below attempt_threshold When a job's `retry` option is lower than its `attempt_threshold` (e.g. `retry: 1` with `attempt_threshold: 3`), the error handler skipped every attempt and the job was silently dropped — it died before ever reaching the threshold. Cap the threshold at the job's final attempt so these jobs still report before entering the dead set. Jobs whose retry count meets or exceeds the threshold behave exactly as before. * Reuse retry_limit --------- Co-authored-by: Neel Shah <neel.shah@sentry.io>
1 parent e747740 commit 89514c0

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

sentry-sidekiq/lib/sentry/sidekiq/error_handler.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ def call(ex, context, sidekiq_config = nil)
2121
scope = Sentry.get_current_scope
2222
scope.set_transaction_name(context_filter.transaction_name, source: :task) unless scope.transaction_name
2323

24+
retry_lim = retry_limit(context, sidekiq_config)
25+
2426
# If Sentry is configured to only report an error _after_ all retries have been exhausted,
2527
# and if the job is retryable, and have not exceeded the retry_limit,
2628
# return early.
2729
if Sentry.configuration.sidekiq.report_after_job_retries && retryable?(context)
2830
retry_count = context.dig(:job, "retry_count")
29-
if retry_count.nil? || retry_count < retry_limit(context, sidekiq_config) - 1
31+
if retry_count.nil? || retry_count < retry_lim - 1
3032
return
3133
end
3234
end
@@ -43,8 +45,10 @@ def call(ex, context, sidekiq_config = nil)
4345
# attempt 2 - this is your first retry so retry_count is 0
4446
# attempt 3 - you have retried once, retry_count is 1
4547
attempt = retry_count.nil? ? 1 : retry_count.to_i + 2
48+
# Cap at the final attempt so jobs with fewer retries than the threshold still report.
49+
effective_threshold = [attempt_threshold, retry_lim + 1].min
4650

47-
return if attempt < attempt_threshold
51+
return if attempt < effective_threshold
4852
end
4953

5054
Sentry::Sidekiq.capture_exception(

sentry-sidekiq/spec/sentry/sidekiq_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ def retry_last_failed_job
141141
retry_last_failed_job
142142
expect(transport.events.count).to eq(0)
143143
end
144+
145+
it "reports on the final attempt when retry limit is below the threshold" do
146+
worker = Class.new(SadWorker)
147+
worker.sidekiq_options attempt_threshold: 3, retry: 1
148+
149+
execute_worker(processor, worker)
150+
expect(transport.events.count).to eq(0)
151+
152+
retry_last_failed_job
153+
expect(transport.events.count).to eq(1)
154+
end
144155
end
145156

146157
context "with config.report_only_dead_jobs = true" do

0 commit comments

Comments
 (0)