-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(keyring): make keyring unlock thread safe #10062
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import functools | ||
| import threading | ||
|
|
||
| from typing import TYPE_CHECKING | ||
| from typing import TypeVar | ||
| from typing import overload | ||
| from weakref import WeakKeyDictionary | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any | ||
| from typing import Callable | ||
|
|
||
|
|
||
| T = TypeVar("T") | ||
| C = TypeVar("C", bound=object) | ||
|
|
||
|
|
||
| class AtomicCachedProperty(functools.cached_property[T]): | ||
| def __init__(self, func: Callable[[C], T]) -> None: | ||
| super().__init__(func) | ||
| self._semaphore = threading.BoundedSemaphore() | ||
| self._locks: WeakKeyDictionary[object, threading.Lock] = WeakKeyDictionary() | ||
|
|
||
| @overload | ||
| def __get__( | ||
| self, instance: None, owner: type[Any] | None = ... | ||
| ) -> AtomicCachedProperty[T]: ... | ||
| @overload | ||
| def __get__(self, instance: object, owner: type[Any] | None = ...) -> T: ... | ||
|
|
||
| def __get__( | ||
| self, instance: C | None, owner: type[Any] | None = None | ||
| ) -> AtomicCachedProperty[T] | T: | ||
| # If there's no instance, return the descriptor itself | ||
| if instance is None: | ||
| return self | ||
|
|
||
| if instance not in self._locks: | ||
| with self._semaphore: | ||
| # we double-check the lock has not been created by another thread | ||
| if instance not in self._locks: | ||
| self._locks[instance] = threading.Lock() | ||
|
|
||
| # Use a thread-safe lock to ensure the property is computed only once | ||
| with self._locks[instance]: | ||
| return super().__get__(instance, owner) | ||
|
|
||
|
|
||
| def atomic_cached_property(func: Callable[[C], T]) -> AtomicCachedProperty[T]: | ||
| """ | ||
| A thread-safe implementation of functools.cached_property that ensures lazily-computed | ||
| properties are calculated only once, even in multithreaded environments. | ||
|
|
||
| This property decorator works similar to functools.cached_property but employs | ||
| thread locks and a bounded semaphore to handle concurrent access safely. | ||
|
|
||
| The computed value is cached on the instance itself and is reused for subsequent | ||
| accesses unless explicitly invalidated. The added thread-safety makes it ideal for | ||
| situations where multiple threads might access and compute the property simultaneously. | ||
|
|
||
| Note: | ||
| - The cache is stored in the instance dictionary just like `functools.cached_property`. | ||
|
|
||
| :param func: The function to be turned into a thread-safe cached property. | ||
| """ | ||
| return AtomicCachedProperty(func) |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.