fix: keep queued_requests in sync with ml_tasks (stop unbounded queue growth)#12
Merged
Merged
Conversation
`queued_requests` (the sorted-set index behind queue_num / queue_time) was added on enqueue but only removed on explicit cancel — never when a worker claimed or completed a task. So it accumulated every task ever enqueued (observed one deepfake queue at 934k, ~99% already-resolved), wildly inflating reported queue depth/ETA and bloating Redis memory. Now mirror `ml_tasks` at every boundary: - zrem queued_requests when a worker claims a task (blpop) - zadd queued_requests on every re-queue (stuck / delayed / task-not-allowed) - clear queued_requests in delete_queue() All 49 tests pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
queued_requests(the Redis sorted set scored byqueued_at, whichqueue_num/queue_timeare computed from) iszadd-ed on enqueue but onlyzrem-ed inremove_task_from_queue()(explicit cancel). The normal lifecycle —blpop("ml_tasks")claim → process → complete — never removes the task fromqueued_requests.So
queued_requestsgrows without bound: every task ever enqueued stays in it forever. In production one deepfake queue held 934,500 entries of which ~99.5% were already-resolved (success/error) tasks — only ~44 were genuinely pending. This madequeue_num/queue_timemeaningless (and drove bogus autoscaling/ETA) and bloated the memory-capped Redis containers.ml_tasks(the actual work list) drains correctly viablpop; it's only the parallel index that leaks.Fix
Keep
queued_requestsmirroringml_tasksat every boundary:blpopin the worker loop):zrem("queued_requests", task_id)once the task is picked up — it's no longer queued.zadd("queued_requests", ...)(the not-allowed path alsosremsprocessing_tasks).delete_queue()now also clearsqueued_requests.Tests
All 49 tests pass (
tests/test_base.py, fakeredis). The existingqueued_requestsassertions are post-enqueue and unaffected.