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(test): prevent DuckDB state leaks and directory watcher races (#11054)

Shahmir Varqha7d7529a60c86
diff --git a/tests/_data/mocks.py b/tests/_data/mocks.py
index 42afb4fc29..2c4d53398e 100644
--- a/tests/_data/mocks.py
+++ b/tests/_data/mocks.py
@@ -71,7 +71,9 @@ def create_dataframes(
import polars as pl
duck_df = pl.DataFrame(data)
- relation = duckdb.sql("SELECT * FROM duck_df")
+ # Narwhals may create temporary views on a relation's connection.
+ # Keep those views out of DuckDB's shared default connection.
+ relation = duckdb.connect(":memory:").sql("SELECT * FROM duck_df")
del duck_df
dfs.append(relation)
diff --git a/tests/_data/test_mocks.py b/tests/_data/test_mocks.py
new file mode 100644
index 0000000000..d3e82cc740
--- /dev/null
+++ b/tests/_data/test_mocks.py
@@ -0,0 +1,38 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+import pytest
+
+from tests._data.mocks import create_dataframes
+
+
[email protected]("duckdb", "polars")
+def test_duckdb_dataframe_has_isolated_connection() -> None:
+ import duckdb
+
+ default_tables = duckdb.sql("SHOW TABLES").fetchall()
+ (relation,) = create_dataframes({"value": [1, 2]}, include=["duckdb"])
+ assert isinstance(relation, duckdb.DuckDBPyRelation)
+
+ # Relation.query creates a temporary view, as Narwhals' timezone lookup does.
+ assert relation.query(
+ "fixture_view", "SELECT * FROM fixture_view"
+ ).fetchall() == [
+ (1,),
+ (2,),
+ ]
+ assert duckdb.sql("SHOW TABLES").fetchall() == default_tables
+
+ (other_relation,) = create_dataframes({"value": [3]}, include=["duckdb"])
+ assert isinstance(other_relation, duckdb.DuckDBPyRelation)
+ assert other_relation.query(
+ "fixture_view", "SELECT * FROM fixture_view"
+ ).fetchall() == [
+ (3,),
+ ]
+ assert relation.query(
+ "fixture_view", "SELECT * FROM fixture_view"
+ ).fetchall() == [
+ (1,),
+ (2,),
+ ]
diff --git a/tests/_save/test_hash.py b/tests/_save/test_hash.py
index 788990c88f..4bf9cb90f0 100644
--- a/tests/_save/test_hash.py
+++ b/tests/_save/test_hash.py
@@ -1700,10 +1700,34 @@ class TestSideEffects:
@staticmethod
@pytest.mark.usefixtures("cleanup_watchers")
async def test_side_effect_directory(
- k: Kernel, exec_req: ExecReqProvider, tmp_path
+ k: Kernel,
+ exec_req: ExecReqProvider,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
) -> None:
- control_requests = []
- k.enqueue_control_request = lambda req: control_requests.append(req) # type: ignore
+ from marimo._runtime.commands import CommandMessage
+ from marimo._runtime.watch import _directory
+
+ loop = asyncio.get_running_loop()
+ watcher_ready = asyncio.Event()
+ request_received = asyncio.Event()
+ control_requests: list[CommandMessage] = []
+ original_hashable_walk = _directory.hashable_walk
+
+ def hashable_walk(
+ path: Path,
+ ) -> set[tuple[Path, tuple[str], tuple[str]]]:
+ structure = original_hashable_walk(path)
+ # The watcher must capture its baseline before we change the directory.
+ loop.call_soon_threadsafe(watcher_ready.set)
+ return structure
+
+ def enqueue_control_request(request: CommandMessage) -> None:
+ control_requests.append(request)
+ loop.call_soon_threadsafe(request_received.set)
+
+ monkeypatch.setattr(_directory, "hashable_walk", hashable_walk)
+ k.enqueue_control_request = enqueue_control_request
await k.run(
[
exec_req.get(
@@ -1747,8 +1771,9 @@ class TestSideEffects:
"""),
]
)
+ await asyncio.wait_for(watcher_ready.wait(), timeout=5)
(tmp_path / "test_dir" / "test.txt").write_text("test")
- await asyncio.sleep(0.25)
+ await asyncio.wait_for(request_received.wait(), timeout=5)
assert len(control_requests) == 1
assert isinstance(control_requests[0], ExecuteStaleCellsCommand)
assert k.graph.cells[r.cell_id].stale
diff --git a/tests/_sql/test_duckdb.py b/tests/_sql/test_duckdb.py
index 208971c1f3..17886d6342 100644
--- a/tests/_sql/test_duckdb.py
+++ b/tests/_sql/test_duckdb.py
@@ -174,34 +174,37 @@ def test_duckdb_engine_get_databases_no_conn() -> None:
engine.execute(
"CREATE TABLE test (id INTEGER PRIMARY KEY, name VARCHAR(255))"
)
- engine.execute(
- """
- INSERT INTO test VALUES
- (1, 'Alice'),
- (2, 'Bob'),
- (3, 'Charlie');
- """
- )
- databases = engine.get_databases(
- include_schemas=True, include_tables=True, include_table_details=True
- )
-
- expected_databases = deepcopy(expected_databases_with_conn)
- expected_databases[0].engine = None
- expected_databases[0].schemas[0].tables[0].engine = None
- expected_databases[0].schemas[0].tables[0].source_type = "duckdb"
- expected_databases[0].schemas[0].tables[0].source = "memory"
+ try:
+ engine.execute(
+ """
+ INSERT INTO test VALUES
+ (1, 'Alice'),
+ (2, 'Bob'),
+ (3, 'Charlie');
+ """
+ )
+ databases = engine.get_databases(
+ include_schemas=True,
+ include_tables=True,
+ include_table_details=True,
+ )
- assert databases == [
- *expected_databases,
- Database(
- name="temp",
- dialect="duckdb",
- schemas=[Schema(name="main", tables=[])],
- ),
- ]
+ expected_databases = deepcopy(expected_databases_with_conn)
+ expected_databases[0].engine = None
+ expected_databases[0].schemas[0].tables[0].engine = None
+ expected_databases[0].schemas[0].tables[0].source_type = "duckdb"
+ expected_databases[0].schemas[0].tables[0].source = "memory"
- engine.execute("DROP TABLE test")
+ assert databases == [
+ *expected_databases,
+ Database(
+ name="temp",
+ dialect="duckdb",
+ schemas=[Schema(name="main", tables=[])],
+ ),
+ ]
+ finally:
+ engine.execute("DROP TABLE test")
@pytest.mark.skipif(not HAS_DUCKDB, reason="duckdb not installed")
diff --git a/tests/_sql/test_get_engines.py b/tests/_sql/test_get_engines.py
index 9f91469adf..529b6b1a78 100644
--- a/tests/_sql/test_get_engines.py
+++ b/tests/_sql/test_get_engines.py
@@ -356,30 +356,32 @@ def test_get_engines_duckdb_databases() -> None:
):
sql("CREATE TABLE test_table (id INTEGER);")
- # Reload the connection to get the new table
- connection = engine_to_data_source_connection(
- VariableName("my_duckdb"), duckdb_engine
- )
+ try:
+ # Reload the connection to get the new table
+ connection = engine_to_data_source_connection(
+ VariableName("my_duckdb"), duckdb_engine
+ )
- assert [database.name for database in connection.databases] == [
- "memory",
- "temp",
- ]
- database = connection.databases[0]
- assert database.name == "memory"
- assert len(database.schemas) == 1
- schema = database.schemas[0]
- assert schema.name == "main"
- assert len(schema.tables) == 1
- table = schema.tables[0]
- assert table.name == "test_table"
- assert table.columns == []
- assert table.num_columns is None
- with patch(
- "marimo._sql.sql.get_configured_sql_output_format",
- return_value="native",
- ):
- sql("DROP TABLE test_table;")
+ assert [database.name for database in connection.databases] == [
+ "memory",
+ "temp",
+ ]
+ database = connection.databases[0]
+ assert database.name == "memory"
+ assert len(database.schemas) == 1
+ schema = database.schemas[0]
+ assert schema.name == "main"
+ assert len(schema.tables) == 1
+ table = schema.tables[0]
+ assert table.name == "test_table"
+ assert table.columns == []
+ assert table.num_columns is None
+ finally:
+ with patch(
+ "marimo._sql.sql.get_configured_sql_output_format",
+ return_value="native",
+ ):
+ sql("DROP TABLE test_table;")
@pytest.mark.skipif(not HAS_SQLALCHEMY, reason="SQLAlchemy not installed")