BUG: Model downloads are cancelled after 10 minutes and the progress bar freezes with no error
Is there an existing issue for this?
- [x] I have searched the existing issues
What happened?
Model downloads get stopped after 10 min even when they're still downloading. _cleanup_stale_tasks in routes/models.py cancels every task older than 10 min, doesn't check if it's done or not. On slow wifi the semantic models (~1.5gb) never finish. Also after it gets cancelled there's no error, progress bar just stays stuck. During onboarding it was stuck on "Downloading facenet... 74%" forever. To test I changed the 10 min to 30 sec. I have attached a screenshot for the same below. after that /models/status said facenet was installed but it was only 19mb out of 87.
Record
- [x] I agree to follow this project's Code of Conduct
👋 Thanks for opening this issue! The maintainers will review it shortly. Till then, do not open any PR. This is a strict rule we like to follow around here :)
@rohan-pandeyy if this is considered a valid issue can I work on this
⚠️ Potential Duplicate Issue (Semantic Match)
This issue appears semantically similar to the following issues in AOSSIE-Org/PictoPy:
-
BUG: Model download endpoint attempts network requests to PLACEHOLDER_URL for unreleased ONNX models (#1564, open) https://github.com/AOSSIE-Org/PictoPy/issues/1564 Similarity: 63.3%
-
BUG: An interrupted model download is reported as installed and then fails to load (#1634, open) https://github.com/AOSSIE-Org/PictoPy/issues/1634 Similarity: 62.7%
Please review before proceeding.
Not a duplicate. #1564 is about requests to PLACEHOLDER_URL for unreleased models, and #1634 is about partial files being reported as installed. This one is about _cleanup_stale_tasks cancelling downloads that are still running after 10 min.
What's the proposed approach @ManyaJain111?
@rohan-pandeyy I looked into this, and the issue traces back to a chain reaction in backend/app/routes/models.py:
- Premature Task Eviction:
In
_cleanup_stale_tasks, downloads running longer than 10 minutes are cancelled viaentry.task.cancel(), regardless of whether bytes are actively streaming. - Silent Cancellation:
task.cancel()raisesasyncio.CancelledError, which inherits fromBaseExceptionand bypasses the worker'sexcept Exception:block. No error payload is ever pushed toentry.queue. - Infinite SSE Heartbeat Loop (Frozen UI):
In
download_progress,event_generatorkeeps emitting heartbeats on the empty queue without verifying ifentry.task.done(), leaving the UI progress bar frozen indefinitely.
Proposed Approach / Fix:
- Activity-Based Cleanup: Track
last_activity(updated insideprogress_callback) onDownloadTaskEntryrather thancreated_at. Only evict and cancel tasks in_cleanup_stale_tasksif no progress has been made for 10 minutes and no active listeners remain. - Handle Cancellation Gracefully: Explicitly catch
asyncio.CancelledErrorin the background download coroutines so an{"status": "error", "message": "Download cancelled"}event is placed intoentry.queuebefore re-raising. - Guard the SSE Stream: In
event_generator, check ifentry.task.done()andentry.queue.empty()during timeout periods. If the task has stopped unexpectedly, yield an error event and break the loop.
@rohan-pandeyy I'd change the cleanup so it only cancels downloads that have stopped making progress, not every one older than 10 min. I'd also send an error event when a download gets cancelled so the progress bar doesn't just freeze. Partial files being shown as installed is already #1634, so I'll leave that part out. I would be happy to start on it if that works :))
@rohan-pandeyy Hello can i get this assigned to me?,i would like to work on this issue
@ManyaJain111 Are you still working on this issue?
Yes I am working on this and would raise a pr soon