dheerajturaga commented on code in PR #68039:
URL: https://github.com/apache/airflow/pull/68039#discussion_r3376877774


##########
airflow-core/src/airflow/api_fastapi/execution_api/app.py:
##########
@@ -253,9 +253,25 @@ async def _extract_w3c_trace_context(
     token = otel_context.attach(ctx)
     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)

Review Comment:
   `2026-06-08T22:52:55.543527Z [info     ] get next_dagrun_info_v2        
[airflow.serialization.definitions.dag] last_automated_run_info=None 
loc=dag.py:432 next_info=None
   2026-06-08T22:52:55.543726Z [info     ] setting next dagrun info       
[airflow.models.dag] loc=dag.py:795 next_dagrun=None 
next_dagrun_create_after=None next_dagrun_data_interval_end=None 
next_dagrun_data_interval_start=None next_dagrun_partition_date=None 
next_dagrun_partition_key=None
   2026-06-08T22:52:55.590289Z [info     ] Bulk-writing dags to db        
[airflow.serialization.definitions.dag] count=1 loc=dag.py:201
   2026-06-08T22:52:55.595287Z [info     ] Getting latest run for 
non-partitioned Dag [airflow.dag_processing.collection] 
dag_id=example_branch_operator loc=collection.py:200
   2026-06-08T22:52:55.596694Z [info     ] no latest run found            
[airflow.dag_processing.collection] dag_id=example_branch_operator 
loc=collection.py:210
   2026-06-08T22:52:55.602863Z [info     ] get next_dagrun_info_v2        
[airflow.serialization.definitions.dag] last_automated_run_info=None 
loc=dag.py:432 next_info=DagRunInfo(run_after=DateTime(2026, 6, 8, 0, 0, 0, 
tzinfo=Timezone('UTC')), data_interval=DataInterval(start=DateTime(2026, 6, 8, 
0, 0, 0, tzinfo=Timezone('UTC')), end=DateTime(2026, 6, 8, 0, 0, 0, 
tzinfo=Timezone('UTC'))), partition_date=None, partition_key=None)
   2026-06-08T22:52:55.603133Z [info     ] setting next dagrun info       
[airflow.models.dag] loc=dag.py:795 next_dagrun='2026-06-08 00:00:00+00:00' 
next_dagrun_create_after='2026-06-08 00:00:00+00:00' 
next_dagrun_data_interval_end='2026-06-08 00:00:00+00:00' 
next_dagrun_data_interval_start='2026-06-08 00:00:00+00:00' 
next_dagrun_partition_date=None next_dagrun_partition_key=None
   2026-06-08T22:52:55.687424Z [info     ] Bulk-writing dags to db        
[airflow.serialization.definitions.dag] count=1 loc=dag.py:201
   2026-06-08T22:52:55.691843Z [info     ] Getting latest run for 
non-partitioned Dag [airflow.dag_processing.collection] 
dag_id=example_branch_dop_operator_v3 loc=collection.py:200
   2026-06-08T22:52:55.693182Z [info     ] no latest run found            
[airflow.dag_processing.collection] dag_id=example_branch_dop_operator_v3 
loc=collection.py:210
   2026-06-08T22:52:55.698828Z [info     ] get next_dagrun_info_v2        
[airflow.serialization.definitions.dag] last_automated_run_info=None 
loc=dag.py:432 next_info=DagRunInfo(run_after=DateTime(2026, 6, 8, 22, 52, 0, 
tzinfo=Timezone('UTC')), data_interval=DataInterval(start=DateTime(2026, 6, 8, 
22, 52, 0, tzinfo=Timezone('UTC')), end=DateTime(2026, 6, 8, 22, 52, 0, 
tzinfo=Timezone('UTC'))), partition_date=None, partition_key=None)
   2026-06-08T22:52:55.698983Z [info     ] setting next dagrun info       
[airflow.models.dag] loc=dag.py:795 next_dagrun='2026-06-08 22:52:00+00:00' 
next_dagrun_create_after='2026-06-08 22:52:00+00:00' 
next_dagrun_data_interval_end='2026-06-08 22:52:00+00:00' 
next_dagrun_data_interval_start='2026-06-08 22:52:00+00:00' 
next_dagrun_partition_date=None next_dagrun_partition_key=None
   2026-06-08T22:52:56.401831Z [error    ] Failed to detach context       
[opentelemetry.context] correlation_id=019ea970-66c8-7adf-adad-a0f0849133ff 
loc=__init__.py:145
   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 143, in detach
       _RUNTIME_CONTEXT.detach(token)
     File 
"/usr/python/lib/python3.10/site-packages/opentelemetry/context/contextvars_context.py",
 line 42, in detach
       self._current_context.reset(token)
   ValueError: <Token var=<ContextVar name='current_context' default={} at 
0xff05cd0043b0> at 0xff05960222c0> was created in a different Context
   2026-06-08T22:52:56.407571Z [info     ] Not time to refresh bundle 
dags-folder [airflow.dag_processing.manager.DagFileProcessorManager] 
loc=manager.py:794
   2026-06-08T22:52:56.408004Z [info     ] Not time to refresh bundle 
example_dags [airflow.dag_processing.manager.DagFileProcessorManager] 
loc=manager.py:794
   2026-06-08T22:52:56.509301Z [info     ] Bulk-writing dags to db        
[airflow.serialization.definitions.dag] count=1 loc=dag.py:201
   2026-06-08T22:52:56.512531Z [info     ] get next_dagrun_info_v2        
[airflow.serialization.definitions.dag] last_automated_run_info=None 
loc=dag.py:432 next_info=None
   2026-06-08T22:52:56.512802Z [info     ] setting next dagrun info       
[airflow.models.dag] loc=dag.py:795 next_dagrun=None 
next_dagrun_create_after=None next_dagrun_data_interval_end=None 
next_dagrun_data_interval_start=None next_dagrun_partition_date=None 
next_dagrun_partition_key=None
   2026-06-08T22:52:56.587047Z [info     ] Bulk-writing dags to db        
[airflow.serialization.definitions.dag] count=1 loc=dag.py:201`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to