From d860a008a1cc9eb59df81aea1cf9edfd7395c730 Mon Sep 17 00:00:00 2001 From: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> Date: Sun, 14 Jun 2026 13:12:00 -0300 Subject: [PATCH] fix: close blob seq sqlite migration connection --- mempalace/backends/chroma.py | 2 +- tests/test_backends.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/mempalace/backends/chroma.py b/mempalace/backends/chroma.py index 06b71ba648..15e074a4e4 100644 --- a/mempalace/backends/chroma.py +++ b/mempalace/backends/chroma.py @@ -1073,7 +1073,7 @@ def _fix_blob_seq_ids(palace_path: str) -> None: if os.path.isfile(marker): return try: - with sqlite3.connect(db_path) as conn: + with contextlib.closing(sqlite3.connect(db_path)) as conn: try: rows = conn.execute( "SELECT rowid, seq_id FROM embeddings WHERE typeof(seq_id) = 'blob'" diff --git a/tests/test_backends.py b/tests/test_backends.py index 8af4f2bba9..a7be828da6 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -768,6 +768,33 @@ def test_fix_blob_seq_ids_writes_marker_when_already_integer(tmp_path): assert marker.is_file(), "marker must be written even when no BLOBs found" +def test_fix_blob_seq_ids_closes_sqlite_connection(tmp_path, monkeypatch): + """The migration closes sqlite connections after the pre-open probe.""" + db_path = tmp_path / "chroma.sqlite3" + with closing(sqlite3.connect(str(db_path))) as conn: + conn.execute("CREATE TABLE embeddings (rowid INTEGER PRIMARY KEY, seq_id INTEGER)") + conn.execute("INSERT INTO embeddings (seq_id) VALUES (42)") + conn.commit() + + closed = [] + real_connect = sqlite3.connect + + class TrackingConnection(sqlite3.Connection): + def close(self): + closed.append(True) + super().close() + + def tracking_connect(*args, **kwargs): + kwargs["factory"] = TrackingConnection + return real_connect(*args, **kwargs) + + monkeypatch.setattr("mempalace.backends.chroma.sqlite3.connect", tracking_connect) + + _fix_blob_seq_ids(str(tmp_path)) + + assert closed == [True] + + def test_fix_blob_seq_ids_skips_sqlite_when_marker_present(tmp_path): """When the marker exists, ``_fix_blob_seq_ids`` does not open sqlite3.