Fixed connection leak in storage when task cancellation interrupts PRAGMA setup.
CI / Formatting (push) Failing after 19s
CI / Linting (push) Successful in 17s
CI / Tests (Python 3.12) (push) Failing after 51s
CI / Tests (Python 3.13) (push) Successful in 34s
CI / Tests (Python 3.14) (push) Failing after 46s
CI / Type Checking (push) Successful in 22s
CI / Spelling (push) Successful in 20s

This commit is contained in:
2026-04-04 18:44:25 -04:00
parent 09af238c8b
commit 15e9cd6131
+14 -3
View File
@@ -16,6 +16,8 @@
from __future__ import annotations
import asyncio
import contextlib
import contextvars
import logging
from contextlib import asynccontextmanager
@@ -242,9 +244,18 @@ class ModuleStorage:
return self._conn
self._db_path.parent.mkdir(parents=True, exist_ok=True)
conn = await aiosqlite.connect(self._db_path)
conn.row_factory = aiosqlite.Row
await conn.execute("PRAGMA journal_mode = WAL")
await conn.execute("PRAGMA foreign_keys = ON")
try:
conn.row_factory = aiosqlite.Row
await conn.execute("PRAGMA journal_mode = WAL")
await conn.execute("PRAGMA foreign_keys = ON")
except BaseException:
# Setup interrupted (timeout, cancellation, etc.). Close the
# connection to release any write lock held by a partially-
# executed PRAGMA. shield() keeps the close running on the
# background thread even if this task is cancelled.
with contextlib.suppress(asyncio.CancelledError, Exception):
await asyncio.shield(conn.close())
raise
self._conn = conn
self._logger.info(f"Database opened at: {self._db_path.absolute()}")
return conn