jecos opened a new issue, #76402:
URL: https://github.com/apache/airflow/issues/76402
### Under which category would you file this issue?
Providers
### Apache Airflow version
3.3.1
### What happened and how to reproduce it?
The FAB FastAPI routes that read a single object, `GET
/auth/fab/v1/users/{username}` and `GET /auth/fab/v1/roles/{name}`, leave their
PostgreSQL session `idle in transaction` after returning 200. The connection is
never returned to the API server's pool. Once enough worker threads hold one,
the pool is exhausted and the whole API server stops answering:
```
sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached,
connection timed out, timeout 30.00
```
The health check (`/api/v2/monitor/health`) then times out, the liveness
probe restarts the pod, and the cycle repeats. Tasks fail too, because the Task
SDK cannot reach the Execution API (`ReadTimeout` on `airflow.sdk.api.client`).
In our case, the trigger is a Terraform provider that reconciles FAB users
and roles every 5 minutes, so it calls `GET /users/{username}` and `GET
/roles/{name}` for each managed object. The pool was full 20 to 30 minutes
after each restart.
What `pg_stat_activity` showed on the affected pod (database CPU around 3%,
no lock waits):
- 14 sessions `idle in transaction`, aged 5 to 25 minutes, each matching one
of the Terraform calls in the access log
- all on the same statement, the `User.roles` → permissions eager load
issued by `find_user`:
```sql
SELECT ab_user_1.id AS ab_user_1_id, ab_role.id AS ab_role_id, ab_role.name
AS ab_role_name, ...
FROM ab_user AS ab_user_1 JOIN ab_user_role AS ab_user_role_1 ON
ab_user_1.id = ab_user_role_1.user_id
JOIN ab_role ON ab_role.id = ab_user_role_1.role_id LEFT OUTER JOIN
(ab_permission_view_role ...) ...
WHERE ab_user_1.id IN (<id>)
```
The ids are those of the users being looked up, not of the caller.
Cause:
1. The routes are sync FastAPI handlers, so they run on an anyio threadpool
thread.
2. The services call `security_manager.find_user()` / `find_role()`, which
read through `settings.Session`, a `scoped_session` keyed by thread. Nothing
ends that transaction.
3. `cleanup_session_middleware` in `FabAuthManager.get_fastapi_app()` calls
`settings.Session.remove()`, but it is an `async` middleware and runs on the
event-loop thread, so it never reaches the worker thread's session.
4. Flask's `app_context()` exit does not help either: the FAB app registers
no `teardown_appcontext` that removes the session.
Each threadpool thread (40 by default) can therefore pin one connection,
while the default pool is 15.
#72361 / #72362 reported the same mechanism for the collection routes.
#72578 and #72950 fixed those by moving `get_users`, `get_roles` and
`get_permissions` to `create_session(scoped=False)`. On `main`, `get_user`,
`get_role` and the write routes (`create_*`, `update_user`, `patch_role`,
`delete_*`), which go through `find_user` / `find_role` / `add_user` /
`update_user`, still use the thread-local session.
Reproduce (PostgreSQL metadata DB, FAB auth manager):
1. Call `GET /auth/fab/v1/users/<existing username>` a few times with a
valid token.
2. Run `SELECT state, query FROM pg_stat_activity WHERE state = 'idle in
transaction';` and see one row per worker thread that served a request.
3. Keep calling the endpoint (or `GET /auth/fab/v1/roles/<name>`) until more
threads than `pool_size + max_overflow` have served one. Every request then
waits 30 s for the pool and fails.
### What you think should happen instead?
Every FAB route should end its database session on the thread that ran the
handler, so no transaction outlives the request and the connection goes back to
the pool.
### Operating System
Debian (official `apache/airflow:3.3.1` image)
### Deployment
Other 3rd-party Helm chart
### Apache Airflow Provider(s)
fab
### Versions of Apache Airflow Providers
apache-airflow-providers-fab 3.8.0 (the code path is unchanged on `main`)
### Kubernetes Version
EKS
### Anything else?
The leak happens on every call to these routes. A client that polls them,
such as a configuration-as-code tool, exhausts the pool within minutes to
hours, depending on the polling rate and the number of objects.
### Are you willing to submit PR?
- [X] Yes I am willing to submit a PR!
### Code of Conduct
- [X] I agree to follow this project's Code of Conduct
--
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]