-
Notifications
You must be signed in to change notification settings - Fork 0
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
fix: Database is locked using RWLock #817
Merged
Merged
Conversation
This file contains 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
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #817 +/- ##
==========================================
+ Coverage 84.89% 85.12% +0.23%
==========================================
Files 64 65 +1
Lines 5913 5931 +18
==========================================
+ Hits 5020 5049 +29
+ Misses 893 882 -11 ☔ View full report in Codecov by Sentry. |
1ed038a
to
4906596
Compare
4906596
to
9ff2863
Compare
e73d613
to
c711334
Compare
pedroven
previously approved these changes
Dec 27, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
pedroven
approved these changes
Dec 29, 2023
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.
Sometimes when the add-on tries to access its database, it fails with
OperationalError
database is locked
. An error message is shown to the user in this case and the current operation fails when this happens.This is one of most frequent errors on Sentry.
OperationalError: database is locked
: https://ankihub.sentry.io/issues/4632925659/?project=6546414User error report: https://community.ankihub.net/t/freeze-while-syncing/105528
The reason for the error is that a thread tries to access the AnkiHub DB through the
_AnkiHubDB
class while the database is attached to the Anki's DB connection. This mostly works on MacOs and Linux, but not on Windows, where the error is raised.This error is related:
DBError: Anki already open, or media currently syncing.
: https://ankihub.sentry.io/issues/4574509733/?project=6546414It is not shown to the user and handled silently instead (but when this happens we don't send user review data to the server). It happens when trying to attach the ankihub database to Anki's db connection while a connection to the ankihub database is open. This also seems to nearly exclusively happen on Windows. This error is also one of the most frequent ones on Sentry.
Related issues
Fix “database is locked” error
Proposed changes
We want a thread to have exclusive access to the AnkiHub database while it is attached to the Anki database. We also want threads to be able to concurrently access the AnkiHub database otherwise.
This can be solved using thread locks:
attached_ankihub_db
context manager. This PR modifies it and adds an andetached_ankihub_db
context manager.detached_ankihub_db
context managerdetached_ankihub_db
context managerdetached_ankihub_db
context concurrentlyattached_ankihub_db
context, no other thread can enter anattached_ankihub_db
ordetached_ankihub_db
contextTo make sure that the locks are used correctly, the
db
module no longer exposesattach_ankihub_db_to_anki_db_connection
anddetach_ankihub_db_from_anki_db_connection
. Instead code outside the module can use theattached_ankihub_db
anddetached_ankihub_db
context managers.There was one feature which depended on the usage of
attach_ankihub_db_to_anki_db_connection
anddetach_ankihub_db_from_anki_db_connection
which can't be implemented using the context managers. This was the sorting capability of theEditedAfterSyncColumn
. It can't be implemented using the context manager because for the query used for sorting we are passing the query as a string to Anki and don't control in which context the query is called. However this is not an important feature and probably very few people have used it.How to reproduce the errors
I reproduced the errors on Windows by running the following snippets from Anki’s debug console:
database is locked
It fails after 5 seconds with sqlite3.OperationalError: database is locked.
When I remove the
#
in front of the line that detaches the db, the error doesn’t occur.Anki already open, or media currently syncing
Resources
https://github.com/elarivie/pyReaderWriterLock