diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index d170089812e..2b04bb37078 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -44,32 +44,32 @@ def wrapper( # type: ignore[misc] **kwargs: typing.Dict[typing.Any, typing.Any], ) -> typing.Optional[typing.Any]: global _RUNTIME_CONTEXT # pylint: disable=global-statement - - with _RUNTIME_CONTEXT_LOCK: - if _RUNTIME_CONTEXT is None: - # FIXME use a better implementation of a configuration manager - # to avoid having to get configuration values straight from - # environment variables - default_context = "contextvars_context" - - configured_context = environ.get( - OTEL_PYTHON_CONTEXT, default_context - ) # type: str - try: - - _RUNTIME_CONTEXT = next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore - group="opentelemetry_context", - name=configured_context, + if _RUNTIME_CONTEXT is None: + with _RUNTIME_CONTEXT_LOCK: + if _RUNTIME_CONTEXT is None: + # FIXME use a better implementation of a configuration manager + # to avoid having to get configuration values straight from + # environment variables + default_context = "contextvars_context" + + configured_context = environ.get( + OTEL_PYTHON_CONTEXT, default_context + ) # type: str + try: + + _RUNTIME_CONTEXT = next( # type: ignore + iter( # type: ignore + entry_points( # type: ignore + group="opentelemetry_context", + name=configured_context, + ) ) - ) - ).load()() + ).load()() - except Exception: # pylint: disable=broad-except - logger.exception( - "Failed to load context: %s", configured_context - ) + except Exception: # pylint: disable=broad-except + logger.exception( + "Failed to load context: %s", configured_context + ) return func(*args, **kwargs) # type: ignore[misc] return typing.cast(_F, wrapper) # type: ignore[misc]