-
Notifications
You must be signed in to change notification settings - Fork 17
first cut at proper read/write locking for KeyBundle #83
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
first cut at proper read/write locking for KeyBundle #83
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #83 +/- ##
===========================================
+ Coverage 77.50% 77.60% +0.10%
===========================================
Files 41 41
Lines 4276 4296 +20
Branches 832 833 +1
===========================================
+ Hits 3314 3334 +20
Misses 700 700
Partials 262 262
Continue to review full report at Codecov.
|
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.
Missed writer method: do_keys
Missed reader methods: contains, iter, len, copy
Maybe iter should work on a copy to avoid errors.
src/cryptojwt/key_bundle.py
Outdated
_typs = [typ.lower(), typ.upper()] | ||
_keys = [k for k in self._keys if k.kty in _typs] | ||
else: | ||
_keys = self._keys |
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.
Need a shallow copy here
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.
src/cryptojwt/key_bundle.py
Outdated
if update: | ||
self._uptodate() | ||
with self._lock_reader: | ||
return self._keys |
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.
Need a shallow copy here. Optimization, save the copy and remove copy every time keys_writer() is called.
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.
Well since the name doesn't start with "_" it suggest that it can be called from the outside. Suggestion, make it private. The method load() calls do_keys() with no locking. |
- create public version of do_keys (with write lock)
KeyBundle is tricky as it updates its own keys from multiple places in the code. This PR uses readwriterlock to put a read lock around key reads and a write lock updates.
The write lock is implemented using a decorator, whereas the read lock implemented using explicit
with
. Most key access has been moved tokeys()
in order to simplify the code.There may be dragons - please review carefully. Extra set of eyes from @janste63 would be most useful.