The dead-letter queue holds tasks for a person to fix
A task lands in the dead-letter queue because it failed every attempt. Most of the time that means a bug or bad data, not bad luck. So the dead-letter queue is a to-do list for a person.
What happens next, in a real system:
- An alert fires when the dead-letter queue grows. Nobody reads a list they don't know about.
- Someone looks at the dead tasks, and the error each one raised, and finds the cause.
- They fix the bug, and redrive the tasks: send them back to the queue with fresh attempts.
A redrive must reset the failure count. A task that comes back with 3 failures and max_attempts=3 dies on its very next failure, with no real retry.
Leave the tasks that weren't fixed where they are, in their order. They are still a record of what went wrong.
There's a classic Python bug waiting in this code. If you loop over a list and remove items from it in the same loop, the loop skips the item after each one you remove. Here's why: after a remove, every later item moves up one place, and the loop's position moves on past the item that just slid into the empty spot. Build a new list instead, and assign it back with dead[:] = ..., so the caller's list changes too.
dead is [a, b, c, d]. You loop over it and remove each task in {b, c}. What is left?