Skip to content
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

fix: make global hooks thread safe #331

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions openfeature/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
import typing

from openfeature import _event_support
Expand Down Expand Up @@ -30,6 +31,7 @@

_evaluation_context = EvaluationContext()

_hooks_lock = threading.Lock()
_hooks: typing.List[Hook] = []


Expand Down Expand Up @@ -58,7 +60,6 @@ def get_provider_metadata(domain: typing.Optional[str] = None) -> Metadata:


def get_evaluation_context() -> EvaluationContext:
global _evaluation_context
return _evaluation_context


Expand All @@ -70,18 +71,18 @@ def set_evaluation_context(evaluation_context: EvaluationContext) -> None:


def add_hooks(hooks: typing.List[Hook]) -> None:
global _hooks
_hooks = _hooks + hooks
beeme1mr marked this conversation as resolved.
Show resolved Hide resolved
with _hooks_lock:
_hooks.extend(hooks)


def clear_hooks() -> None:
global _hooks
_hooks = []
with _hooks_lock:
_hooks.clear()


def get_hooks() -> typing.List[Hook]:
global _hooks
return _hooks
with _hooks_lock:
return _hooks


def shutdown() -> None:
Expand Down