Repository navigation
Fix missing task alert emails on Dags using native template rendering - #73228
Eason09053360 wants to merge 1 commit into
Conversation
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.
7e35a93 to
256dd59
Compare
|
Hello @Eason09053360 - thank you for your contributions to Apache Airflow! The Airflow community has introduced a limit of 5 open pull requests at a time for contributors without write access to the repository. You currently have 33 open pull requests, so - as a one-time step of introducing the limit - we closed the ones where maintainers have not engaged yet:
These pull requests stay open because maintainers are already engaged in them - they count towards your limit:
This is not a judgement of you or of your changes. We never told contributors before that opening many pull requests at once was a problem, so there is nothing to feel bad about - and nothing is lost: your branches, commits and the review history stay where they are. What we ask you to do is to make your first prioritization decision: choose which of the pull requests above matter most to you, and reopen them (up to 5 open at a time, including the ones still open) with the "Reopen pull request" button or While your pull requests are waiting for review, the most valuable thing you can do is help in other ways - reviewing other contributors' pull requests, helping with issues, and taking part in the discussions on the devlist and Slack. Why we introduced the limit, what it means for you and how to reopen or restore a pull request is explained in https://github.com/apache/airflow/blob/main/contributing-docs/32_open_pull_request_limit.rst. Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting |
Why
On a Dag with
render_template_as_native_obj=True,email_on_failure/email_on_retryalerts are not delivered.Those alerts are rendered through the Dag's Jinja environment, so a native environment evaluates the rendered subject and body back into Python objects. The email backend cannot send a non-string subject or body —
MIMETextraisesAttributeError: 'int' object has no attribute 'encode'— and_send_error_email_notificationswallows that exception into a log line, so the alert disappears with no signal to whoever was supposed to receive it.This was reported and fixed once as #22152 / #22770, which introduced
DAG.get_template_env(force_sandboxed=...)for exactly this case. It regressed in #57354, when the alert path moved toSmtpNotifierand the only production caller offorce_sandboxed=Truewas dropped; the parameter has had no caller outside tests since.What
_send_error_email_notificationintask-sdk/src/airflow/sdk/execution_time/task_runner.pynow renders the alert subject and body itself through a sandboxed environment, and removes those two fields from the notifier's own templating pass so they are not rendered a second time.The override is deliberately scoped to those two fields rather than applied to the whole notifier: recipient fields still render the way the Dag asks for, so a templated address list such as
email="{{ var.json.oncall_emails }}"still resolves to a real list instead of being flattened into a string and split into invalid addresses.test_alert_recipients_still_render_as_native_objectspins that down.One behavior change worth a reviewer's attention: the notifier is now handed already-rendered strings for
subjectandhtml_contentinstead of raw templates. This restores the pre-#57354 contract, and the existing assertions inTestEmailNotificationswere updated from the raw template text to the rendered output.Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Opus 5) following the guidelines