Skip to content

Commit 2274ba4

Browse files
committed
Add comment explaining the check. Use to ensure that the context is not just equal but is the same object
1 parent fe8b0a5 commit 2274ba4

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

temporalio/contrib/opentelemetry.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,11 @@ async def handle_query(self, input: temporalio.worker.HandleQueryInput) -> Any:
472472
)
473473
return await super().handle_query(input)
474474
finally:
475-
if attach_context == opentelemetry.context.get_current():
475+
# In some exceptional cases this finally is executed with a
476+
# different contextvars.Context than the one the token was created
477+
# on. As such we do a best effort detach to avoid using a mismatched
478+
# token.
479+
if attach_context is opentelemetry.context.get_current():
476480
opentelemetry.context.detach(token)
477481

478482
def handle_update_validator(
@@ -563,10 +567,13 @@ def _top_level_workflow_context(
563567
kind=opentelemetry.trace.SpanKind.INTERNAL,
564568
)
565569

566-
if attach_context == opentelemetry.context.get_current():
570+
# In some exceptional cases this finally is executed with a
571+
# different contextvars.Context than the one the token was created
572+
# on. As such we do a best effort detach to avoid using a mismatched
573+
# token.
574+
if attach_context is opentelemetry.context.get_current():
567575
opentelemetry.context.detach(token)
568576

569-
#
570577
def _context_to_headers(
571578
self, headers: Mapping[str, temporalio.api.common.v1.Payload]
572579
) -> Mapping[str, temporalio.api.common.v1.Payload]:

0 commit comments

Comments
 (0)