Skip to content

Commit 2514d48

Browse files
fix(tests): install DuckLake once per process
Windows denies the file move in install_extension while a previous connection still has ducklake.dll mapped, and duckdb_extensions().installed does not stop that second install. Load first, install only if that fails, and pre-install the extension before Windows pytest. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent b7aa915 commit 2514d48

3 files changed

Lines changed: 73 additions & 19 deletions

File tree

‎.github/workflows/pr.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ jobs:
150150
. ./venv/Scripts/activate
151151
python.exe -m pip install --upgrade pip
152152
make install-dev
153+
- name: Install DuckLake extension
154+
run: |
155+
. ./venv/Scripts/activate
156+
python -c "import duckdb; conn = duckdb.connect(); conn.install_extension('ducklake'); conn.load_extension('ducklake'); conn.close()"
153157
- name: Run fast unit tests
154158
run: |
155159
. ./venv/Scripts/activate

‎tests/conftest.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,8 @@ def reset_console():
275275
def duck_conn() -> t.Generator[duckdb.DuckDBPyConnection, None, None]:
276276
conn = duckdb.connect()
277277
yield conn
278-
# Explicitly close so that any extension DLLs loaded during the test are
279-
# released before teardown. On Windows an open DLL handle prevents
280-
# install_extension() from moving the newly-downloaded file into place,
281-
# causing an "Access is denied" IOException when subsequent tests in the
282-
# same xdist worker call install_extension() for the same extension.
278+
# Closing the connection does not unload extension DLLs. DuckDB keeps
279+
# those mapped for the rest of the process.
283280
conn.close()
284281

285282

‎tests/core/engine_adapter/test_duckdb.py‎

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import typing as t
22

3+
import duckdb # noqa: TID253
34
import pandas as pd # noqa: TID253
45
import pytest
56
from pytest_mock.plugin import MockerFixture
@@ -126,25 +127,77 @@ def test_drop_catalog_motherduck(make_mocked_engine_adapter: t.Callable) -> None
126127
assert to_sql_calls(adapter) == ['DROP DATABASE IF EXISTS "foo" CASCADE']
127128

128129

129-
@pytest.fixture
130-
def ducklake_conn(duck_conn):
131-
"""Load DuckLake without reinstalling it when a previous test already did.
130+
class DuckLakeInstallState:
131+
def __init__(self) -> None:
132+
self.ready = False
133+
134+
135+
_DUCKLAKE_STATE = DuckLakeInstallState()
136+
132137

133-
On Windows, install_extension moves a file into the extension directory.
134-
A ducklake.dll that is still mapped from an earlier test in this process
135-
makes that move fail with "Access is denied". Installing only when
136-
duckdb_extensions() says it is absent, then calling load_extension, avoids
137-
the second install while the DLL is still mapped.
138+
def _disable_extension_auto_install(conn: duckdb.DuckDBPyConnection) -> None:
139+
# LOAD of a missing extension otherwise downloads and moves the file, the
140+
# same Windows failure as install_extension.
141+
for setting in ("autoinstall_known_extensions", "autoload_known_extensions"):
142+
try:
143+
conn.execute(f"SET {setting} = false")
144+
except duckdb.Error:
145+
continue
146+
147+
148+
def ensure_ducklake(
149+
conn: duckdb.DuckDBPyConnection, state: t.Optional[DuckLakeInstallState] = None
150+
) -> None:
151+
"""Load DuckLake, installing it at most once per process.
152+
153+
install_extension replaces ducklake.duckdb_extension. On Windows that move
154+
fails with "Access is denied" while a DLL loaded by an earlier connection
155+
in this process is still mapped. A new connection can still report the
156+
extension as not installed, so the install decision cannot be made from
157+
duckdb_extensions() on every test.
138158
"""
139-
installed = duck_conn.execute(
140-
"SELECT installed FROM duckdb_extensions() WHERE extension_name = 'ducklake'"
141-
).fetchone()
142-
if not installed or not installed[0]:
143-
duck_conn.install_extension("ducklake")
144-
duck_conn.load_extension("ducklake")
159+
holder = _DUCKLAKE_STATE if state is None else state
160+
_disable_extension_auto_install(conn)
161+
if holder.ready:
162+
conn.load_extension("ducklake")
163+
return
164+
try:
165+
conn.load_extension("ducklake")
166+
except duckdb.Error:
167+
conn.install_extension("ducklake")
168+
conn.load_extension("ducklake")
169+
holder.ready = True
170+
171+
172+
@pytest.fixture
173+
def ducklake_conn(duck_conn):
174+
ensure_ducklake(duck_conn)
145175
return duck_conn
146176

147177

178+
def test_ensure_ducklake_loads_without_install_when_already_present(mocker: MockerFixture):
179+
conn = mocker.Mock()
180+
state = DuckLakeInstallState()
181+
182+
ensure_ducklake(conn, state)
183+
ensure_ducklake(conn, state)
184+
185+
conn.install_extension.assert_not_called()
186+
assert conn.load_extension.call_args_list == [mocker.call("ducklake"), mocker.call("ducklake")]
187+
188+
189+
def test_ensure_ducklake_installs_once_when_load_fails(mocker: MockerFixture):
190+
conn = mocker.Mock()
191+
conn.load_extension.side_effect = [duckdb.IOException("missing"), None, None]
192+
state = DuckLakeInstallState()
193+
194+
ensure_ducklake(conn, state)
195+
ensure_ducklake(conn, state)
196+
197+
conn.install_extension.assert_called_once_with("ducklake")
198+
assert conn.load_extension.call_count == 3
199+
200+
148201
@pytest.mark.xdist_group("ducklake_extension")
149202
def test_ducklake_partitioning(adapter: EngineAdapter, ducklake_conn, tmp_path):
150203
catalog = "a_ducklake_db"

0 commit comments

Comments
 (0)