Skip to content

SerializedDagModel.get_count() should raise on None scalar result instead of silently returning 0 #66796

@1fanwang

Description

@1fanwang

Apache Airflow version

main (3.x)

What happened?

SerializedDagModel.get_count() returns session.scalar(select(func.count()).select_from(cls)) or 0. The or 0 collapses the legitimately-impossible None case with the "table is empty" case (which actually returns 0, not None) — and also masks any transient DB error that surfaces as None from session.scalar().

In practice the only path that reaches or 0 is a DB-side failure, and silently emitting serialized_dag.count = 0 causes false on-call pages ("all DAGs disappeared!") while hiding the real problem.

What you think should happen instead?

Drop the or 0. select(func.count()) on an aggregate is guaranteed to return an int, so a None return is anomalous and should raise (or log warning + raise). Callers can opt into try/except as needed.

Anything else?

This is a 1-line change in the model; one caller in dag_processing/manager.py emits the count to a metric path and should wrap in try/except after the change.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:corekind:bugThis is a clearly a bugpriority:mediumBug that should be fixed before next release but would not block a release

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions