[BUG] Fix thread-unsafe singleton initialization in factory functions#82
Open
direkkakkar319-ops wants to merge 5 commits into
Open
[BUG] Fix thread-unsafe singleton initialization in factory functions#82direkkakkar319-ops wants to merge 5 commits into
direkkakkar319-ops wants to merge 5 commits into
Conversation
Author
|
Hey! @Shashankss1205 , This PR fixes a race condition in all five singleton factory functions. Since f |
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
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.
What does this implement/fix? Explain your changes.
All five singleton factory functions (get_registry, get_handle_manager, get_executor, get_tag_resolver, get_composition_validator) used a simple if None check with no locking, making them unsafe under concurrent access. When fit_predict_async offloads work to thread pool executors via loop.run_in_executor(), multiple threads can race through the if _instance is None check simultaneously, creating duplicate instances and leading to inconsistent state (e.g., duplicate registry loads, split handle stores).
This PR applies the double-checked locking pattern to all five singletons using threading.Lock
import threadingadded toimportsthreading.Lock()and the double-checked locking pattern in thefactory functionDoes your contribution introduce a new dependency? If yes, which one?
No. threading is part of the Python standard library.
What should a reviewer concentrate their feedback on?
Any other comments?
Double-checked locking was used instead of
functools.lru_cache(maxsize=1)because it keeps the existing global pattern (so reset/tests stay simple), avoidscache-related overheadandGCcomplications, and ensures the fast path has no lock overhead—just a quick None check.PR checklist
For all contributions
For new estimators
None
pytestpass locallylint pass locally
