This is an automated email from the ASF dual-hosted git repository.

vatsrahul1001 pushed a commit to branch v3-4-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/v3-4-test by this push:
     new 51e71063fb5 [v3-4-test] Use READ COMMITTED for async MySQL metadata 
sessions (#76694) (#77687)
51e71063fb5 is described below

commit 51e71063fb59bd35ba056075177e06bc29fdba2b
Author: Rahul Vats <[email protected]>
AuthorDate: Fri Nov 27 19:36:45 2026 +0530

    [v3-4-test] Use READ COMMITTED for async MySQL metadata sessions (#76694) 
(#77687)
    
    The async engine never got the isolation level the sync engine sets for
    MySQL, so its sessions ran at the server default, REPEATABLE READ. A
    query repeated in one async transaction could then read a stale snapshot
    that the sync engine's sessions would not, and task heartbeats, audit
    log rows and other writes that already go through the async engine
    behaved differently from their sync counterparts.
    (cherry picked from commit ed918278f2f09dc729a9fa3a45ad5ffba9a6b549)
    
    Co-authored-by: Dev-iL <[email protected]>
    Co-authored-by: Claude <[email protected]>
---
 airflow-core/src/airflow/settings.py          | 24 +++++++++++++++++-------
 airflow-core/tests/unit/core/test_settings.py |  6 ++++++
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/airflow-core/src/airflow/settings.py 
b/airflow-core/src/airflow/settings.py
index b374ed85afb..fc2612dca1d 100644
--- a/airflow-core/src/airflow/settings.py
+++ b/airflow-core/src/airflow/settings.py
@@ -498,6 +498,7 @@ def _configure_async_session() -> None:
         engine_args["pool_recycle"] = conf.getint("database", 
"SQL_ALCHEMY_POOL_RECYCLE", fallback=1800)
         engine_args["pool_pre_ping"] = conf.getboolean("database", 
"SQL_ALCHEMY_POOL_PRE_PING", fallback=True)
         engine_args["max_overflow"] = conf.getint("database", 
"SQL_ALCHEMY_MAX_OVERFLOW", fallback=10)
+    engine_args.update(_get_isolation_level_args(SQL_ALCHEMY_CONN_ASYNC))
 
     async_engine = create_async_metadata_engine(
         SQL_ALCHEMY_CONN_ASYNC,
@@ -672,17 +673,26 @@ def prepare_engine_args(disable_connection_pool=False, 
pool_class=None):
         engine_args["pool_pre_ping"] = pool_pre_ping
         engine_args["max_overflow"] = max_overflow
 
-    # The default isolation level for MySQL (REPEATABLE READ) can introduce 
inconsistencies when
-    # running multiple schedulers, as repeated queries on the same session may 
read from stale snapshots.
-    # 'READ COMMITTED' is the default value for PostgreSQL.
-    # More information here:
-    # 
https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html
-    if SQL_ALCHEMY_CONN.startswith("mysql"):
-        engine_args["isolation_level"] = "READ COMMITTED"
+    engine_args.update(_get_isolation_level_args(SQL_ALCHEMY_CONN))
 
     return engine_args
 
 
+def _get_isolation_level_args(sql_alchemy_conn: str) -> dict[str, str]:
+    """
+    Return the isolation level engine argument for the metadata database 
connection.
+
+    The default isolation level for MySQL (REPEATABLE READ) can introduce 
inconsistencies when
+    running multiple schedulers, as repeated queries on the same session may 
read from stale snapshots.
+    'READ COMMITTED' is the default value for PostgreSQL.
+    More information here:
+    
https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html
+    """
+    if sql_alchemy_conn.startswith("mysql"):
+        return {"isolation_level": "READ COMMITTED"}
+    return {}
+
+
 def dispose_orm(do_log: bool = True):
     """Properly close pooled database connections."""
     global Session, engine, NonScopedSession, async_engine, AsyncSession
diff --git a/airflow-core/tests/unit/core/test_settings.py 
b/airflow-core/tests/unit/core/test_settings.py
index e6802350e40..28716ce2d1c 100644
--- a/airflow-core/tests/unit/core/test_settings.py
+++ b/airflow-core/tests/unit/core/test_settings.py
@@ -761,6 +761,12 @@ def 
test_async_pool_is_closed_before_process_shutdown(driver):
         pytest.param(
             "mysql+aiomysql", "SELECT @@session.time_zone", "+00:00", 
marks=pytest.mark.backend("mysql")
         ),
+        pytest.param(
+            "mysql+aiomysql",
+            "SELECT @@session.transaction_isolation",
+            "READ-COMMITTED",
+            marks=pytest.mark.backend("mysql"),
+        ),
         pytest.param("sqlite+aiosqlite", "PRAGMA foreign_keys", 1, 
marks=pytest.mark.backend("sqlite")),
     ],
 )

Reply via email to