Skip to content

Conversation

@kaxil
Copy link
Member

@kaxil kaxil commented May 8, 2025

This change replaces the usage of app.state.dag_bag with a proper FastAPI dependency for better dependency injection and testability. The DagBag is now provided through a dependency that can be easily mocked in tests and follows FastAPI best practices as mentioned here.

image
  • Explicit Dependencies: Using DagBagDep makes it clear in the function signature what each endpoint needs, rather than hiding the dependency in app.state.
  • Better Testing: Dependencies can be easily mocked (via FastAPI's dependency_overrides) in tests without modifying app state, making unit testing simpler and more reliable.
  • Request Isolation: Each request gets its own DagBag instance through the dependency, preventing any potential state leakage between requests.
  • Type Safety: The dependency system provides better type checking and IDE support compared to accessing app.state.

This will help me have better dependency injection in #50300


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

@kaxil kaxil added this to the Airflow 3.0.2 milestone May 8, 2025
@boring-cyborg boring-cyborg bot added the area:API Airflow's REST/HTTP API label May 8, 2025
Copy link
Contributor

@bugraoz93 bugraoz93 left a comment

Choose a reason for hiding this comment

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

Love it. Handling this with annotated depends is a great idea and follows the best practices. Not loading the request each time we need DagBag is already great for security measures and prevents leakage, as mentioned. Only CI and tests need some care

This change replaces the usage of `app.state.dag_bag` with a proper FastAPI dependency
for better dependency injection and testability. The DagBag is now provided through
a dependency that can be easily mocked in tests and follows FastAPI best practices as mentioned [here](https://fastapi.tiangolo.com/reference/fastapi/#fastapi.FastAPI.state:~:text=You%20normally%20wouldn%27t%20use%20this%20in%20FastAPI%2C%20for%20most%20of%20the%20cases%20you%20would%20instead%20use%20FastAPI%20dependencies.).

- Explicit Dependencies: Using `DagBagDep` makes it clear in the function signature what each endpoint needs, rather than hiding the dependency in app.state.
- Better Testing: Dependencies can be easily mocked (via FastAPI's `dependency_overrides`) in tests without modifying app state, making unit testing simpler and more reliable.
- Request Isolation: Each request gets its own `DagBag` instance through the dependency, preventing any potential state leakage between requests.
- Type Safety: The dependency system provides better type checking and IDE support compared to accessing `app.state`.
@kaxil kaxil force-pushed the replace-state-with-dep branch from 919c857 to 1a33d1e Compare May 8, 2025 23:17
@kaxil
Copy link
Member Author

kaxil commented May 8, 2025

Love it. Handling this with annotated depends is a great idea and follows the best practices. Not loading the request each time we need DagBag is already great for security measures and prevents leakage, as mentioned. Only CI and tests need some care

CI is green now 🎉

Copy link
Member

@jason810496 jason810496 left a comment

Choose a reason for hiding this comment

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

Thanks! Having DagBag provided via FastAPI Depends is indeed much easier to trace than using the global app state.

@jason810496
Copy link
Member

Follow up:

For the Auth Manager, should we also consider replacing the use of the global app state with FastAPI Depends?
Currently, init_auth_manager just sets the Auth Manager on the global app state.

Copy link
Contributor

@amoghrajesh amoghrajesh left a comment

Choose a reason for hiding this comment

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

Much cleaner, thank you!

@kaxil kaxil merged commit 3275cb2 into apache:main May 9, 2025
95 checks passed
@kaxil kaxil deleted the replace-state-with-dep branch May 9, 2025 07:03
@kaxil
Copy link
Member Author

kaxil commented May 9, 2025

Follow up:

For the Auth Manager, should we also consider replacing the use of the global app state with FastAPI Depends? Currently, init_auth_manager just sets the Auth Manager on the global app state.

Yeah, go for it

Copy link
Member

@ashb ashb left a comment

Choose a reason for hiding this comment

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

cc @kaxil

kaxil added a commit to astronomer/airflow that referenced this pull request May 11, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in apache#50372,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.
kaxil added a commit to astronomer/airflow that referenced this pull request May 11, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in apache#50372,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.
kaxil added a commit to astronomer/airflow that referenced this pull request May 11, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in apache#50372,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.
kaxil added a commit to astronomer/airflow that referenced this pull request May 11, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in apache#50372 ,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.
kaxil added a commit to astronomer/airflow that referenced this pull request May 11, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in apache#50372 ,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.
kaxil added a commit to astronomer/airflow that referenced this pull request May 12, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in apache#50372 ,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.
kaxil added a commit that referenced this pull request May 12, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in #50372 ,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.
kaxil added a commit that referenced this pull request May 12, 2025
kaxil added a commit that referenced this pull request May 12, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in #50372 ,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.

(cherry picked from commit 3b49f9d)
kaxil added a commit that referenced this pull request Jun 3, 2025
kaxil added a commit that referenced this pull request Jun 3, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in #50372 ,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.

(cherry picked from commit 3b49f9d)
sanederchik pushed a commit to sanederchik/airflow that referenced this pull request Jun 7, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in apache#50372 ,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Sep 25, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in apache/airflow#50372 ,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.

(cherry picked from commit 3b49f9d99e8967cf3f21f6ad58789380beaac83e)

GitOrigin-RevId: e9fb9ad2362f6c5300f2b1451615be875323a8b8
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Oct 22, 2025
Previously, DagBag was injected via onlyFastAPI dependency as part of the refactor in apache/airflow#50372 ,
but a new instance was created for each request. This change updates the dependency to resolve
DagBag from `app.state`, ensuring it is only instantiated once at startup while keeping benefits of FastAPI dependency.

GitOrigin-RevId: 3b49f9d99e8967cf3f21f6ad58789380beaac83e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants