Add thread-safe handler initialization using double-checked locking#174
Merged
ImMin5 merged 2 commits intocloudforet-io:masterfrom May 19, 2025
Merged
Add thread-safe handler initialization using double-checked locking#174ImMin5 merged 2 commits intocloudforet-io:masterfrom
ImMin5 merged 2 commits intocloudforet-io:masterfrom
Conversation
…king Signed-off-by: ImMin5 <mino@megazone.com>
There was a problem hiding this comment.
Pull Request Overview
Implements a thread-safe double-checked locking pattern for handler initialization to prevent duplicate setups under concurrent requests.
- Introduce a module-level lock and wrap
_check_init_statein a double-checked lock. - Ensure only one initialization of handlers on simultaneous startup requests.
Comments suppressed due to low confidence (3)
src/spaceone/core/handler/init.py:29
- [nitpick] The lock name
_HANDLER_THREAD_LOCKis generic; consider renaming it to_HANDLER_INIT_LOCKto clarify that it's specifically for handler initialization.
_HANDLER_THREAD_LOCK = threading.Lock()
src/spaceone/core/handler/init.py:148
- [nitpick] Add a brief docstring for
_check_init_stateexplaining the double-checked locking logic to help future maintainers understand the thread-safety mechanism.
def _check_init_state() -> None:
src/spaceone/core/handler/init.py:149
- Consider adding a concurrent unit test that calls
_check_init_statefrom multiple threads to verify_init_handlersis invoked only once under simultaneous access.
if not _HANDLER_INFO["init"]:
Signed-off-by: ImMin5 <mino@megazone.com>
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.
Category
Description
Known issue
When the server starts and both the first and second requests arrive simultaneously, the handler was being initialized twice.
To resolve this, a thread lock was introduced. Only the first and second requests are affected by the lock, and since subsequent requests bypass the initialization logic, there appears to be no performance issue.