Skip to content

Add thread-safe handler initialization using double-checked locking #174

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 2 commits into from
May 19, 2025

Conversation

ImMin5
Copy link
Member

@ImMin5 ImMin5 commented May 19, 2025

Category

  • New feature
  • Bug fix
  • Improvement
  • Refactor
  • etc

Description

  • Add thread-safe handler initialization using double-checked locking

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.

…king

Signed-off-by: ImMin5 <mino@megazone.com>
@ImMin5 ImMin5 requested a review from Copilot May 19, 2025 02:36
@ImMin5 ImMin5 self-assigned this May 19, 2025
@ImMin5 ImMin5 added the enhancement New feature or request label May 19, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_state in 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_LOCK is generic; consider renaming it to _HANDLER_INIT_LOCK to 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_state explaining 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_state from multiple threads to verify _init_handlers is invoked only once under simultaneous access.
if not _HANDLER_INFO["init"]:

@ImMin5 ImMin5 merged commit cea4921 into cloudforet-io:master May 19, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pass/signedoff
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant