Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix background tasks blocking UI #797

Merged

Conversation

RisingOrange
Copy link
Collaborator

@RisingOrange RisingOrange commented Nov 8, 2023

Anki 23.10 serializes background tasks by default and uses_collection=False needs to be specified in order to allow the task to be run in parallel with other tasks (by Anki's task manager which is defined in aqt.mw.taskman).

The add-on is using aqt.taskman.TaskManager.run_in_background in multiple places to start background tasks. Due to the changes introduced in Anki 23.10 some background tasks can now block the UI.
For example, when the media sync is in progress and then the user selects a deck on the Anki main window, a progress dialog opens and blocks the UI like this until the media sync is done:

This is because Anki calls set_current_deck() as a CollectionOp in the background and set_current_deck can’t be run until the media sync is done.

To fix this, we can specify uses_collection=False for these background tasks so that the task manager (aqt.mw.taskman) runs the task in parallel with other tasks. We can do this for all tasks that don't use the Anki collection.

Related issues

Proposed changes

@RisingOrange RisingOrange force-pushed the chore/specify-uses-collection-false-for-background-tasks branch from 9b9fd35 to 4e5526a Compare November 8, 2023 14:26
Copy link

codecov bot commented Nov 8, 2023

Codecov Report

Merging #797 (2b78b8c) into main (c1e76c8) will increase coverage by 0.46%.
The diff coverage is 97.67%.

@@            Coverage Diff             @@
##             main     #797      +/-   ##
==========================================
+ Coverage   81.94%   82.41%   +0.46%     
==========================================
  Files          64       65       +1     
  Lines        5743     5754      +11     
==========================================
+ Hits         4706     4742      +36     
+ Misses       1037     1012      -25     
Files Coverage Δ
ankihub/gui/errors.py 54.78% <100.00%> (+8.54%) ⬆️
ankihub/gui/media_sync.py 95.09% <100.00%> (+0.20%) ⬆️
ankihub/gui/menu.py 46.81% <100.00%> (+0.17%) ⬆️
ankihub/gui/operations/deck_creation.py 80.00% <100.00%> (ø)
ankihub/gui/optional_tag_suggestion_dialog.py 83.01% <100.00%> (+0.16%) ⬆️
ankihub/gui/operations/__init__.py 91.66% <91.66%> (ø)

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

@RisingOrange RisingOrange changed the title chore: Set uses_collection=False for chosen background tasks chore: Fix background tasks blocking UI Nov 8, 2023
@RisingOrange RisingOrange force-pushed the chore/specify-uses-collection-false-for-background-tasks branch from 6711911 to 4e5526a Compare November 8, 2023 15:52
@RisingOrange RisingOrange force-pushed the chore/specify-uses-collection-false-for-background-tasks branch from e9fa39a to f98bf7a Compare November 9, 2023 21:18
@RisingOrange RisingOrange force-pushed the chore/specify-uses-collection-false-for-background-tasks branch from f98bf7a to c32218c Compare November 9, 2023 21:20
@RisingOrange RisingOrange marked this pull request as ready for review November 9, 2023 21:20
@RisingOrange RisingOrange requested a review from a team November 9, 2023 21:46
@RisingOrange RisingOrange requested a review from abdnh November 11, 2023 10:03
already running in parallel, so there is no need to do anything.
This way we can use the same code for all Anki versions.
"""
if ANKI_MINOR < 231000:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌱 Since this is a value used in multiple places, maybe is a good idea to store this in a constant that can be imported by different modules.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedroven
What do you mean? ANKI_MINOR is already a constant (which is defined in ankihub.settings.

Here's a related PR, which renames the constant to ANKI_INT_VERSION make the name more accurate btw: #805

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't talking about the ANKI_MINOR, I'm talking about the value 231000. This value is kind of a magic number, without context is almost impossible to know why this value is important. So I'm suggesting changing this value to a constant that adds some context.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedroven Oh that makes sense! Created a PR for it: #806

@RisingOrange RisingOrange merged commit 5217c8e into main Nov 13, 2023
8 checks passed
@RisingOrange RisingOrange deleted the chore/specify-uses-collection-false-for-background-tasks branch November 13, 2023 16:34
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