Skip to content

Fix spurious "Failed to detach context" error on Execution API disconnects#68039

Open
dheerajturaga wants to merge 1 commit into
apache:mainfrom
dheerajturaga:fix-otel-detach-context-execution-api
Open

Fix spurious "Failed to detach context" error on Execution API disconnects#68039
dheerajturaga wants to merge 1 commit into
apache:mainfrom
dheerajturaga:fix-otel-detach-context-execution-api

Conversation

@dheerajturaga
Copy link
Copy Markdown
Member

@dheerajturaga dheerajturaga commented Jun 4, 2026

The Execution API trace-propagation dependency attaches an OpenTelemetry context before yielding and detaches it afterwards. When a client disconnects or the request is cancelled, the dependency generator is force-closed from a different asyncio task, so the detach ran against a contextvars.Token created in a different Context. OpenTelemetry caught and logged that ValueError at ERROR ("Failed to detach context") before our suppression could see it, producing alarming but harmless log noise on the Dag processor and other clients.

Skip the detach on the GeneratorExit unwind path, where the originating Context is being discarded anyway, and detach only on normal completion.

2026-06-04T19:40:02.380692Z [error    ] Failed to detach context       [opentelemetry.context] correlation_id=019e9426-5bc1-738d-a8a2-9efdfe6738d1 loc=__init__.py:157
Traceback (most recent call last):
  File "/opt/airflow/airflow-core/src/airflow/api_fastapi/execution_api/app.py", line 255, in _extract_w3c_trace_context
    yield
GeneratorExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/python/lib/python3.10/site-packages/opentelemetry/context/__init__.py", line 155, in detach
    _RUNTIME_CONTEXT.detach(token)
  File "/usr/python/lib/python3.10/site-packages/opentelemetry/context/contextvars_context.py", line 53, in detach
    self._current_context.reset(token)
ValueError: <Token var=<ContextVar name='current_context' default={} at 0xffbbf418f0b0> at 0xffbbb9bffdc0> was created in a different Context

Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

ClaudeCode Opus 4.8

@boring-cyborg boring-cyborg Bot added area:API Airflow's REST/HTTP API area:task-sdk labels Jun 4, 2026
Comment thread airflow-core/src/airflow/api_fastapi/execution_api/app.py
Comment thread airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
@dheerajturaga dheerajturaga force-pushed the fix-otel-detach-context-execution-api branch 2 times, most recently from 3501422 to 3da728d Compare June 5, 2026 04:13
…nects

The Execution API trace-propagation dependency attaches an OpenTelemetry
context before yielding and detaches it afterwards. When a client disconnects
or the request is cancelled, the dependency generator is force-closed from a
different asyncio task, so the detach ran against a contextvars.Token created
in a different Context. OpenTelemetry caught and logged that ValueError at
ERROR ("Failed to detach context") before our suppression could see it,
producing alarming but harmless log noise on the Dag processor and other
clients.

Skip the detach only on the GeneratorExit force-close path, where the
originating Context is being discarded anyway. On all same-task unwind paths
-- normal completion and a route handler raising (which FastAPI throws into
the generator at the yield) -- detach as before so the upstream trace context
does not leak into the exception handler, the error response, or error-path
spans. The route-error detach is suppressed so it can never mask the original
exception.
@dheerajturaga dheerajturaga force-pushed the fix-otel-detach-context-execution-api branch from 3da728d to 00a8b23 Compare June 5, 2026 17:31
Comment on lines 254 to +274
try:
yield
finally:
except GeneratorExit:
# Cross-task force-close (client disconnect / request cancellation): the
# finalizer runs in a different asyncio Task — and thus a different
# contextvars.Context — than attach did, so detaching the token would raise
# "Token was created in a different Context" (which OTel logs at ERROR before
# any suppression here could see it). The attached Context is being discarded
# with the dying task, so detaching is unnecessary; skip it and re-raise.
raise
except BaseException:
# A route handler raised: FastAPI throws the exception into this generator at
# the yield, in the SAME task that attach ran in. Detach so the upstream trace
# context does not stay attached for the exception handler, the error response,
# and any spans/logs emitted while unwinding. Suppress any detach error so it
# cannot mask the original exception being propagated.
with contextlib.suppress(Exception):
otel_context.detach(token)
raise
else:
otel_context.detach(token)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you share before/after logs from an actual disconnect? The tests assert detach isn't called, but the spurious line is logged inside OTel's own detach() (except Exception: logger.exception(...)), so I'd like to see it's actually gone from the output.

Minor and optional: rather than splitting on GeneratorExit vs BaseException, you could capture the task at attach and detach only when unwinding in it:

attached_in = asyncio.current_task()
try:
    yield
finally:
    if asyncio.current_task() is attached_in:
        otel_context.detach(token)

Same result, one branch. Not a blocker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API area:task-sdk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants