-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Database is locked using RWLock (#817)
* Vendor `readerwriterlock` * Use rw lock to prevent db errors * Use multiple read locks, not just one global one * Use read lock in the DBConnection * Refactor * Edit docstring * Raise `LockAcquisitionTimeoutError` * Release write lock after detaching * Remove sorting capability for `EditedAfterSyncColumn` * Use lock in context manager * Create `write_lock_context` and `read_lock_context` * Use `detached_ankihub_db` in `errors.py` * Extract timeout constant * Edit docstrings * Refactor * Reduce log level * Add tests * Edit test * Edit tests to make them work on all Anki versions * Edit tests * Lock on transaction level * fix: Dont commit if there was an exception in the context * Adjust code for the not-context manager-case * Rollback explicitly * Add test * Add tests * Add docstrings * ref: Rename to `thread_safety_context_func`
- Loading branch information
1 parent
f95b9fb
commit 2ace8f7
Showing
16 changed files
with
1,720 additions
and
104 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
from .db import ( # noqa: F401 | ||
ankihub_db, | ||
attach_ankihub_db_to_anki_db_connection, | ||
attached_ankihub_db, | ||
detach_ankihub_db_from_anki_db_connection, | ||
detached_ankihub_db, | ||
is_ankihub_db_attached_to_anki_db, | ||
) |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""This module defines a readers-writer lock for the AnkiHub DB. | ||
Multiple threads can enter the non_exclusive_db_access_context() context, but when | ||
a thread enters the exclusive_db_access_context() context, no other thread can enter | ||
either context until the thread that entered the exclusive_db_access_context() context | ||
exits it. | ||
""" | ||
from contextlib import contextmanager | ||
|
||
from readerwriterlock import rwlock | ||
|
||
from .. import LOGGER | ||
from .exceptions import LockAcquisitionTimeoutError | ||
|
||
LOCK_TIMEOUT_SECONDS = 5 | ||
|
||
rw_lock = rwlock.RWLockFair() | ||
write_lock = rw_lock.gen_wlock() | ||
|
||
|
||
@contextmanager | ||
def exclusive_db_access_context(): | ||
if write_lock.acquire(blocking=True, timeout=LOCK_TIMEOUT_SECONDS): | ||
LOGGER.debug("Acquired exclusive access.") | ||
try: | ||
yield | ||
finally: | ||
write_lock.release() | ||
LOGGER.debug("Released exclusive access.") | ||
else: | ||
raise LockAcquisitionTimeoutError("Could not acquire exclusive access.") | ||
|
||
|
||
@contextmanager | ||
def non_exclusive_db_access_context(): | ||
lock = rw_lock.gen_rlock() | ||
if lock.acquire(blocking=True, timeout=LOCK_TIMEOUT_SECONDS): | ||
LOGGER.debug("Acquired non-exclusive access.") | ||
try: | ||
yield | ||
finally: | ||
lock.release() | ||
LOGGER.debug("Released non-exclusive access.") | ||
else: | ||
raise LockAcquisitionTimeoutError("Could not acquire non-exclusive access.") |
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
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
Oops, something went wrong.