You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "seanghaeli (via GitHub)" <gi...@apache.org> on 2026/10/13 23:44:52 UTC

[I] A deadline alert that undoes the scheduler's database changes can crash the scheduler or lose the Dag run [airflow]

seanghaeli opened a new issue, #74677:
URL: https://github.com/apache/airflow/issues/74677

   ### What happened
   
   When the scheduler creates a Dag run, it saves the Dag run and a deadline for each of the Dag's deadline alerts to the database in one go. It handles the alerts one at a time, in the order they're listed on the Dag.
   
   If one alert's code undoes the scheduler's unsaved changes (for example, a custom deadline reference that catches a database error and calls `session.rollback()`), the new Dag run is undone along with it. Then:
   
   - **If a later alert uses `DeadlineReference.FIXED_DATETIME`,** it still adds a deadline for the Dag run that was just undone. Saving fails because that Dag run no longer exists, and the scheduler exits. Restarting it crashes it again within a second, and no Dag gets new runs in the meantime.
   - **Otherwise,** the Dag run is quietly not created, but the scheduler moves on as if it had been. It only tries again when the Dag file is re-read (every 30 seconds by default), and fails the same way each time. The Dag never runs.
   
   #68917 made a failing alert safe for ordinary errors, like a missing Variable. This is the one kind of failure it doesn't cover.
   
   ### What you think should happen instead
   
   A failing alert should never crash the scheduler or stop the Dag run from being created.
   
   ### How to reproduce
   
   Tested on main (`417a33b5432`) in Breeze with Postgres, LocalExecutor, and the real scheduler, dag-processor and api-server.
   
   Plugin, `plugins/my_references.py`:
   
   ```python
   from sqlalchemy import text
   
   from airflow.plugins_manager import AirflowPlugin
   from airflow.sdk import BaseDeadlineReference, deadline_reference
   
   
   @deadline_reference
   class RollbackReference(BaseDeadlineReference):
       """Reads a time from a table. On a database error it rolls back, then re-raises."""
   
       def _evaluate_with(self, *, session, **kwargs):
           try:
               return session.execute(text("SELECT due FROM my_sla_table")).scalar_one()
           except Exception:
               session.rollback()
               raise
   
   
   async def notify(**kwargs):
       print("deadline missed", kwargs)
   
   
   class MyReferencesPlugin(AirflowPlugin):
       name = "my_references"
       deadline_references = [RollbackReference]
   ```
   
   Dag (`my_sla_table` doesn't exist, so the first alert always fails):
   
   ```python
   from datetime import datetime, timedelta
   
   from my_references import notify
   
   from airflow.providers.standard.operators.bash import BashOperator
   from airflow.sdk import DAG, AsyncCallback, DeadlineAlert, DeadlineReference
   
   with DAG(
       dag_id="rollback_fixed",
       schedule="@daily",
       start_date=datetime(2026, 1, 1),
       catchup=False,
       deadline=[
           DeadlineAlert(
               reference=DeadlineReference.RollbackReference,
               interval=timedelta(hours=1),
               callback=AsyncCallback(notify),
           ),
           DeadlineAlert(
               reference=DeadlineReference.FIXED_DATETIME(datetime(2030, 1, 1)),
               interval=timedelta(0),
               callback=AsyncCallback(notify),
           ),
       ],
   ):
       BashOperator(task_id="wait", bash_command="sleep 600")
   ```
   
   Unpause it and let the scheduler create the Dag run. Saving fails with an `IntegrityError` on `deadline_dagrun_id_fkey` and the scheduler exits.
   
   Change the second alert to `DeadlineReference.DAGRUN_LOGICAL_DATE` (or remove it) and the scheduler stays up, but no Dag run is ever created.
   
   ### Anything else
   
   - **Possible fix:** before handling each alert, check whether the Dag run has already been undone, and if so skip the rest of the alerts (in code: skip when `orm_dagrun not in session`).
   - **Why it matters for #70714:** once #70714 merges, `DAGRUN_LOGICAL_DATE` and `DAGRUN_QUEUED_AT` alerts behave like `FIXED_DATETIME` here, so the crash covers the most common references too.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] A deadline alert that undoes the scheduler's database changes can crash the scheduler or lose the Dag run [airflow]

Posted by "seanghaeli (via GitHub)" <gi...@apache.org>.
seanghaeli commented on issue #74677:
URL: https://github.com/apache/airflow/issues/74677#issuecomment-6176158430

   Go ahead, thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] A deadline alert that undoes the scheduler's database changes can crash the scheduler or lose the Dag run [airflow]

Posted by "schizophrenicmaniac (via GitHub)" <gi...@apache.org>.
schizophrenicmaniac commented on issue #74677:
URL: https://github.com/apache/airflow/issues/74677#issuecomment-6157596426

   Hi, I'd like to work on this. In _process_dagrun_deadline_alerts I'd check whether orm_dagrun is still in the session before each alert, and if a rollback dropped it, log and stop instead of adding orphan Deadline rows. I'll add tests with a reference that rolls back the session.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] A deadline alert that undoes the scheduler's database changes can crash the scheduler or lose the Dag run [airflow]

Posted by "jason810496 (via GitHub) via commits" <co...@airflow.apache.org>.
jason810496 commented on issue #74677:
URL: https://github.com/apache/airflow/issues/74677#issuecomment-7207906061

   Hi @seanghaeli,
   
   Would it make sense to refactor the public interface of `BaseDeadlineReference` so it no longer takes the DB session as an argument? As it stands, users can access the metadata DB session from within the scheduler, which is a high-risk operation.
   
   Instead, we could pass the necessary info/context into the method. Users could then implement their own business logic without ever touching the DB session directly.
   
   WDYT? Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org