PCTEJA commented on code in PR #77527:
URL: https://github.com/apache/airflow/pull/77527#discussion_r4730317113


##########
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:
   Audited main, no leak, 3a0e41e5



##########
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:
   remove() dropped in commit 3a0e41e5



##########
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:
   Docstring trimmed in commit 3a0e41e5



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