Skip to content

Fix DAG_DISCOVERY_SAFE_MODE config not being respected - #67325

Closed
kir-rin wants to merge 1 commit into
apache:mainfrom
kir-rin:fix-dag-discovery-safe-mode-config
Closed

kir-rin wants to merge 1 commit into
apache:mainfrom
kir-rin:fix-dag-discovery-safe-mode-config

Conversation

@kir-rin

@kir-rin kir-rin commented May 22, 2026

Copy link
Copy Markdown
Contributor

closes: #66104

Problem

When DAG_DISCOVERY_SAFE_MODE is set to False in the config, Python files
without "airflow" or "dag" keywords are still being skipped during DAG discovery.

This happens because the default argument safe_mode=conf.getboolean(...) in
list_py_file_paths() is evaluated at module load time, not at call time.
Once evaluated, the value is fixed and subsequent config changes are ignored.

Solution

Use the existing NOTSET sentinel pattern (already used in DagBag) to defer
config reading to runtime:

def list_py_file_paths(
    directory: str | os.PathLike[str] | None,
    safe_mode: bool | ArgNotSet = NOTSET,
) -> list[str]:
    if not is_arg_set(safe_mode):
        safe_mode = conf.getboolean("core", "DAG_DISCOVERY_SAFE_MODE", fallback=True)
    ...

This ensures the config value is read at call time, allowing runtime config
changes to take effect.

Testing

Added a test that would fail without this fix, verifying that:

  • Files without keywords are skipped with safe_mode=True
  • Files without keywords are found with safe_mode=False
  • Config setting is now respected when using default argument

Was generative AI tooling used to co-author this PR?
  • Yes — GLM-5

Generated-by: GLM-5 following the guidelines

@kir-rin
kir-rin force-pushed the fix-dag-discovery-safe-mode-config branch from 93aeac8 to 55e167f Compare May 23, 2026 04:40
@kir-rin

kir-rin commented May 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Closing this PR.

After further investigation:

  1. Restart is standard practice - Airflow reads config at process startup. Restarting processes after config change is the expected workflow.
  2. Pattern is widespread - The default argument pattern (param: bool = conf.getboolean(...)) is used multiple times across the codebase (e.g., deferrable parameter in operators/sensors). Changing it only here would create inconsistency.

Based on these reasons, the behavior of evaluating default arguments at module load time is standard Python behavior and consistent with Airflow's design philosophy. This is not a code bug.

@kir-rin kir-rin closed this May 23, 2026
@kir-rin
kir-rin deleted the fix-dag-discovery-safe-mode-config branch May 25, 2026 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

`dag_discovery_safe_mode not scanning all files when set to False

1 participant