This is an automated email from the ASF dual-hosted git repository. villebro pushed a commit to branch 7.0 in repository https://gitbox.apache.org/repos/asf/superset.git
commit 358ecc3ae8dc9ef149dd38efdd376ede08b16603 Author: Ville Brofeldt <[email protected]> AuthorDate: Sat Nov 14 10:25:36 2026 -0800 fix(models): close has_view connection (has_view part of #45861) Only the has_view connection leak and its test. The engine-cache disposal on eviction is left out. (cherry picked from commit 50ea495285f587ef2708caeed67cd4abfb647fbf, reduced) --- superset/models/core.py | 18 +++++++++--------- tests/unit_tests/models/core_test.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/superset/models/core.py b/superset/models/core.py index a700e44d6c8..ab484f7409c 100755 --- a/superset/models/core.py +++ b/superset/models/core.py @@ -1492,15 +1492,15 @@ class Database(CoreDatabase, AuditMixinNullable, ImportExportMixin): # pylint: def has_view(self, table: Table) -> bool: with self.get_sqla_engine(catalog=table.catalog, schema=table.schema) as engine: - connection = engine.connect() - try: - views = engine.dialect.get_view_names( - connection=connection, - schema=table.schema, - ) - except Exception: # pylint: disable=broad-except - logger.warning("Has view failed", exc_info=True) - views = [] + with engine.connect() as connection: + try: + views = engine.dialect.get_view_names( + connection=connection, + schema=table.schema, + ) + except Exception: # pylint: disable=broad-except + logger.warning("Has view failed", exc_info=True) + views = [] return table.table in views diff --git a/tests/unit_tests/models/core_test.py b/tests/unit_tests/models/core_test.py index 4a92922f01f..50633300423 100644 --- a/tests/unit_tests/models/core_test.py +++ b/tests/unit_tests/models/core_test.py @@ -17,6 +17,7 @@ # pylint: disable=import-outside-toplevel from datetime import datetime +from pathlib import Path from typing import Any, Callable import numpy @@ -2554,3 +2555,34 @@ def test_function_names_returns_empty_list_when_engine_spec_raises( assert database.function_names == [] assert logger.error.called + + +def _duckdb_database(path: Path, threads: int) -> Database: + """A saved DuckDB database whose connection settings differ by ``threads``.""" + database = Database( + database_name="duck", + sqlalchemy_uri=f"duckdb:///{path}", + extra=json.dumps( + {"engine_params": {"connect_args": {"config": {"threads": threads}}}} + ), + ) + database.id = 1 + return database + + +def _query_duckdb(database: Database) -> None: + with database.get_sqla_engine() as engine, engine.connect() as connection: + connection.execute(select(1)) + + +def test_has_view_closes_its_connection(tmp_path: Path) -> None: + """ + With the default ``NullPool`` a connection is only closed once it is + released. ``has_view`` must release the connection it opens: otherwise the + DuckDB database stays open with its old configuration, and opening it again + with edited settings fails until the leaked connection is garbage collected. + """ + path = tmp_path / "db.duckdb" + _duckdb_database(path, threads=1).has_view(Table("missing")) + + _query_duckdb(_duckdb_database(path, threads=2))
