diff --git a/packages/service-library/src/servicelib/tracing.py b/packages/service-library/src/servicelib/tracing.py index 6635e824a46..e1b3b348a72 100644 --- a/packages/service-library/src/servicelib/tracing.py +++ b/packages/service-library/src/servicelib/tracing.py @@ -21,13 +21,14 @@ def get_context() -> TracingContext: @contextmanager def use_tracing_context(context: TracingContext): + token: object | None = None if context is not None: - otcontext.attach(context) + token = otcontext.attach(context) try: yield finally: - if context is not None: - otcontext.detach(context) + if token is not None: + otcontext.detach(token) def setup_log_tracing(tracing_settings: TracingSettings):