aeroyorch opened a new issue, #72338:
URL: https://github.com/apache/airflow/issues/72338

   ### Under which category would you file this issue?
   
   Airflow Core
   
   ### Apache Airflow version
   
   3.2.2
   
   ### What happened and how to reproduce it?
   
   After a few tasks were removed from a DAG, "Mark failed" on an older run 
returned HTTP 500:
   
   ```
   File ".../api/common/mark_tasks.py", line 344, in set_dag_run_state_to_failed
       pending_normal_tis = [ti for ti in pending_tis if not 
dag.task_dict[ti.task_id].is_teardown]
   KeyError: '<task removed in a later DAG version>'
   ```
   
   `pending_tis` is queried without restricting to the DAG's task ids, so it 
picks up TI rows for
   tasks the new version no longer has, and the `task_dict` lookup blows up. 
`running_tis`, built a few
   lines above, does apply that filter.
   
   The asymmetry is still there on `main`, where the code has since been 
refactored into
   `_set_dag_run_terminal_state`: `running_tis` has 
`TaskInstance.task_id.in_(task_ids)`,
   `pending_tis` does not, and the unguarded lookup is at line 300. So it 
survived that refactor.
   
   ### What you think should happen instead?
   
   The two queries should be consistent, and a leftover task instance should be 
ignored rather than raise.
   The scheduler already copes with this: once a run starts, `verify_integrity` 
marks such rows `removed`.
   
   ### Operating System
   
   _No response_
   
   ### Deployment
   
   None
   
   ### Apache Airflow Provider(s)
   
   _No response_
   
   ### Versions of Apache Airflow Providers
   
   _No response_
   
   ### Official Helm Chart version
   
   Not Applicable
   
   ### Kubernetes Version
   
   _No response_
   
   ### Helm Chart configuration
   
   _No response_
   
   ### Docker Image customizations
   
   _No response_
   
   ### Anything else?
   
   **Suggested fix**
   
   Add `TaskInstance.task_id.in_(task_ids)` to the `pending_tis` query, 
mirroring `running_tis`.
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


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