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

   ### Apache Airflow version
   
   3.1.4
   
   ### If "Other Airflow 2/3 version" selected, which one?
   
   _No response_
   
   ### What happened?
   
   In Airflow 3.1.4, when attempting to clear task instances using the UI — 
specifically clearing upstream or downstream tasks — the operation fails and 
returns a 500 Internal Server Error from the endpoint:
   
   ```
   POST /api/v2/dags/<dag_id>/clearTaskInstances
   ```
   
   
   
   ## **Actual Behavior**
   
   The API returns **500 Internal Server Error**, and the logs show a 
`NotMapped` exception inside the mapped-operator traversal logic.
   
   Excerpt from logs:
   
   ```
   airflow.exceptions.NotMapped
   ```
   
   Stack trace points to:
   
   ```
   File "airflow/models/taskinstance.py", line 2288, in find_relevant_relatives
     _visit_relevant_relatives_for_mapped(mapped_tasks)
   
   File "airflow/models/mappedoperator.py", line 507
     raise NotMapped()
   ```
   
   Airflow incorrectly tries to call:
   
   ```
   get_mapped_ti_count()
   ```
   
   on a task that is **not** mapped, which triggers the exception.
   
   ---
   
   ## **Relevant Logs (Simplified)**
   
   ```
   POST /api/v2/dags/<dag_id>/clearTaskInstances → 500
   
   File "airflow/models/mappedoperator.py", line 507
     raise NotMapped()
   airflow.exceptions.NotMapped
   ```
   
   Full error path shows the failure originates inside:
   
   ```
   /airflow/api_fastapi/core_api/routes/public/task_instances.py
   ```
   
   while handling:
   
   ```
   find_relevant_relatives()
   ```
   
   ---
   
   ## **Analysis / Root Cause Hypothesis**
   
   `find_relevant_relatives()` always attempts to evaluate mapped-task 
relatives, even when the operator is **not mapped**.
   
   The expected logic should be:
   
   ```python
   if not task.is_mapped:
       skip mapped-task processing
   ```
   
   Instead, it directly calls:
   
   ```
   _visit_relevant_relatives_for_mapped(...)
   ```
   
   which then calls:
   
   ```
   get_mapped_ti_count(...)
   ```
   
   that raises `NotMapped()` when `task.is_mapped` is False.
   
   This results in the 500 error.
   
   ---
   
   ## **Impact**
   
   * Upstream/downstream clearing is **completely broken** in Airflow 3.1.4.
   * UI recovery actions cannot be performed when DAGs are stuck or partially 
executed.
   * Manual database manipulation becomes the only workaround, which is unsafe 
for production environments.
   
   ---
   
   ## **Possible Fix ?**
   
   Add a guard condition before invoking mapped-task logic:
   
   ```python
   if not task.is_mapped:
       return
   ```
   
   Specifically inside:
   
   ```
   find_relevant_relatives()
   ```
   
   or
   
   ```
   _visit_relevant_relatives_for_mapped()
   ```
   
   ---
   
   
   
   ### What you think should happen instead?
   
   Airflow should:
   * Successfully clear the selected task instances.
   * Apply upstream/downstream rules correctly.
   * Return HTTP 200 OK.
   
   Not attempt mapped-task logic on tasks that are not mapped.
   
   ### How to reproduce
   
   1. Open the Airflow UI.
   1. Navigate to any DAG.
   1. Open a DAG Run.
   1. Select a task, then choose Clear → Upstream or Downstream.
   1. The UI does nothing and the backend returns a 500 error.
   
   ### Operating System
   
   Ubuntu 24.04.3 LTS
   
   ### Versions of Apache Airflow Providers
   
   ```                                                                          
                                                                                
                                                                
   Providers info
   airflow-provider-duckdb                   | 0.2.0  
   apache-airflow-providers-amazon           | 9.18.0 
   apache-airflow-providers-celery           | 3.14.0 
   apache-airflow-providers-cncf-kubernetes  | 10.11.0
   apache-airflow-providers-common-compat    | 1.10.0 
   apache-airflow-providers-common-io        | 1.7.0  
   apache-airflow-providers-common-messaging | 2.0.1  
   apache-airflow-providers-common-sql       | 1.30.0 
   apache-airflow-providers-docker           | 4.5.0  
   apache-airflow-providers-elasticsearch    | 6.4.0  
   apache-airflow-providers-fab              | 3.0.3  
   apache-airflow-providers-ftp              | 3.14.0 
   apache-airflow-providers-git              | 0.1.0  
   apache-airflow-providers-google           | 19.1.0 
   apache-airflow-providers-grpc             | 3.9.0  
   apache-airflow-providers-hashicorp        | 4.4.0  
   apache-airflow-providers-http             | 5.6.0  
   apache-airflow-providers-microsoft-azure  | 12.9.0 
   apache-airflow-providers-mysql            | 6.4.0  
   apache-airflow-providers-odbc             | 4.11.0 
   apache-airflow-providers-openlineage      | 2.9.0  
   apache-airflow-providers-postgres         | 6.5.0  
   apache-airflow-providers-redis            | 4.4.0  
   apache-airflow-providers-salesforce       | 5.12.0 
   apache-airflow-providers-sendgrid         | 4.2.0  
   apache-airflow-providers-sftp             | 5.5.0  
   apache-airflow-providers-slack            | 9.6.0  
   apache-airflow-providers-smtp             | 2.4.0  
   apache-airflow-providers-snowflake        | 6.7.0  
   apache-airflow-providers-ssh              | 4.2.0  
   apache-airflow-providers-standard         | 1.10.0 
   ```
   
   ### Deployment
   
   Docker-Compose
   
   ### Deployment details
   
   ```
   Apache Airflow
   version                | 3.1.4                                               
  
   executor               | CeleryExecutor                                      
  
   task_logging_handler   | airflow.utils.log.file_task_handler.FileTaskHandler 
  
   sql_alchemy_conn       | 
postgresql+psycopg2://airflow:airflow@postgres/airflow
   dags_folder            | /opt/airflow/dags                                   
  
   plugins_folder         | /opt/airflow/plugins                                
  
   base_log_folder        | /opt/airflow/logs                                   
  
   remote_base_log_folder |                                                     
  
   ```                                                                          
   
   
   ```
   System info
   OS              | Linux                                                      
                                                                                
                                 
   architecture    | x86_64                                                     
                                                                                
                                 
   uname           | uname_result(system='Linux', node='02adf0a80285', 
release='6.14.0-36-generic', version='#36~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC 
Wed Oct 15 15:45:17 UTC 2', machine='x86_64')
   locale          | ('C', 'UTF-8')                                             
                                                                                
                                 
   python_version  | 3.12.12 (main, Dec 10 2025, 07:42:02) [GCC 12.2.0]         
                                                                                
                                 
   python_location | /usr/python/bin/python3.12                                 
                                                                                
                                 
   ```                                                                          
                                                                                
                                    
   
   ```
   Tools info
   git             | git version 2.39.5                                         
                                    
   ssh             | OpenSSH_9.2p1 Debian-2+deb12u7, OpenSSL 3.0.17 1 Jul 2025  
                                    
   kubectl         | NOT AVAILABLE                                              
                                    
   gcloud          | NOT AVAILABLE                                              
                                    
   cloud_sql_proxy | NOT AVAILABLE                                              
                                    
   mysql           | mysql  Ver 15.1 Distrib 10.11.15-MariaDB, for 
debian-linux-gnu (x86_64) using  EditLine wrapper
   sqlite3         | 3.40.1 2022-12-28 14:03:47 
df5c253c0b3dd24916e4ec7cf77d3db5294cc9fd45ae7b9c5e82ad8197f3alt1    
   psql            | psql (PostgreSQL) 18.1 (Debian 18.1-1.pgdg12+2)            
                                    
   ```
                                                                                
                                    
   ```
   Paths info
   airflow_home    | /opt/airflow                                               
                                                                                
                                                                                
                             
   system_path     | 
/root/bin:/home/airflow/.local/bin:/usr/python/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
                                                                                
                                                         
   python_path     | 
/home/airflow/.local/bin:/opt/airflow/dags:/usr/python/lib/python312.zip:/usr/python/lib/python3.12:/usr/python/lib/python3.12/lib-dynload:/home/airflow/.local/lib/python3.12/site-packages:/usr/python/lib/python3.12/site-packages:/opt/airflow/confi
                   | g:/opt/airflow/plugins                                     
                                                                                
                                                                                
                             
   airflow_on_path | True                                                       
                                                                                
                                                                                
                                      ```
   
   ### Anything else?
   
   _No response_
   
   ### Are you willing to submit PR?
   
   - [x] 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