vincbeck commented on code in PR #77527:
URL: https://github.com/apache/airflow/pull/77527#discussion_r4681232667
##########
airflow-core/src/airflow/api_fastapi/auth/managers/base_auth_manager.py:
##########
@@ -171,6 +174,35 @@ async def get_user_from_token(self, token: str) ->
BaseUser:
log.error("Couldn't deserialize user from token, JWT token is not
valid: %s", e)
raise InvalidTokenError(str(e))
+ @staticmethod
+ def _is_token_revoked(jti: str) -> bool:
+ """
+ Return whether the token ``jti`` has been revoked, tolerating a stale
DB connection.
Review Comment:
nit: I would keep this docstring to 2 lines: what it does and why. The PR
and issue numbers belong in the PR description.
##########
airflow-core/src/airflow/api_fastapi/auth/managers/base_auth_manager.py:
##########
@@ -171,6 +174,35 @@ async def get_user_from_token(self, token: str) ->
BaseUser:
log.error("Couldn't deserialize user from token, JWT token is not
valid: %s", e)
raise InvalidTokenError(str(e))
+ @staticmethod
+ def _is_token_revoked(jti: str) -> bool:
+ """
+ Return whether the token ``jti`` has been revoked, tolerating a stale
DB connection.
+
+ This is the first database access in the auth path and runs on the
shared scoped
+ session. A prior request can leave that session bound to a connection
the database
Review Comment:
Does this still reproduce on main? #68100 (FAB 3.7.0) fixed the session leak
in `deserialize_user`, and it was reported on #71444 that main survives the
idle disconnect. API routes use non-scoped sessions, so what else still leaves
the scoped session open?
##########
airflow-core/src/airflow/api_fastapi/auth/managers/base_auth_manager.py:
##########
@@ -171,6 +174,35 @@ async def get_user_from_token(self, token: str) ->
BaseUser:
log.error("Couldn't deserialize user from token, JWT token is not
valid: %s", e)
raise InvalidTokenError(str(e))
+ @staticmethod
+ def _is_token_revoked(jti: str) -> bool:
+ """
+ Return whether the token ``jti`` has been revoked, tolerating a stale
DB connection.
+
+ This is the first database access in the auth path and runs on the
shared scoped
+ session. A prior request can leave that session bound to a connection
the database
+ later drops on idle timeout (MySQL error 4031, "disconnected ...
because of
+ inactivity"). The connection is never re-checked-out, so
``pool_pre_ping`` /
+ ``pool_recycle`` cannot detect it and the first request after an idle
period fails
+ with a 500. On any ``SQLAlchemyError`` we discard the poisoned scoped
session and
+ retry once on a fresh connection, mirroring the recovery
+ ``FabAuthManager.deserialize_user`` gained in #62919. See #71395.
+ """
+ try:
+ return RevokedToken.is_revoked(jti)
+ except SQLAlchemyError:
+ log.warning(
+ "Revoked-token check failed on a stale DB session; discarding
the scoped "
+ "session and retrying once on a fresh connection.",
+ exc_info=True,
+ )
+ # settings.Session is Optional and only set once the DB is
configured; guard so the
+ # discard is a no-op (rather than a crash) if it is missing.
+ if (session_registry := settings.Session) is not None:
+ with suppress(Exception):
+ session_registry.remove()
Review Comment:
Is `remove()` needed? `create_session` already rolls back and closes the
scoped session when `is_revoked` raises, so the retry gets a new connection
anyway. I tried it with a leaked scoped session and a disconnect error, and the
retry works without it. Dropping it also removes the `settings.Session` guard,
the `suppress` and the last test.
--
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]