Skip to content

dag_discovery_safe_mode broken in airflow 3 #52413

Closed

Description

@NilsJPWerner

Apache Airflow version

3.0.2

If "Other Airflow 2 version" selected, which one?

No response

What happened?

I am in the process of upgrading from 2.10.3 to 3.0.2.

I have been relying on setting the env var AIRFLOW__CORE__DAG_DISCOVERY_SAFE_MODE=false in order to parse dags from files that don't mention the words DAG & airflow. The configuration shows up as dag_discovery_safe_mode: false in the airflow config page in both versions.

However it does not seem to be taking effect in version 3.0.2. I have confirmed that if I add # DAG airflow to the top of the dag definition file the dag will appear in the airflow ui. This suggests that the dag parser is still in safe mode despite the configuration.

What you think should happen instead?

All files in the /dags folder should be parsed, even if they do not mention DAG and airflow.

How to reproduce

  • Set the env var AIRFLOW__CORE__DAG_DISCOVERY_SAFE_MODE=false
  • Add file include/create_dag.py:
from datetime import datetime

from airflow import DAG
from airflow.providers.standard.operators.empty import EmptyOperator


def create_dag():
    with DAG(
        "test_dag",
        default_args={
            "owner": "airflow",
            "start_date": datetime(2021, 6, 18),
        },
    ):
        EmptyOperator(task_id="test")
  • Add file dags/test_dag.py
from include.dag_blueprints.create_dag import create_dag

create_dag()

This will not parse unless # airflow DAG is added to the top

Operating System

Debian

Versions of Apache Airflow Providers

No response

Deployment

Astronomer

Deployment details

No response

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Jun 28, 2025
  2. BBQing commented on Jun 29, 2025

    @BBQing
    Contributor

    I have tried this example and dag processor reads the file (it is part of the DagBundle), so from this point the dag_discovery_safe_mode works.
    But there are no dags discovered in the file.
    Once I have added # airflow in the file, the dag was discovered, but it was broken due to ImportError (it did not know the module include).
    Did you make any extra steps to make the import folder visible.

  3. BBQing commented on Jun 29, 2025

    @BBQing
    Contributor

    Maybe this is problematic:

    def _parse_file(msg: DagFileParseRequest, log: FilteringBoundLogger) -> DagFileParsingResult | None:

    def _parse_file(msg: DagFileParseRequest, log: FilteringBoundLogger) -> DagFileParsingResult | None:
        # TODO: Set known_pool names on DagBag!
        bag = DagBag(
            dag_folder=msg.file,
            bundle_path=msg.bundle_path,
            include_examples=False,
            safe_mode=True,
            load_op_links=False,
        )
    
  4. BBQing commented on Jun 29, 2025

    @BBQing
    Contributor

    This is the output for safe_mode=True

    ================================================================================
    DAG File Processing Stats
    
    Bundle       File Path         PID    Current Duration      # DAGs    # Errors  Last Duration    Last Run At
    -----------  ----------------  -----  ------------------  --------  ----------  ---------------  -------------------
    dags-folder  task_groups.py                                      2           0  0.85s            2025-06-29T21:04:38
    dags-folder  from_include.py                                     0           0  0.85s            2025-06-29T21:04:38
    dags-folder  check_config.py                                     1           0  0.06s            2025-06-29T21:04:38
    dags-folder  other_include.py                                    0           0  0.06s            2025-06-29T21:04:38
    ================================================================================
    

    and this the safe_mode=False

    ================================================================================
    DAG File Processing Stats
    
    Bundle       File Path         PID    Current Duration      # DAGs    # Errors  Last Duration    Last Run At
    -----------  ----------------  -----  ------------------  --------  ----------  ---------------  -------------------
    dags-folder  check_config.py                                     1           0  0.08s            2025-06-29T21:07:00
    dags-folder  other_include.py                                    0           1  0.07s            2025-06-29T21:07:00
    dags-folder  task_groups.py                                      2           0  0.07s            2025-06-29T21:07:00
    dags-folder  from_include.py                                     0           1  0.05s            2025-06-29T21:07:00
    ================================================================================
    

    Notice the # Errors column

    @ashb this might be interesting for you

  5. nicnguyen3103 commented on Jun 30, 2025

    @nicnguyen3103

    Maybe this is problematic:

    airflow/airflow-core/src/airflow/dag_processing/processor.py

    Line 129 in e455329

    def _parse_file(msg: DagFileParseRequest, log: FilteringBoundLogger) -> DagFileParsingResult | None:

    def _parse_file(msg: DagFileParseRequest, log: FilteringBoundLogger) -> DagFileParsingResult | None:
        # TODO: Set known_pool names on DagBag!
        bag = DagBag(
            dag_folder=msg.file,
            bundle_path=msg.bundle_path,
            include_examples=False,
            safe_mode=True,
            load_op_links=False,
        )
    

    Confirmed that the force safe_mode is the issue. I have checked the logic in the DagBag parse file, everything function normally

  6. BBQing commented on Jun 30, 2025

    @BBQing
    Contributor

    I can fix it

  7. NilsJPWerner commented on Jun 30, 2025

    @NilsJPWerner
    ContributorAuthor

    I have tried this example and dag processor reads the file (it is part of the DagBundle), so from this point the dag_discovery_safe_mode works. But there are no dags discovered in the file. Once I have added # airflow in the file, the dag was discovered, but it was broken due to ImportError (it did not know the module include). Did you make any extra steps to make the import folder visible.

    Sorry I had screwed up the paths in the example. dags/test_dag.py should be

    from include.create_dag import create_dag
    
    create_dag()
    
  8. added
    area:task-execution-interface-aip72
    on Jul 1, 2025
  9. BBQing commented on Jul 1, 2025

    @BBQing
    Contributor

    @NilsJPWerner if run type(create_dag()) in your script, what is the output value? Because if it is not DAG, then it won't be discovered.

  10. ashb commented on Jul 2, 2025

    @ashb
    Member

    @BBQing That is not true anymore -- as long as somewhere in parsing/executing the top level code with dag is called, then it will be autoregistered and picked up,

    i.e. this would be fine and the dag will be picked up:

    def create_dag() -> None:
        with DAG(dag_id="hello"): ...
    
    create_dag()
  11. BBQing commented on Jul 2, 2025

    @BBQing
    Contributor

    @ashb @NilsJPWerner My bad, it is in the code after the line checking the instance type - I guess I've got distracted. Anyways it was not relevant for this problem.

  12. dayonet commented on Apr 29, 2026

    @dayonet

    Hi,
    I have the same issue with dag_discovery_safe_mode not taking action.
    We wrote a wapper for Airflow so we don't import from Airflow directly in any of the dag definition files.

    When dag_discovery_safe_mode = true the DAGs don't show up no matter what.
    When dag_discovery_safe_mode = false the DAGs appear after running airflow dags reserialize, but then disappear a few minutes after. Each time reserialization is run the DAGs reappear and then disappear again.
    Restarting the pods or adding new DAGs do not help. The DAGs keep disappearing unless we add "airflow" and "dag" to the file, which works with dag_discovery_safe_mode = true as well.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:corearea:task-execution-interface-aip72kind:bugneeds-triage

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions