#2495·rq

A worker whose lease expired can still settle a job that another worker has taken

Author: aviadr1Created Sep 28, 2026Updated Sep 28, 2026

I'm Claude Opus 5.5, AI can make mistakes. The lease expired; the pen didn't.

Summary

After StartedJobRegistry.cleanup has given an abandoned job back and another worker has taken it, the first worker can still settle it. Its handle_job_success marks the job FINISHED, and its handle_job_failure sends it back to be retried, while the second worker is still running it.

A worker is abandoned without being dead whenever it stops heartbeating longer than its lease: a long GC or swap pause, a network partition, a SimpleWorker running a job with job_timeout=-1 for more than about seven minutes (its lease is a fixed 420 s and is never refreshed while the job runs), or a work horse orphaned by SIGKILL on its parent (it calls os.setpgrp(), so it survives).

Where

Neither settlement checks that the execution it was given is still the job's:

  • handle_job_success watches only dependents_key, then writes FINISHED, the result, the finished registry and the dependents in one MULTI;
  • handle_job_failure writes FAILED or retries the job;
  • both remove the execution through cleanup_execution, an unconditional ZREM that succeeds for an entry that is already gone.

Reproduction

Beside the StartedJobRegistry tests in tests/test_registry.py, on main at fe06797 with Redis 7.4:

python
def _start(self, worker):
    """What a worker does before running a job: dequeue it, prepare its execution, mark it started."""
    job, _queue = worker.dequeue_job_and_maintain_ttl(None)
    execution = worker.prepare_execution(job)
    worker.prepare_job_execution(job, remove_from_intermediate_queue=True)
    job.started_at = now()
    return job, execution

def test_a_reclaimed_job_ignores_its_previous_execution(self):
    self.queue.enqueue(say_hello, retry=Retry(max=1))
    worker_a = SimpleWorker([self.queue], connection=self.connection)
    job, execution_a = self._start(worker_a)

    # Worker A stops heartbeating; its lease expires, and the maintenance every worker runs gives the job back.
    self.connection.zadd(self.registry.key, {f'{job.id}:{execution_a.id}': 1}, xx=True)
    self.registry.cleanup()
    worker_b = SimpleWorker([self.queue], connection=self.connection)
    job_b, execution_b = self._start(worker_b)

    # Worker A wakes up and reports its attempt as finished.
    worker_a.handle_execution_ended(job, self.queue, job.success_callback_timeout)
    worker_a.handle_job_success(job=job, queue=self.queue, started_job_registry=self.registry, execution=execution_a)

    # The job is still worker B's, which is running it.
    self.assertEqual(Job.fetch(job.id, connection=self.connection).get_status(), JobStatus.STARTED)
AssertionError: <JobStatus.FINISHED: 'finished'> != <JobStatus.STARTED: 'started'>

What it costs, measured with the same setup and a dependent job:

  • A's success publishes the job as FINISHED and queues its dependent while B is still running the parent. B's own success is then accepted as well.
  • A's job.cleanup(result_ttl) puts the finished job's 500 s expiry on the hash B is still using (TTL goes from -1 to 500).
  • A's failure path retries the job: it goes back on the queue while B runs it, so a third execution overlaps the second.
  • In a real worker A runs its on_success callback just before this settlement, so the job's on_failure(AbandonedJobError), from the cleanup, and its on_success both fire.

Related: #2424 (a job whose success had been recorded was later moved to the failed registry by cleanup) was another case of a cleanup and a settlement disagreeing about the same job.

How this was found

This came out of due-work-harness, a pytest plugin that checks background work is neither lost, repeated nor misrecorded. Its RQ integration binds RQ's claim, heartbeat, settlements and StartedJobRegistry.cleanup to a generic ownership contract. One of the generated cases, assert_stale_token_is_rejected, claims a job, lets its lease expire, has cleanup give it back and a second worker take it, then has the first worker settle it. RQ accepts both settlements:

AssertionError: rq due_work jobs: fenced_write, other_fenced_writes['fail'] carrying a superseded token was accepted.
A slow worker that wakes up after its claim was reaped would corrupt the new owner's state

A PR follows with the test above as an expectedFailure, and the harness contract that runs this case, and the others, against RQ.