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

bbovenzi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 8de681ba13f Fix DB connection exhaustion in the migration stairway 
test (#77254)
8de681ba13f is described below

commit 8de681ba13fcce40ebd50c3bb7d49b23acc90cc5
Author: Ephraim Anierobi <[email protected]>
AuthorDate: Thu Nov 19 00:51:31 2026 +0100

    Fix DB connection exhaustion in the migration stairway test (#77254)
    
    upgradedb() rebuilds the ORM from config when it leaves
    _single_connection_pool, so the NullPool the stairway fixture set with
    configure_orm(disable_connection_pool=True) only lasted until the first
    step. The session and migration-lock connection that upgradedb() keeps
    using are bound to the engine that rebuild disposed, so every step left
    about one idle connection in a pool that only a full garbage collection
    closes.
    
    Full collections are rare in the long "Other" test process, so on
    Postgres and MySQL the stairway ran out of connections partway through
    and left the database mid-migration for the tests after it.
---
 .../tests/unit/migrations/test_migration_utils.py  | 59 ++++++++++------------
 1 file changed, 26 insertions(+), 33 deletions(-)

diff --git a/airflow-core/tests/unit/migrations/test_migration_utils.py 
b/airflow-core/tests/unit/migrations/test_migration_utils.py
index 24a3ce580b4..7e4d89af151 100644
--- a/airflow-core/tests/unit/migrations/test_migration_utils.py
+++ b/airflow-core/tests/unit/migrations/test_migration_utils.py
@@ -56,6 +56,8 @@ from airflow.utils.db import (
     upgradedb,
 )
 
+from tests_common.test_utils.config import conf_vars
+
 # The stairway runs hundreds of upgrade/downgrade cycles. Each cycle goes
 # through ``_single_connection_pool`` which calls 
``settings.reconfigure_orm()``
 # at entry and exit, and ``dispose_orm()`` cannot synchronously close asyncpg
@@ -162,20 +164,18 @@ def stairway_db():
     """
     original_async_conn = settings.SQL_ALCHEMY_CONN_ASYNC
     settings.SQL_ALCHEMY_CONN_ASYNC = ""
-    # Use NullPool: every connection is closed immediately on return to the
-    # pool. Each upgrade/downgrade leaks ~one connection from various places
-    # (alembic's env.py, _configured_alembic_environment, external DB manager
-    # post-migration hooks); over hundreds of revisions a normal pool fills up
-    # and exhausts Postgres' max_connections.
-    settings.dispose_orm(do_log=False)
-    settings.configure_orm(disable_connection_pool=True)
     try:
-        downgrade(to_revision=_STAIRWAY_START_REVISION)
+        # Set NullPool in config, not with 
configure_orm(disable_connection_pool=True):
+        # upgradedb() rebuilds the ORM from config on leaving 
_single_connection_pool, yet keeps
+        # using the disposed engine, whose pooled connections then stay open 
until a full GC.
+        with conf_vars({("database", "sql_alchemy_pool_enabled"): "False"}):
+            settings.reconfigure_orm()
+            downgrade(to_revision=_STAIRWAY_START_REVISION)
 
-        yield
+            yield
 
-        # Restore to latest heads even if the test failed mid-way.
-        upgradedb()
+            # Restore to latest heads even if the test failed mid-way.
+            upgradedb()
     finally:
         settings.SQL_ALCHEMY_CONN_ASYNC = original_async_conn
         settings.reconfigure_orm()
@@ -198,28 +198,21 @@ def test_migration_stairway(stairway_db) -> None:
     revisions = _get_revisions_in_order()
 
     for revision_id in revisions:
-        try:
-            _run_step_with_retry(
-                partial(upgradedb, to_revision=revision_id),
-                revision_id=revision_id,
-                label="upgrade",
-            )
-            _run_step_with_retry(
-                partial(downgrade, to_revision="-1"),
-                revision_id=revision_id,
-                label="downgrade",
-            )
-            _run_step_with_retry(
-                partial(upgradedb, to_revision=revision_id),
-                revision_id=revision_id,
-                label="re-upgrade",
-            )
-        finally:
-            # upgradedb()/downgrade() each enter ``_single_connection_pool``,
-            # which calls ``reconfigure_orm()`` and resets the pool to default.
-            # Re-apply NullPool so the iteration cap is preserved.
-            settings.dispose_orm(do_log=False)
-            settings.configure_orm(disable_connection_pool=True)
+        _run_step_with_retry(
+            partial(upgradedb, to_revision=revision_id),
+            revision_id=revision_id,
+            label="upgrade",
+        )
+        _run_step_with_retry(
+            partial(downgrade, to_revision="-1"),
+            revision_id=revision_id,
+            label="downgrade",
+        )
+        _run_step_with_retry(
+            partial(upgradedb, to_revision=revision_id),
+            revision_id=revision_id,
+            label="re-upgrade",
+        )
 
 
 class TestDisableSqliteFkeys:

Reply via email to