Fix race condition in auth manager initialization#62431
Fix race condition in auth manager initialization#62431kimyoungi99 wants to merge 3 commits intoapache:mainfrom
Conversation
f25b370 to
82796cd
Compare
jason810496
left a comment
There was a problem hiding this comment.
Hi @kimyoungi99, thanks for raising the PR again!
Would you mind starting Airflow locally to verify the system behavior, in case the situation described in #62404 happens again?
If you haven’t installed Breeze yet, you can run: uv tool install -e ./dev/breeze --force
Then run: breeze start-airflow --mount-sources providers-and-tests --auth-manager FabAuthManager to verify that the updated FabAuthManager and FastAPI app work as expected.
Thanks!
f9e3584 to
9d0d69a
Compare
|
Hi @jason810496, thanks for the suggestion! I ran Sequential requests — all working correctly:
Concurrent requests — while testing concurrent FAB + Core requests, I discovered an additional race condition in Added a new commit ( |
d7a80ca to
2f82360
Compare
…races FAB FastAPI routes call get_application_builder() on every request, which creates a new Flask app and invokes init_app(). Concurrent calls race on the singleton auth_manager's appbuilder and security_manager, causing KeyError: 'AUTH_USER_REGISTRATION' and AttributeError. Add _init_app_lock around the critical section in init_app() that mutates the singleton auth_manager state and registers views, so concurrent get_application_builder() calls are serialized.
2f82360 to
ad1324f
Compare
Closes #61108
This is a follow-up to #62214 (reverted in #62404).
Problem
Concurrent requests to
/auth/tokencause intermittent 500 errors:create_auth_manager()creates a new instance on every call. Under concurrent requests, one thread overwrites_AuthManagerState.instancewhile another's is still initializing.Previous approach (#62214) and why it was reverted
The previous fix added
purge_cached_app()inget_application_builder(), but that function is called at runtime by FAB FastAPI routes (login, user/role management). Clearing the singleton on every call broke subsequent core API requests withKeyError: 'AUTH_USER_REGISTRATION'.This fix
create_auth_manager(): Double-checked locking withisinstancevalidation — creates the singleton once, replaces it only when the auth manager class changes (e.g.SimpleAuthManager→FabAuthManager).init_appbuilder.py: Clearssecurity_manager@cached_propertywheninit_app()is called with a new Flask app, so_init_config()runs against the current app context.No changes to
get_application_builder()or test fixtures.Testing
Added
test_create_auth_manager_thread_safety— verifies singleton behavior under 10 concurrent threads.