Repository navigation
Conversation
A killed API process never reaches task_done(), so its task stayed in processing forever and DELETE kept answering 409. The lease expiry already frees the global slot; record which task each slot runs and fail the task when its lease is gone. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
harry0703
left a comment
There was a problem hiding this comment.
Thanks for tracking this down. I reproduced the original bug locally on macOS with Redis 7.2.3 and the default 90-second lease. After a SIGKILL and API restart, main still returned 409 when I tried to delete the interrupted task. On this branch the task kept its progress, was marked failed with failed_stage=worker, and could be deleted with a 200. In both runs the queued task went on to produce an MP4.
Could you fix these three cases before we merge? I shortened the lease for the tests below so I could repeat them quickly.
- A paused worker can bring back a deleted task. I paused the worker with
SIGSTOP, waited for a second API process to mark the task failed, then deleted it.DELETEreturned200andGETreturned404. AfterSIGCONT, the same task ID reappeared as complete with a new MP4. Please use the lease token to reject state and output commits from a worker that has lost its slot. Deletion also needs to wait until that worker can no longer write to the task directory. - The expiry check can overwrite a completed task. I paused the dispatcher just after it read
processing, let the worker finish, then resumed the dispatcher. The API went from complete to failed withfailed_stage=worker, even though progress was already 100% and the MP4 was present. Please recheck the token's ownership, lease and current task state in the same Redis operation that marks it failed. - A crash during queue pickup can still leave a task stuck. I killed the API process after it popped a queued task and reserved its slot, just before it wrote the token-to-task mapping. After restart and lease expiry, the task stayed at
processingwith 0% progress andDELETEreturned409. Please record the task ID in the same Lua operation that takes the queue item and creates its lease.
Regression tests for those three cases would make this much easier to verify on the next pass.
…tomically Review of the first commit found three cases it did not cover. A worker that was paused, not killed, wrote its task again after the task had been failed and deleted. Worker writes now go through its slot token and are refused once the expiry check has taken the token away, and every deletion entry point waits for an OS lock the worker process holds on the task directory. The expiry check read the task and wrote the failure in separate steps, so a task that completed in between was turned into a failure. The slot, its lease and the task state are now checked in the Redis operation that writes the failure. A process killed between claiming a queue item and recording its task left a slot nobody could trace. The scripts that create a lease now record the task themselves. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…g it Holding the lock across the removal made two concurrent deletions of the same finished task exclude each other: one got 409 and test_concurrent_task_deletions_do_not_return_server_error failed in CI. A deletion only needs to know that no worker process holds the directory. Once it is free it stays free, because a task in a final state is never handed to a worker again. The check waits briefly so that another deletion asking the same question is not mistaken for a worker. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
All three reproduce here on 1. A paused worker brings back a deleted task
Two limits: the worker is stopped at its next state write, not at once, and the lock needs the deleting process and the worker to share the storage directory. I did not run it across hosts or on Windows. 2. The expiry check overwrites a completed task API B is held for 6 s right before its write that marks the task failed. API A's lease is not renewed and its worker finishes during those 6 s.
3. A crash during queue pickup leaves a task stuck API A exits right after the claim script returns, before any Python code handles the payload.
Tests
One behaviour change to be aware of: a live worker that cannot renew for longer than the lease used to keep running and flip its task back to |
|
@harry0703 The three cases from your review are fixed in 9b78dde and 02addc5; the runs are in my reply above. Could you take another look when you have time?
That run used a fakeredis TCP server, not a real Redis; the process-level runs in my earlier reply were against Redis 7. I did not push the merge. If you would rather have |
|
Thanks for addressing the earlier cases. I reran the queue and controller tests against real Redis on top of current main, and those checks passed. I found one upgrade case that still leaves a task stuck. With the storage root read-only and the existing storage/tasks directory writable, main completed the task. On this branch, creating storage/task_locks raised PermissionError before the worker entered the pipeline. The slot and its task mapping were removed, but the task stayed in PROCESSING, even after an expiry sweep. Could we handle failures to create or acquire the directory lock so a task ends in a state the user can recover from? |
With a storage root that is not writable, creating storage/task_locks raised PermissionError in the worker before the pipeline started. The exception skipped everything except task_done(), which removed the slot and its task entry, so the task stayed in processing and nothing was left for the expiry check to find. A lock that could not be acquired was only logged and the pipeline ran without it. The worker now fails the task when the lock directory cannot be created or the lock cannot be acquired, and does not run the pipeline. The failure is written through the slot token, before the slot is released. A deletion that cannot create or open the lock goes ahead when the lock file does not exist: a worker runs a task only while it holds that file. It still refuses when the file exists and cannot be opened. Before, the API answered 500 and the WebUI raised. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
This reproduces here on Layout: The
Tests Six new tests. They raise the
All of them fail on Both runs used the fakeredis TCP server. Limits
|
With
enable_redis = true, a task whose API process is killed while it runs (OOM kill,docker kill, power loss) stays instate=4forever, andDELETE /api/v1/tasks/{id}keeps answering409 task is still running. Tasks that were only queued recover; the one that was running does not.#1742 gave every running slot a renewable lease, so the crashed worker's capacity comes back after 90 s. The task record was left out: the slot member is a random token, so once the lease is pruned nothing knows which task it belonged to, and the only cleanup is the
finally: self.task_done()that a killed process never reaches.Real run on macOS, Python 3.13, three processes: a Redis stand-in (fakeredis
TcpFakeServer), an API process that accepts two requests and iskill -9ed while the first one runs, and a replacement API process that goes through the normal lifespan startup. Onlytm._run_pipelineis stubbed. The 90 s lease and 30 s heartbeat are unchanged.Cause:
app/controllers/manager/redis_manager.py—_ADMIT_TASK_SCRIPTand_CLAIM_QUEUED_TASK_SCRIPTdrop expired slots withZREMRANGEBYSCOREand nothing else looks at the task that slot was running.Fix:
<queue>:running:tasks(token -> task_id). The admission and claim scripts write the entry in the same operation that creates the lease, and it is removed before the slot is released.fail_tasks_of_expired_slots()lists the recorded slots whose lease is missing or past its deadline. For each one, a single script checks again that the token still owns the task and that its lease is still expired, sets the task to failed (failed_stage="worker") only if it is stillprocessing, and removes the entry and the slot. A task that already reached a final state keeps it. A slot that was renewed in between is left alone.update_task()andpatch_task()go through a script that first checks the token still owns the task. Once the expiry check has removed the entry,update_task()raisesTaskOwnershipLostError, which stops the pipeline, andpatch_task()returns False. A worker that was only paused cannot change the record or create it again after a deletion.storage/task_locks/,filelock) for as long as it runs it. If the lock directory cannot be created or the lock cannot be acquired, the worker fails the task through its slot token and does not run the pipeline. The API delete and the WebUI delete check that lock first and refuse (409 for the API) while a process holds it. A killed process releases it at once; a paused one keeps it until it resumes and stops. The deletion does not hold the lock itself, so two deletions of the same task still both succeed.check_queue(), so it runs at startup and then at most every 5 s.Liveness is decided by the same renewable lease that already returns the slot, not by the age of the task.
Validation:
tm._run_pipelinestubbed. Each case was run on the first commit of this PR and on the current head; the outputs are in the review reply below.SIGSTOPped mid-task: failed after the lease,DELETEanswers 409 while the process is stopped. AfterSIGCONTthe task stays failed and the delete returns 200. The record and the directory do not come back.SIGKILLed mid-task (the original case): failed after the lease,DELETEreturns 200.test/services/test_redis_queue_admission.py(they need Redis, like the rest of the file, and now keep the task records in the same Redis): the five from the first commit, plus a renewed slot is left alone, a task that completes during the check keeps its result, a crash right after admission or right after queue pickup leaves a task that gets failed, a worker that lost its slot cannot bring back a deleted task or overwrite the failure, a worker that still holds its slot writes normally, and the task directory stays held until the worker stops.test_controller_video.pyandtest_webui_task.pycover the two delete entry points refusing a held directory.python -X utf8 -m pytest -q test/services/test_redis_queue_admission.py test/services/test_task_manager.py test/services/test_controller_video.py test/services/test_webui_task.py test/services/test_concurrent_task_delete.py→ 123 passed against Redis 7 (at02addc5).aec13dcadds six tests for a lock that cannot be taken (three for the worker, two for the API delete, one for the WebUI delete). The same five files: 129 passed against a fakeredis TCP server.ruff check .Other effects of this change:
token -> task_identry, not the lease score. A worker whose lease was pruned by a new admission, but whose task the expiry check has not failed yet, can still finish and write its result.storage/task_locks/. A killed worker leaves its lock file behind until the task is deleted.failed_stage="worker"and the path and OS error inerror. This includes a storage root that is not writable, wherestorage/task_lockscannot be created:maincompletes a task in that layout (withstorage/taskswritable), and with this change every task in Redis mode ends failed there.Not changed here:
processingrecord with no slot and no queue item.🤖 Generated with Claude Code