Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(core): close the omission store's connection when opening it fails (#2555)

Raghav Chamadiyadf1585bfe12e
diff --git a/packages/core/src/repowise/core/distill/store.py b/packages/core/src/repowise/core/distill/store.py
index 973f5a704..d9dac3bc8 100644
--- a/packages/core/src/repowise/core/distill/store.py
+++ b/packages/core/src/repowise/core/distill/store.py
@@ -107,8 +107,13 @@ class OmissionStore:
self.max_mb = max_mb
db_path.parent.mkdir(parents=True, exist_ok=True)
self._conn = sqlite3.connect(db_path, isolation_level=None)
- apply_sqlite_pragmas(self._conn, _BUSY_TIMEOUT_MS)
- savings_schema.initialize_savings_schema(self._conn)
+ try:
+ apply_sqlite_pragmas(self._conn, _BUSY_TIMEOUT_MS)
+ savings_schema.initialize_savings_schema(self._conn)
+ except BaseException:
+ # A corrupt file fails here; the caller never gets a store to close.
+ self._conn.close()
+ raise
@classmethod
def open_default(cls, start: Path | None = None) -> OmissionStore:
diff --git a/tests/unit/distill/test_store.py b/tests/unit/distill/test_store.py
index 86e2548ab..df8d3f393 100644
--- a/tests/unit/distill/test_store.py
+++ b/tests/unit/distill/test_store.py
@@ -2,9 +2,13 @@
from __future__ import annotations
+import sqlite3
import time
from pathlib import Path
+import pytest
+
+from repowise.core.distill import store as store_module
from repowise.core.distill.store import OmissionStore, content_ref, default_store_path
@@ -212,3 +216,24 @@ def test_default_store_path_falls_back_to_home(tmp_path: Path) -> None:
path = default_store_path(tmp_path)
assert path.name == "omissions.db"
assert ".repowise" in str(path)
+
+
+def test_a_store_that_fails_to_open_closes_its_connection(tmp_path: Path, monkeypatch) -> None:
+ """A corrupt database file must not leak the connection opened for it."""
+ opened: list[sqlite3.Connection] = []
+ real_connect = sqlite3.connect
+
+ def _connect(*args, **kwargs):
+ conn = real_connect(*args, **kwargs)
+ opened.append(conn)
+ return conn
+
+ monkeypatch.setattr(store_module.sqlite3, "connect", _connect)
+ db = tmp_path / "omissions.db"
+ db.write_bytes(b"not a sqlite database" * 100)
+
+ with pytest.raises(sqlite3.DatabaseError):
+ OmissionStore(db)
+ assert len(opened) == 1
+ with pytest.raises(sqlite3.ProgrammingError):
+ opened[0].execute("SELECT 1")