Skip to content

Recover the revoked-token check from a stale DB connection - #77527

Open
PCTEJA wants to merge 2 commits into
apache:mainfrom
PCTEJA:fix/airflow-71395-revoked-token-stale-session
Open

PCTEJA wants to merge 2 commits into
apache:mainfrom
PCTEJA:fix/airflow-71395-revoked-token-stale-session

Conversation

@PCTEJA

@PCTEJA PCTEJA commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

With a MySQL metadata database, the first authenticated API request after the connection goes idle fails with HTTP 500. The revoked-token lookup in BaseAuthManager.get_user_from_token runs on the pooled session, and when MySQL has already dropped the connection (wait_timeout exceeded, error 4031), the query raises, so the first request fails instead of retrying on a fresh connection. The next request succeeds because the failed attempt already evicted the dead connection.

This mirrors the recovery that FABAuthManager.deserialize_user already does: on a database error from the revocation lookup, retry the lookup once on a fresh connection (create_session rolls back and closes the failed session). If the retry also fails, the error propagates unchanged, so a genuinely unavailable database still surfaces instead of being masked.

This revives the approach from #71444, which the reporter ran in production for weeks before it was closed by the stale bot; the fix here applies it at the shared BaseAuthManager level so every auth manager that relies on the revoked-token check gets the same recovery.

closes: #71395

Verification

  • Added three tests to test_base_auth_manager.py covering the recovery path: the retry succeeds after a stale-connection error, a revoked token is still rejected after a retry, and a persistent error propagates after exactly one retry.
  • The new tests fail against the unmodified code, and a negative control (call site temporarily reverted to the direct RevokedToken.is_revoked call) fails with exactly the reported sqlalchemy.exc.OperationalError: (4031, 'The client was disconnected...').
  • airflow-core/tests/unit/api_fastapi/auth/managers/test_base_auth_manager.py: 72 passed. Ruff format and check pass; mypy for airflow-core passes.
  • No live MySQL reproduction was possible in this environment (no Docker/breeze), so verification of the failure mode uses a mocked MySQL 4031 error; CI's MySQL-backed tests are the remaining check for the real database path.
  • No API route or schema is touched, so the generated OpenAPI spec is unchanged.

Was generative AI tooling used to co-author this PR?
  • Yes

Generated-by: I used ChatGPT astra only for inline code completion following the guidelines

@PCTEJA
PCTEJA requested a review from vincbeck as a code owner November 24, 2026 07:46
@boring-cyborg boring-cyborg Bot added the area:API Airflow's REST/HTTP API label Nov 24, 2026
@PCTEJA

PCTEJA commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

@vincbeck The PR is ready to be reviewed.

@vincbeck

Copy link
Copy Markdown
Contributor

@vincbeck The PR is ready to be reviewed.

Can you answer my comments please?

The lookup runs inside create_session, which already rolls back and
closes the scoped session when it raises, so the retry gets a fresh
connection without an explicit remove(). Keeping the discard would
leave a settings import, a guard and a test with no behaviour behind
them. The helper docstring is trimmed to two lines, with the longer
rationale kept in the PR description.
@PCTEJA
PCTEJA force-pushed the fix/airflow-71395-revoked-token-stale-session branch from 60debb4 to 3a0e41e Compare November 29, 2026 05:01
@PCTEJA

PCTEJA commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

@vincbeck Simplified as suggested: remove() gone, docstring two lines. Audited main, found no remaining leak. Close this, or keep retry?

This branch has not been deployed

No deployments
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

2 participants