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 -1
View File
@@ -8,6 +8,7 @@ import sqlite3
import time
from typing import Any, List, Optional
from ..utils.cache_db import closing_connection
from ..utils.cache_paths import get_cache_base_dir
logger = logging.getLogger(__name__)
@@ -127,7 +128,7 @@ class DownloadQueueService:
def _initialize_schema(self) -> None:
if self._schema_initialized:
return
with self._connect() as conn:
with closing_connection(self._connect()) as conn:
conn.executescript(self._SCHEMA_TABLES)
# Databases created by older versions lack