surawut-jirasaktavee opened a new issue, #74750:
URL: https://github.com/apache/airflow/issues/74750

   ### Under which category would you file this issue?
   
   Task SDK
   
   ### Apache Airflow version
   
   3.3.1 (also reproduced on `main` at 22f86cb551)
   
   ### What happened and how to reproduce it?
   
   A TaskFlow function decorated with `@teardown` can only be used once per 
Dag. Calling it a second time, or calling `.override(...)` after the first use, 
raises `ValueError`:
   
   ```python
   from airflow.sdk import DAG, teardown
   
   @teardown
   def cleanup(x=None): ...
   
   with DAG("teardown_reuse", schedule=None):
       cleanup()
       cleanup()  # ValueError: Trigger rule not configurable for teardown 
tasks.
   ```
   
   `cleanup.override(task_id="cleanup_2")()` raises the same error after a 
first `cleanup()` or `cleanup.expand(x=[1, 2])`.
   
   Separately, a teardown mapped with `expand_kwargs()` does not get the 
teardown trigger rule:
   
   ```python
   @teardown
   def cleanup_mapped(x): ...
   
   with DAG("teardown_expand_kwargs", schedule=None):
       op = cleanup_mapped.expand_kwargs([{"x": 1}, {"x": 2}]).operator
       print(op.trigger_rule)  # TriggerRule.ALL_SUCCESS
   ```
   
   ### What you think should happen instead?
   
   - A `@teardown` function can be called any number of times, directly or 
through `.override()`, like any other TaskFlow task. Every resulting task gets 
`all_done_setup_success`.
   - A teardown mapped with `expand_kwargs()` gets `all_done_setup_success`, as 
it does with `expand()`. With `all_success`, the teardown is skipped when an 
upstream task fails, so the resources it should clean up are left behind.
   
   Cause: `_TaskDecorator.__call__` and `expand()` in 
`task-sdk/src/airflow/sdk/bases/decorator.py` write `trigger_rule` into the 
decorator's own `kwargs`. The next call sees the stored `trigger_rule` as user 
input and fails the "not configurable" check. `__call__` also pops 
`on_failure_fail_dagrun` from those `kwargs`, so once the `ValueError` is 
fixed, an override set through `.override()` would still be lost after the 
first call. `expand_kwargs()` has no teardown handling.
   
   ### Operating System
   
   macOS (Dag parsing only; not OS-specific)
   
   ### Deployment
   
   Virtualenv installation
   
   ### Apache Airflow Provider(s)
   
   standard
   
   ### Versions of Apache Airflow Providers
   
   Not Applicable
   
   ### Official Helm Chart version
   
   Not Applicable
   
   ### Kubernetes Version
   
   Not Applicable
   
   ### Helm Chart configuration
   
   Not Applicable
   
   ### Docker Image customizations
   
   Not Applicable
   
   ### 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