Skip to content

Count attempts when a job is picked up so killed runs can't loop forever - #37

Merged
amamd07 merged 2 commits into
masterfrom
tasks/11943
Nov 16, 2026
Merged

amamd07 merged 2 commits into
masterfrom
tasks/11943

Conversation

@amamd07

@amamd07 amamd07 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

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) leaves attempts unchanged and failed_at unset. The worker name is host:<hostname> pid:<pid>; in a container both are the same after a restart, and get_job also picks up jobs where locked_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_job counts the attempt when it locks the job: attempts = attempts + 1 is added to the existing lock UPDATE, and the lock and attempt are committed right away. Otherwise a kill during run() would roll them back with the open transaction. It returns attempts - 1, so job.attempts still means "attempts before this run": set_error_unlock, the backoff and the jobAttempts reporting are unchanged, and a normal error is still counted once.
  • handle_job fails a job that already used max_attempts instead of running it. This goes through the normal error path: AttemptsExhaustedException → set_error_unlock → error / failure hooks, failed_at, and New Relic reporting.

Net effect: a job whose runs keep getting killed runs at most max_attempts times, then is marked failed.

Tests

  • New unit tests: the lock query counts the attempt; an exhausted job is not run and reports a failure; a job with attempts left still runs; the lock is committed before the job runs. All 68 tests pass.
  • Checked against Postgres 16 with a job SIGKILLed 3s into each of 4 pickups (max_attempts = 3): before this PR it ends with attempts = 0 and is picked up forever; with it, the three killed runs are counted and the 4th pickup fails the job (attempts = 4, failed_at set) without running it.

Notes

  • delayed_jobs.attempts now includes the run in progress, and a job failed this way ends with attempts = 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.

@amamd07

amamd07 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

@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.

Comment thread pyworker/worker.py
error = failed = False
caught_exc_info = None
try:
if job.attempts >= self.max_attempts:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this replace the comparison done on error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@maboelnour maboelnour self-assigned this Nov 10, 2026
@maboelnour

Copy link
Copy Markdown
Contributor

#38 is merged now; please update this branch from master so the checks run green here.

Comment thread pyworker/worker.py Resolved
ahmed and others added 2 commits November 11, 2026 13:36
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
@maboelnour

Copy link
Copy Markdown
Contributor

#38 is merged now; please update this branch from master so the checks run green here.

@amamd07

@amamd07
amamd07 merged commit d7307a3 into master Nov 16, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants