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

   ### Under which category would you file this issue?
   
   Airflow Core
   
   ### Apache Airflow version
   
   main (`8edc9c1136`), also present in 3.4.0
   
   ### What happened and how to reproduce it?
   
   Clearing a task instance or a Dag run discards the task's state store 
entries unless `keep_task_state` is set, so that the next attempt starts over 
instead of resuming from a stale checkpoint (#72100, #76806). The discard only 
deletes rows of the `task_state_store` table. It never calls the backend 
configured as `[state_store] backend`:
   
   
https://github.com/apache/airflow/blob/8edc9c1136b81ea3d1ea78050307bc5c8338e9a4/airflow-core/src/airflow/models/taskinstance.py#L687-L709
   
   The worker's reads and writes do go through the configured backend:
   
   
https://github.com/apache/airflow/blob/8edc9c1136b81ea3d1ea78050307bc5c8338e9a4/airflow-core/src/airflow/api_fastapi/execution_api/routes/task_state_store.py#L69-L77
   
   So with a custom `[state_store] backend` that keeps its values outside the 
metadata database, a clear reports success, deletes nothing the backend wrote, 
and the next attempt reads the state of the cleared attempt. A resumable task 
then returns its old result, and a checkpointing task resumes where the cleared 
attempt stopped.
   
   The docstring of `_discard_task_state_store` says the backend is not 
touched, but the documentation of the clear does not: it says that clearing 
"discards them by default unless `keep_task_state` is set".
   
   
https://github.com/apache/airflow/blob/8edc9c1136b81ea3d1ea78050307bc5c8338e9a4/airflow-core/docs/administration-and-deployment/task-and-asset-state-store.rst?plain=1#L63-L65
   
   A script that shows the discard never reaches the configured backend:
   
   ```python
   """A clear only deletes metadata DB rows: a custom ``[state_store] backend`` 
is never asked to clear."""
   
   from __future__ import annotations
   
   import uuid
   from types import SimpleNamespace
   from unittest import mock
   
   import airflow.state
   from airflow.models import taskinstance
   
   calls: list[tuple] = []
   
   
   class CustomBackend:
       """Stands for a ``[state_store] backend`` that keeps task state outside 
the metadata DB."""
   
       def clear(self, scope, **kwargs):
           calls.append(("clear", scope))
   
       def delete(self, scope, key, **kwargs):
           calls.append(("delete", scope, key))
   
   
   session = mock.MagicMock()
   ti = SimpleNamespace(dag_id="d", run_id="r", task_id="t", 
region_id=uuid.UUID(int=0), region_index=None)
   with mock.patch.object(airflow.state, "get_state_backend", 
return_value=CustomBackend()):
       # What clear_task_instances() calls unless keep_task_state is set.
       taskinstance._discard_task_state_store([ti], session=session)
   
   print("statements on the metadata DB:", [str(c.args[0]).split()[0] for c in 
session.execute.call_args_list])
   print("calls reaching the configured backend:", calls)
   ```
   
   Output:
   
   ```
   statements on the metadata DB: ['DELETE']
   calls reaching the configured backend: []
   ```
   
   ### What you think should happen instead?
   
   A clear without `keep_task_state` should leave no task state for the next 
attempt to read, whichever backend holds it. Two ways were named in the review 
of #72100 
([r4121935386](https://github.com/apache/airflow/pull/72100#discussion_r4121935386)):
 discard through `get_state_backend()`, as the clear-on-success path does, or 
refuse a clear without `keep_task_state` while a backend other than the 
metastore one is configured.
   
   Until one of them lands, the documentation of the clear could say that the 
discard only covers the default backend.
   
   ### Operating System
   
   Ubuntu 24.04 (WSL2), Python 3.12.3
   
   ### Deployment
   
   Other
   
   ### Apache Airflow Provider(s)
   
   _No response_
   
   ### Versions of Apache Airflow Providers
   
   _No response_
   
   ### Official Helm Chart version
   
   Not Applicable
   
   ### Kubernetes Version
   
   Not Applicable
   
   ### Helm Chart configuration
   
   Not Applicable
   
   ### Docker Image customizations
   
   Not Applicable
   
   ### Anything else?
   
   This was confirmed as a gap when the discard was added ([reply in 
#72100](https://github.com/apache/airflow/pull/72100#discussion_r4122615506)), 
with the fix to be tracked separately. I found no issue for it, so this one is 
meant as that place.
   
   The same discard leaves the payload of a custom `[workers] 
state_store_backend` behind. That case is different: its reference row in the 
database is deleted, so the next attempt does start over, and only the stored 
object is orphaned.
   
   ### 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