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
+2 -2
View File
@@ -19,7 +19,7 @@ import threading
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Set, Tuple
from ..utils.cache_db import connect_cache_db
from ..utils.cache_db import closing_connection, connect_cache_db
from ..utils.cache_paths import CacheType, resolve_cache_path_with_migration
from ..utils.file_lock import exclusive_lock
@@ -457,7 +457,7 @@ class PersistentRecipeCache:
if self._schema_initialized:
return
try:
with self._connect() as conn:
with closing_connection(self._connect()) as conn:
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA foreign_keys = ON")
conn.executescript(