Skip to content

Fix deadline never firing after non-deadline Dag edit - #68734

Merged
o-nikolas merged 9 commits into
apache:mainfrom
ehab20011:fix/deadline-alert-orphan-on-reserialize
Jul 15, 2026
Merged

o-nikolas merged 9 commits into
apache:mainfrom
ehab20011:fix/deadline-alert-orphan-on-reserialize

Conversation

@ehab20011

@ehab20011 ehab20011 commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

This fixes the bug reported in #68732 by @katherine-wong7, where a deadline silently stops firing after any non-deadline DAG edit (e.g. adding a task). The root cause is that when _try_reuse_deadline_uuids successfully reuses existing deadline UUIDs, the deadline_uuid_mapping is emptied (= {}). If the DAG hash still changes due to a non-deadline edit and write_dag enters the INSERT branch (new serialized_dag row), _create_deadline_alert_records receives an empty mapping and creates nothing. The new row ends up with deadline UUIDs in its JSON but zero deadline_alert rows pointing to it so the materialization query at dag run creation finds nothing, and the deadline never fires.

Where the problem was

airflow-core/src/airflow/models/serialized_dag.py, line 676 in the write_dag classmethod:

if reuse_result is not None:
    deadline_uuid_mapping, name_updates = reuse_result
    ...
    dag.data["dag"]["deadline"] = existing_deadline_uuids
    deadline_uuid_mapping = {}   # <-- deadline data is lost here

This emptying is fine when the hash matches (we short-circuit and skip writing), and fine for the UPDATE branch (same serialized_dag.id, so existing deadline_alert rows stay linked). But when the hash changes and the INSERT branch runs, the new serialized_dag row has no deadline_alert rows.

Reproducing the bug locally

Wrote a test that mirrors the exact scenario from the issue — create a deadline-bearing DAG, add a dagrun (to force the INSERT branch), add a second task (non-deadline change), re-serialize, and assert the new serialized_dag has a linked deadline_alert.

Running this test without the fix fails with the exact error described in the issue:

FAILED
assert new_alert is not None, (
AssertionError: serialized_dag 019edd40-9a35-735c-9c87-6a44d0890b7a has deadline UUIDs
in JSON but no deadline_alert row links to it

The fix

Updated based on reviewer feedback: the structural issue was that cls(dag) was constructed before knowing which branch (skip / UPDATE / INSERT) we'd take. By deferring construction past the branch decision, each branch can prepare dag.data correctly before the constructor runs — eliminating the need for post-hoc patching of internal attributes.

Changes to write_dag in airflow-core/src/airflow/models/serialized_dag.py:

  1. Compute the hash early — cls.hash(dag.data) instead of cls(dag) — to decide which branch to take without constructing the full object.
  2. UPDATE branch — construct cls(dag) at the top of the branch. Reused UUIDs are already in dag.data, so the constructor picks them up naturally.
  3. INSERT branch — regenerate fresh UUIDs and update dag.data before calling cls(dag), so the constructor serializes the correct data. This replaces the previous 14-line post-hoc patching block that manually re-serialized, re-compressed, and re-hashed internal attributes.

The UPDATE branch needs no change — it updates the row in place (same serialized_dag.id), so existing deadline_alert rows remain correctly linked.

Testing

Added three tests covering all branches where deadline reuse interacts with re-serialization:

Test Branch What it proves
test_non_deadline_edit_creates_deadline_alert_for_new_serdag INSERT The bug — new serialized_dag gets its own deadline_alert rows, old version's alerts stay intact
test_non_deadline_edit_preserves_alert_in_update_branch UPDATE Existing alert stays linked after in-place update
test_deadline_reuse_skips_write_when_hash_matches Short-circuit No-op when nothing changed — write_dag returns False, alerts untouched

All deadline tests pass (each runs in both raw and compressed serialization modes):

10 passed, 62 deselected, 1 warning in 1.84s

closes: #68732


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

@eladkal
eladkal requested a review from ferruzzi June 19, 2026 20:07
@potiuk potiuk added the ready for maintainer review Set by triage when all criteria pass, or by the fork CI reconciler when fork CI is green. label Jun 22, 2026
@ferruzzi

Copy link
Copy Markdown
Contributor

Nice catch on the bug. Duplicating the data feels very heavy-handed, surely there's a better way? It looks like this whole issue is because new_serialized_dag = cls(dag) is constructed before we know which branch we'll take and we're just plastering over that.

Would it be a cleaner fix to calculate the hash early with cls.hash(dag.data) and use that for the branching logic, then construct the new serialized Dag only in the branch that needs it, after the UUID is resolved? If you build the serdag after calling _generate_deadline_uuids(dag.data) then it'll get the right UUID and not need to store the old values, recalculate hashes, etc.

@ehab20011 ehab20011 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hey ferruzi! thanks for the suggestion :) yea you're right, deferring cls(dag) past the branch decision is def a much cleaner approach. I've updated the PR to:

  1. Compute the hash early with cls.hash(dag.data) instead of constructing the full SerializedDagModel
  2. Construct cls(dag) at the top of the UPDATE branch
  3. In the INSERT branch, regenerate UUIDs and update dag.data before cls(dag), so the constructor picks up the right values naturally. Which then eliminates the 14-line post-hoc patching block (re-serializing, re-compressing, re-hashing internal attributes).

So yea and I think this should be good from here imo what do you think? all existing tests pass without modification when i ran them.

oh yea and one note, reused_deadline_data is still needed (not for the old UUIDs), but to hold the deadline definitions so we can create new DeadlineAlertModel records in the INSERT branch. Unless you think theres a better way

@ferruzzi ferruzzi 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.

Yeah, I think that looks better.

@sjyangkevin - Note this may cause a conflict with yours, but your deepcopy fix would go on top of this change

@o-nikolas
o-nikolas merged commit 00ce83c into apache:main Jul 15, 2026
78 checks passed
@vatsrahul1001 vatsrahul1001 added this to the Airflow 3.3.2 milestone Aug 12, 2026
@ferruzzi ferruzzi added backport-to-v3-3-test and removed ready for maintainer review Set by triage when all criteria pass, or by the fork CI reconciler when fork CI is green. labels Aug 15, 2026
vatsrahul1001 pushed a commit that referenced this pull request Sep 7, 2026
The structural issue was that cls(dag) was constructed before knowing which branch (skip / UPDATE / INSERT) we'd take. By deferring construction past the branch decision, each branch can prepare dag.data correctly before the constructor runs — eliminating the need for post-hoc patching of internal attributes.

(cherry picked from commit 00ce83c)

Co-authored-by: Ehab Abdalla <[email protected]>
vatsrahul1001 pushed a commit that referenced this pull request Sep 7, 2026
The structural issue was that cls(dag) was constructed before knowing which branch (skip / UPDATE / INSERT) we'd take. By deferring construction past the branch decision, each branch can prepare dag.data correctly before the constructor runs — eliminating the need for post-hoc patching of internal attributes.

(cherry picked from commit 00ce83c)

Co-authored-by: Ehab Abdalla <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

5 participants