Fix AttributeError when notifier renders templates with SerializedDAG#67862
Open
GayathriSrividya wants to merge 1 commit into
Open
Fix AttributeError when notifier renders templates with SerializedDAG#67862GayathriSrividya wants to merge 1 commit into
GayathriSrividya wants to merge 1 commit into
Conversation
e63742b to
4ab3680
Compare
When 'airflow dags test' triggers a DAG callback that uses a notifier, the context's 'dag' key holds a SerializedDAG instance which does not have a get_template_env method. BaseNotifier.render_template_fields passes this dag to Templater.get_template_env, which unconditionally calls dag.get_template_env() — raising AttributeError. Guard the call with hasattr() so that a dag lacking get_template_env (such as SerializedDAG) silently falls back to SandboxedEnvironment. closes: apache#64649
4ab3680 to
d8fdac9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes: #64649
When `airflow dags test` is used and the DAG has an `on_failure_callback` (or similar callback) that uses a `BaseNotifier`, the context passed to the callback contains a `SerializedDAG` object rather than a real `DAG`. `SerializedDAG` does not inherit from `Templater` and therefore has no `get_template_env` method.
`Templater.get_template_env` called `dag.get_template_env(force_sandboxed=False)` unconditionally whenever `dag` was truthy, raising:
```
AttributeError: 'SerializedDAG' object has no attribute 'get_template_env'
```
Fix: guard the call with `hasattr(dag, "get_template_env")` so that a dag object lacking the method (such as `SerializedDAG`) falls back to `SandboxedEnvironment` instead of crashing.
Thanks to @mscsy0104 and @Quantum0uasar for investigating this issue and outlining the fix approach in the issue thread.