Skip to content

Fix email_on_failure with render_template_as_native_obj - #22770

Merged
ashb merged 5 commits into
apache:mainfrom
astronomer:template_native_obj
Apr 7, 2022
Merged

ashb merged 5 commits into
apache:mainfrom
astronomer:template_native_obj

Conversation

@jedcunningham

Copy link
Copy Markdown
Member

Co-authored-by: andyhuang [email protected]

Closes: #22152
Alternative of #22218, refactored to only change behavior when generating emails and including test coverage.

cc @andyfcx

@uranusjr uranusjr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nitpicks otherwise lgtm.

Comment thread airflow/models/abstractoperator.py Outdated
Comment thread airflow/models/dag.py Outdated
@github-actions github-actions Bot added the full tests needed We need to run full set of tests for this PR to merge label Apr 6, 2022
@github-actions

github-actions Bot commented Apr 6, 2022

Copy link
Copy Markdown
Contributor

The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest main at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.

Comment thread tests/models/test_dag.py
Comment thread airflow/models/taskinstance.py Outdated

else:
jinja_env = self.task.get_template_env()
jinja_env = self.task.get_template_env(force_sandboxed=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you may need to update this operator to handle kwargs

also, this illustrates a backcompat issue if a user implemented this method (since it's a signature change).

if we wanted to be real careful we could inspect the signature before calling....

wdyt?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ugh that's going to make this approach hard/basically impossible. Nice catch @dstandish

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I lean toward this being "internal" to the task, but I'm not sure how others view it.

We could inspect, but emails would still fail even then. I wonder if we should instead release a new psrp provider that has a minimum core of 2.3.0. I guess the most 'flexible', ignoring the email issue, is to inspect in both so new providers still work on old core and vice versa.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use dag.get_template_env(force_sandbox) instead of self in side the email rendering?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good idea, it uses dag anyways! Let me try that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that works 🍺

@blag blag left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides @dstandish's questions, LGTM.

Comment thread airflow/models/abstractoperator.py Outdated
return self.task_id

def get_template_env(self) -> "jinja2.Environment":
def get_template_env(self, *, force_sandboxed: bool = False) -> "jinja2.Environment":

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we should remove support for force_sandboxed here so we arent changing the signature of the method psrp is overriding?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't do any harm since its an optional arg, but for the sake of a smaller diff, yes we could remove it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to removing it since it’d make more obvious why we call the DAG’s get_template_env instead (so we can pass this option). It’d be nice to have a comment there describing why we don’t call the task’s get_template_env there.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a commit for this (plus a small tweak for Mypy).

Also added comments on why we use the DAG level implementation instead
of the task level one.
@ashb
ashb merged commit d80d52a into apache:main Apr 7, 2022
@ashb
ashb deleted the template_native_obj branch April 7, 2022 08:48
@ashb ashb added this to the Airflow 2.3.0 milestone Apr 7, 2022
@ephraimbuddy ephraimbuddy added the type:bug-fix Changelog: Bug Fixes label Apr 7, 2022
blag pushed a commit to astronomer/airflow that referenced this pull request Apr 13, 2022
jedcunningham added a commit to astronomer/airflow that referenced this pull request Apr 13, 2022
@vandanthaker

vandanthaker commented Jun 20, 2022 •

Copy link
Copy Markdown

@jedcunningham
Any alternative to fix this on v2.2.5 without upgrading to 2.3.X?
Thanks in advance

@potiuk

potiuk commented Jun 20, 2022

Copy link
Copy Markdown
Member

@jedcunningham Any alternative to fix this on v2.2.5 without upgrading to 2.3.X? Thanks in advance

As usual in open source - you can cherry-pick and apply it yourself. But we recommend to upgrade to 2.3 instead. Upgrading to latest released version is generally good thing to do.

@potiuk

potiuk commented Jun 20, 2022

Copy link
Copy Markdown
Member

This is really the trade-off - either you take the burden of migration to what community maintain, or you take the maintenance burden yourself :). Choose one that is best for you.

@vandanthaker

Copy link
Copy Markdown

Agree :)

@uranusjr

Copy link
Copy Markdown
Member

Note that you can always manually add the patch—this one is simple enough I believe a simple patch command should work.

@dstandish

Copy link
Copy Markdown
Contributor

And here's an example of how we patched another PR in a dockerfile

RUN apt update && apt install -y patch patchutils

RUN set -ex; \
    curl -o /tmp/kpo.patch https://patch-diff.githubusercontent.com/raw/apache/airflow/pull/24117.patch; \
    cd /usr/local/lib/python3.9/site-packages/airflow; \
    filterdiff -p1 -i 'airflow*' /tmp/kpo.patch | patch -u -p 2; \
    rm /tmp/kpo.patch

Eason09053360 added a commit to Eason09053360/airflow that referenced this pull request Sep 17, 2026
A Dag with render_template_as_native_obj=True made email_on_failure and
email_on_retry alerts fail to send: the alert subject and body were evaluated
into Python objects, which the email backend cannot send, and the resulting
error was swallowed by the notification error handler, so the alert vanished
without any signal to the operator.

This was fixed once in apache#22770 and regressed when the alert path moved to
SmtpNotifier in apache#57354, which dropped the only caller of force_sandboxed.

Only the subject and body are forced to strings; the recipient fields keep
rendering the way the Dag asks for, so a templated address list still
resolves to a real list.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full tests needed We need to run full set of tests for this PR to merge type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

render_template_as_native_obj=True in DAG constructor prevents failure e-mail from sending

8 participants