kimyoungi99 opened a new pull request, #62213:
URL: https://github.com/apache/airflow/pull/62213

   Closes #61108
   
   ### Problem
   
   When sending concurrent requests to `/auth/token`, intermittent 500 errors 
occur:
   
   ```
   AttributeError: 'AirflowAppBuilder' object has no attribute 'sm'
   ```
   
   `create_auth_manager()` creates a new instance on every call without 
checking for an existing one. Under concurrent requests, one thread can 
overwrite `_AuthManagerState.instance` while another thread's instance is still 
being initialized via `init()`, resulting in an uninitialized auth manager 
being served.
   
   ### Fix
   
   Apply double-checked locking in `create_auth_manager()` so the auth manager 
is created exactly once:
   
   ```python
   def create_auth_manager() -> BaseAuthManager:
       if _AuthManagerState.instance is None:
           with _AuthManagerState._lock:
               if _AuthManagerState.instance is None:
                   auth_manager_cls = get_auth_manager_cls()
                   _AuthManagerState.instance = auth_manager_cls()
       return _AuthManagerState.instance
   ```
   
   The first check avoids lock overhead on subsequent calls. The second check 
(inside the lock) prevents duplicate creation when multiple threads pass the 
first check simultaneously.
   
   ### What's changed
   
   - `airflow-core/src/airflow/api_fastapi/app.py`: Added `threading.Lock` to 
`_AuthManagerState` and guarded instance creation with double-checked locking
   - `airflow-core/tests/unit/api_fastapi/test_app.py`: Added 
`test_create_auth_manager_thread_safety` — spawns 10 concurrent threads and 
verifies singleton behavior
   
   ### Testing
   
   - All 9 tests in `test_app.py` pass locally (including the new one)
   - `prek`, `ruff check`, `ruff format` all clean


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to