Skip to content

Fix missing task alert emails on Dags using native template rendering - #73228

Closed
Eason09053360 wants to merge 1 commit into
apache:mainfrom
Eason09053360:sandbox-error-email-templates
Closed

Eason09053360 wants to merge 1 commit into
apache:mainfrom
Eason09053360:sandbox-error-email-templates

Conversation

@Eason09053360

Copy link
Copy Markdown
Contributor

Why

On a Dag with render_template_as_native_obj=True, email_on_failure / email_on_retry alerts 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 — MIMEText raises AttributeError: 'int' object has no attribute 'encode' — and _send_error_email_notification swallows 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 to SmtpNotifier and the only production caller of force_sandboxed=True was dropped; the parameter has had no caller outside tests since.

What

_send_error_email_notification in task-sdk/src/airflow/sdk/execution_time/task_runner.py now 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_objects pins that down.

One behavior change worth a reviewer's attention: the notifier is now handed already-rendered strings for subject and html_content instead of raw templates. This restores the pre-#57354 contract, and the existing assertions in TestEmailNotifications were updated from the raw template text to the rendered output.


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 5)

Generated-by: Claude Code (Opus 5) following the guidelines

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.
@Eason09053360
Eason09053360 force-pushed the sandbox-error-email-templates branch from 7e35a93 to 256dd59 Compare September 17, 2026 07:54
@potiuk potiuk added the closed because of open PR limit Closed as a one-time step of introducing the open pull request limit label Sep 25, 2026
@potiuk

potiuk commented Sep 25, 2026

Copy link
Copy Markdown
Member

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 gh pr reopen <PR_NUMBER> --repo apache/airflow. Reopen the ones you are ready to follow through - keep them rebased, respond to review comments and fix failing checks.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:task-sdk closed because of open PR limit Closed as a one-time step of introducing the open pull request limit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants