fix(cache): close the connection the schema init opened

`with sqlite3.Connection` ends the transaction but keeps the file handle open,
so every cache class leaked one handle per instance until the garbage collector
happened to run. On Windows that makes the database file undeletable while the
process lives (WinError 32), which broke 30 cache tests in teardown and would
also block moving or clearing a cache at runtime.

All eight `with <connection>` sites now go through `closing_connection()`, and a
regression test asserts the schema init leaves no usable connection behind.
This commit is contained in:
Will Miao
2026-10-12 11:35:14 +08:00
parent 5fbac84910
commit fd40cd952b
7 changed files with 61 additions and 11 deletions
+30
View File
@@ -724,3 +724,33 @@ class TestHasWorkflowColumn:
assert loaded is not None
assert loaded.raw_data[0]["id"] == "legacy-1"
assert loaded.raw_data[0]["has_workflow"] is False
def test_schema_initialization_closes_its_connection(tmp_path, monkeypatch):
"""The schema init must not leak a file handle.
``with sqlite3.Connection`` only ends the transaction: the handle stays open,
and on Windows a live handle keeps the cache database undeletable
(``WinError 32``) until the garbage collector happens to run -- which broke
every teardown that removes a temporary cache file.
"""
import sqlite3
from py.services import persistent_recipe_cache as module
opened = []
real_connect = sqlite3.connect
def tracking_connect(*args, **kwargs):
conn = real_connect(*args, **kwargs)
opened.append(conn)
return conn
monkeypatch.setattr(sqlite3, "connect", tracking_connect)
module.PersistentRecipeCache(db_path=str(tmp_path / "recipe.sqlite"))
assert opened, "initializing the schema should have opened a connection"
for conn in opened:
with pytest.raises(sqlite3.ProgrammingError):
conn.execute("SELECT 1")