kalluripradeep commented on PR #67372:
URL: https://github.com/apache/airflow/pull/67372#issuecomment-4772968963

   Thanks for the detailed review — you're exactly right.
   
   The root cause is in `fetch_handle_failure_context()` (line 1809 in 
`taskinstance.py`):
   
   ```python
   # Before
   if ti.state == TaskInstanceState.RUNNING:
       ti.prepare_db_for_next_try(session)
   ```
   
   When the executor sends a FAILED event for a QUEUED/SCHEDULED/DEFERRED TI, 
`handle_failure()` is called but `ti.state != RUNNING`, so 
`prepare_db_for_next_try()` — and therefore `TaskInstanceHistory.record_ti()` — 
was never invoked. History lost, hostname gone.
   
   Fixed by changing the guard to exclude only `RESTARTING` (which already had 
`prepare_db_for_next_try` called during the clear operation):
   
   ```python
   # After
   if ti.state != TaskInstanceState.RESTARTING:
       # Covers RUNNING (normal exception), QUEUED/SCHEDULED/DEFERRED (external 
kill)
       ti.prepare_db_for_next_try(session)
   ```
   
   Added a parametrized regression test 
`test_process_executor_events_queued_ti_retry_preserves_history` covering both 
`QUEUED` and `SCHEDULED` states — verifies that after a FAILED executor event: 
TI gets a new UUID (confirming `prepare_db_for_next_try` ran), a 
`TaskInstanceHistory` row exists, and `hostname`/`start_date` are preserved.


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