Attach OpenTelemetry contexts so that the matching detach is always safe.
| Function | attached |
Attach context for the block and detach it afterwards where possible. |
| Function | _detach |
Detach context if it is current and token is valid here. |
Attach context for the block and detach it afterwards where possible.
None attaches nothing. The block's finally can run in a different
contextvars.Context than the one that attached: a context manager
abandoned by an evicted workflow is finalized wherever garbage collection
happens to run. The token is not valid there, and
opentelemetry.context.detach would log "Failed to detach context" even
though there is nothing to detach. Checking that the attached context is
still current does not catch every such case, because OpenTelemetry's
threading instrumentation (enabled by strands, among others) propagates
the same Context object into new threads. The thread is no test
either: workflow activations move between pool threads while the asyncio
task keeps its contextvars.Context, and those detaches must happen.
Only contextvars knows which Context a token belongs to, so
_detach performs the reset detach performs and ignores the
ValueError raised for a token from another Context.