Repository navigation
Count attempts when a job is picked up so killed runs can't loop forever - #37
Conversation
|
@maboelnour I remember you have done this fix before, no? Did you change anything so that a job whose worker gets OOMKilled stops being picked up again? We found that a killed job is reclaimed immediately and attempts never increases. |
| error = failed = False | ||
| caught_exc_info = None | ||
| try: | ||
| if job.attempts >= self.max_attempts: |
There was a problem hiding this comment.
Does this replace the comparison done on error?
There was a problem hiding this comment.
No, it complements it. The comparison in set_error_unlock still handles runs that fail with an exception: on the last attempt the job is failed right away and the failure hook runs. The new check only matters for runs that never reached set_error_unlock because the process was killed, so their attempts were counted at pickup but never finished. Dropping the error-path comparison would fail those jobs one pickup later than now (rescheduled with backoff, picked up, then failed), so both are kept.
|
#38 is merged now; please update this branch from master so the checks run green here. |
A run that never reaches set_error_unlock (the process is SIGKILLed, e.g. by the OOM killer) left attempts unchanged and failed_at unset. Because the worker name (hostname + pid) is the same after a container restart and get_job also matches locked_by = self.name, the restarted worker reclaimed the same job immediately and was killed again, forever. - get_job increments attempts in the existing lock UPDATE (no extra query) and returns attempts - 1, so job.attempts still means attempts before this run and set_error_unlock, backoff and reporting are unchanged. - handle_job fails a job that already used max_attempts instead of running it, through the normal error path (error/failure hooks, failed_at, reporting). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The lock UPDATE in get_job ran in an open transaction that was only committed by a later query on the same connection (set_error_unlock, remove, or a job's own hook). A process killed during run() rolled back attempts + 1 together with the lock, so a job killed every time still had attempts = 0 and was picked up again forever. Commit right after the UPDATE returns. Checked against Postgres 16: a job SIGKILLed on each of 4 pickups ends with attempts = 0 before this change, and with attempts = 4 and failed_at set after it (max_attempts = 3). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PqPDJ4ACaD8D7s3VMSFmfY
Stops a job from being retried forever when the worker process is killed while running it (e.g. OOM-killed in Kubernetes).
Problem
A run that never reaches
set_error_unlock(the process gets SIGKILL) leavesattemptsunchanged andfailed_atunset. The worker name ishost:<hostname> pid:<pid>; in a container both are the same after a restart, andget_jobalso picks up jobs wherelocked_by = self.name. So the restarted worker reclaims the same job immediately and is killed again, indefinitely. A job that reliably runs a worker out of memory can therefore keep it crash-looping for hours and block its queue.Fix
get_jobcounts the attempt when it locks the job:attempts = attempts + 1is added to the existing lockUPDATE, and the lock and attempt are committed right away. Otherwise a kill duringrun()would roll them back with the open transaction. It returnsattempts - 1, sojob.attemptsstill means "attempts before this run":set_error_unlock, the backoff and thejobAttemptsreporting are unchanged, and a normal error is still counted once.handle_jobfails a job that already usedmax_attemptsinstead of running it. This goes through the normal error path:AttemptsExhaustedException→set_error_unlock→error/failurehooks,failed_at, and New Relic reporting.Net effect: a job whose runs keep getting killed runs at most
max_attemptstimes, then is marked failed.Tests
max_attempts = 3): before this PR it ends withattempts = 0and is picked up forever; with it, the three killed runs are counted and the 4th pickup fails the job (attempts = 4,failed_atset) without running it.Notes
delayed_jobs.attemptsnow includes the run in progress, and a job failed this way ends withattempts = max_attempts + 1.Because the lock is now committed when the job is picked up, other workers see it immediately and skip the job; before, they could block on the uncommitted row until the job finished.