Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: handle closed DuckDB connections gracefully (#7111) (#7114)

Myles Scolnickd500207c9efd
diff --git a/marimo/_data/get_datasets.py b/marimo/_data/get_datasets.py
index 04da219334..f077359e22 100644
--- a/marimo/_data/get_datasets.py
+++ b/marimo/_data/get_datasets.py
@@ -98,7 +98,15 @@ def execute_duckdb_query(
return duckdb.execute(query).fetchall()
return connection.execute(query).fetchall()
- except Exception:
+ except Exception as e:
+ if DependencyManager.duckdb.has():
+ import duckdb
+
+ # Connection is closed, return empty result
+ if isinstance(e, duckdb.ConnectionException):
+ LOGGER.debug("Skipping query on closed DuckDB connection")
+ return []
+
LOGGER.exception("Failed to execute DuckDB query %s", query)
return []
@@ -156,6 +164,14 @@ def _get_databases_from_duckdb_internal(
if DependencyManager.duckdb.has():
import duckdb
+ # Connection is closed, skip gracefully
+ if isinstance(e, duckdb.ConnectionException):
+ LOGGER.debug(
+ "Skipping closed DuckDB connection for engine %s",
+ engine_name,
+ )
+ return []
+
# Certain ducklakes don't support SHOW ALL TABLES
if isinstance(e, duckdb.NotImplementedException):
return get_duckdb_databases_agg_query(connection, engine_name)
@@ -424,7 +440,15 @@ def _get_duckdb_database_names(
if not internal:
database_names.append(database_name)
return database_names
- except Exception:
+ except Exception as e:
+ if DependencyManager.duckdb.has():
+ import duckdb
+
+ # Connection is closed, skip gracefully
+ if isinstance(e, duckdb.ConnectionException):
+ LOGGER.debug("Skipping closed DuckDB connection")
+ return []
+
LOGGER.debug("Failed to get database names from DuckDB")
return []
diff --git a/tests/_data/test_get_datasets.py b/tests/_data/test_get_datasets.py
index 601158e83f..9d46a3f839 100644
--- a/tests/_data/test_get_datasets.py
+++ b/tests/_data/test_get_datasets.py
@@ -571,3 +571,44 @@ def test_get_databases_agg_query() -> None:
]
connection.execute(cleanup_query)
+
+
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+def test_get_databases_with_closed_connection() -> None:
+ """Test that closed connections are handled gracefully without errors."""
+ import duckdb
+
+ # Create a connection, add tables, then close it
+ connection = duckdb.connect(":memory:")
+ connection.execute("CREATE TABLE test_table (id INTEGER, name VARCHAR)")
+ connection.close()
+
+ # This should not raise an exception
+ result = get_databases_from_duckdb(
+ connection=connection, engine_name=VariableName("closed_engine")
+ )
+
+ # Should return empty list for closed connection
+ assert result == []
+
+
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+def test_get_databases_with_context_manager_closed_connection() -> None:
+ """Test that connections closed via context manager are handled gracefully."""
+ import duckdb
+
+ # Use context manager to automatically close connection
+ with duckdb.connect(":memory:") as connection:
+ connection.execute(
+ "CREATE TABLE test_table (id INTEGER, name VARCHAR)"
+ )
+ # Store reference to connection
+ closed_conn = connection
+
+ # Connection is now closed, this should not raise an exception
+ result = get_databases_from_duckdb(
+ connection=closed_conn, engine_name=VariableName("closed_engine")
+ )
+
+ # Should return empty list for closed connection
+ assert result == []