PictoPy icon indicating copy to clipboard operation
PictoPy copied to clipboard

BUG: Model downloads are cancelled after 10 minutes and the progress bar freezes with no error

Open ManyaJain111 opened this issue 1 month ago • 10 comments

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.

Image

Record

  • [x] I agree to follow this project's Code of Conduct

ManyaJain111 avatar Oct 23 '26 12:10 ManyaJain111

👋 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 :)

github-actions[bot] avatar Oct 23 '26 12:10 github-actions[bot]

@rohan-pandeyy if this is considered a valid issue can I work on this

ManyaJain111 avatar Oct 23 '26 12:10 ManyaJain111

⚠️ Potential Duplicate Issue (Semantic Match)

This issue appears semantically similar to the following issues in AOSSIE-Org/PictoPy:

  1. 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%

  2. 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.

github-actions[bot] avatar Oct 23 '26 12:10 github-actions[bot]

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.

ManyaJain111 avatar Oct 23 '26 12:10 ManyaJain111

What's the proposed approach @ManyaJain111?

rohan-pandeyy avatar Oct 25 '26 09:10 rohan-pandeyy

@rohan-pandeyy I looked into this, and the issue traces back to a chain reaction in backend/app/routes/models.py:

  1. Premature Task Eviction: In _cleanup_stale_tasks, downloads running longer than 10 minutes are cancelled via entry.task.cancel(), regardless of whether bytes are actively streaming.
  2. Silent Cancellation: task.cancel() raises asyncio.CancelledError, which inherits from BaseException and bypasses the worker's except Exception: block. No error payload is ever pushed to entry.queue.
  3. Infinite SSE Heartbeat Loop (Frozen UI): In download_progress, event_generator keeps emitting heartbeats on the empty queue without verifying if entry.task.done(), leaving the UI progress bar frozen indefinitely.

Proposed Approach / Fix:

  1. Activity-Based Cleanup: Track last_activity (updated inside progress_callback) on DownloadTaskEntry rather than created_at. Only evict and cancel tasks in _cleanup_stale_tasks if no progress has been made for 10 minutes and no active listeners remain.
  2. Handle Cancellation Gracefully: Explicitly catch asyncio.CancelledError in the background download coroutines so an {"status": "error", "message": "Download cancelled"} event is placed into entry.queue before re-raising.
  3. Guard the SSE Stream: In event_generator, check if entry.task.done() and entry.queue.empty() during timeout periods. If the task has stopped unexpectedly, yield an error event and break the loop.

Sirwagya avatar Oct 25 '26 11:10 Sirwagya

@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 :))

ManyaJain111 avatar Oct 25 '26 11:10 ManyaJain111

@rohan-pandeyy Hello can i get this assigned to me?,i would like to work on this issue

RitikaSen27 avatar Oct 26 '26 08:10 RitikaSen27

@ManyaJain111 Are you still working on this issue?

Sirwagya avatar Oct 31 '26 12:10 Sirwagya

Yes I am working on this and would raise a pr soon

ManyaJain111 avatar Oct 31 '26 15:10 ManyaJain111