HsiuChuanHsu commented on PR #55159:
URL: https://github.com/apache/airflow/pull/55159#issuecomment-3245703085

   When implementing unit tests for the new orphaned task detection logic in 
the `fetch_handle_failure_context` method of `taskinstance.py`, a critical 
timing issue was discovered that prevented the logic from functioning correctly.
   
   
https://github.com/apache/airflow/blob/bcf51254380209a9c2e260d0cf9407964ba9d012/airflow-core/src/airflow/models/taskinstance.py#L1585
   
   **Original problem**
   - The orphaned task detection relies on the condition: `ti.end_date is None`
   - However, `ti.end_date = timezone.utcnow()` was executed earlier in the 
method
   - This made the `ti.end_date is None` condition impossible to satisfy
   ```
   def fetch_handle_failure_context(...):
       # ... other code ...
       ti.end_date = timezone.utcnow()  # ← Sets end_date first
       
       # ... later in the method ...
       if ti.state is None and ti.start_date is not None and ti.end_date is 
None:
           # ← This condition can never be True because end_date was already 
set above
   
   ```
   
   
   
   **Solution**
   The orphaned task detection logic was moved to execute before the `end_date` 
assignment:
   ```
   def fetch_handle_failure_context(...):
       # Check for orphaned task BEFORE setting end_date
       if (
           ti.is_eligible_to_retry()
           and ti.state is None
           and ti.start_date is not None
           and ti.end_date is None  # ← Now this can actually be True
       ):
           # Handle orphaned task detection and history recording
       
       # THEN set end_date
       ti.end_date = timezone.utcnow()
   ```
   


-- 
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