Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@RisingOrange
Copy link
Contributor

@RisingOrange RisingOrange commented Mar 2, 2025

This fixes some issues with aqt.taskman.TaskManager:

  • TaskManager.run_in_background() can raise an exception if there was an exception in a (possibly unrelated) background task that was just completed. When this happens, the new background task is not run and the exception bubbles up to the code that called run_in_background().
    • This can lead to confusing issues. For example, we encountered an issue where the central exception handler of the AnkiHub add-on was raising an exception after trying to start a background task (for uploading logs). As a fix, we wrapped the run_in_background() calls in the exception handler with run_on_main(). We encountered similar issues with this before.
  • When there are multiple closures in TaskManager._closures, and one of the closures raises an exception, the other closures are not called at all

Proposed changes

  • In TaskManager._on_closures_pending(), catch exceptions and re-raise them using QTimer.singleShot with a delay of 0 instead of allowing them to bubble up immediately.
    • Effects:
      • All closures are run
      • The caller of run_in_background() is not interrupted by exceptions from closures and the new background task is scheduled successfully
      • All exceptions from closures are raised

Risks

  • Maybe some code relies on the current taskman exception-handling behavior?
    • If making this change is too risky, it could be adjusted. However, it would be good to prevent exceptions from bubbling up to the caller of run_in_background(), or at least document that this can happen.

@RisingOrange RisingOrange marked this pull request as draft March 3, 2025 01:47
@RisingOrange RisingOrange changed the title fix: Exception propagation in run_in_background fix: Task manager exception handling Mar 3, 2025
@RisingOrange RisingOrange marked this pull request as ready for review March 3, 2025 09:44
@dae
Copy link
Member

dae commented Mar 10, 2025

Seems like a reasonable approach. Thanks for digging into this!

@dae dae merged commit d8460d3 into ankitects:main Mar 10, 2025
1 check passed
@RisingOrange RisingOrange deleted the fix/taskman-run_in_background-exception-from-closure branch March 10, 2025 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants