"""Durable Optuna storage closes every SQLite handle on each exit path. Python 3.13+ reports an unclosed ``sqlite3.Connection`` as an unraisable ResourceWarning at finalization; older interpreters stay silent, so the contract is asserted directly on the DB-API handles as well. """ import gc import sqlite3 from types import SimpleNamespace import warnings import optuna import pytest import torch from obliteratus import bayesian_optimizer as bo from obliteratus.abliterate import AbliterationPipeline from tests.test_bayesian_optimizer_optuna import _TinyModel, _TinyTokenizer pytestmark = pytest.mark.cpu def _is_closed(connection: sqlite3.Connection) -> bool: try: connection.execute("SELECT 1") except sqlite3.ProgrammingError: return True return False @pytest.fixture def opened(monkeypatch): """Record every SQLite handle and engine the durable study storage opens.""" handles = [] engines = [] real_connect = sqlite3.connect def recording_connect(*args, **kwargs): handle = real_connect(*args, **kwargs) handles.append(handle) return handle class RecordingStorage(optuna.storages.RDBStorage): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) engines.append(self.engine) # SQLAlchemy's pysqlite dialect connects through ``sqlite3.dbapi2``. monkeypatch.setattr(sqlite3, "connect", recording_connect) monkeypatch.setattr(sqlite3.dbapi2, "connect", recording_connect) monkeypatch.setattr(optuna.storages, "RDBStorage", RecordingStorage) return SimpleNamespace(handles=handles, engines=engines) def _pipeline(tmp_path): pipeline = AbliterationPipeline( model_name="tiny-local", method="optimized", output_dir=str(tmp_path), use_sae_features=False, attention_head_surgery=False, per_expert_directions=False, ) pipeline.handle = SimpleNamespace( model=_TinyModel(), tokenizer=_TinyTokenizer(), architecture="llama" ) pipeline._strong_layers = [0] pipeline.refusal_directions = {0: torch.tensor([1.0, 0.0])} pipeline.harmful_prompts = ["A local test prompt"] pipeline._free_gpu_memory = lambda: None return pipeline def _fail_kl(error): def measure(*_args, **_kwargs): raise error return measure @pytest.mark.parametrize("outcome", ["success", "failure", "cancellation"]) def test_durable_study_closes_every_sqlite_connection( tmp_path, monkeypatch, opened, outcome, ): pipeline = _pipeline(tmp_path) if outcome == "failure": monkeypatch.setattr(bo, "_measure_kl_divergence", _fail_kl(RuntimeError("trial failed"))) elif outcome == "cancellation": monkeypatch.setattr(bo, "_measure_kl_divergence", _fail_kl(KeyboardInterrupt())) with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always", ResourceWarning) try: if outcome == "success": assert bo.run_bayesian_optimization( pipeline, n_trials=2, n_refusal_prompts=1, n_kl_prompts=1 ) else: expected = RuntimeError if outcome == "failure" else KeyboardInterrupt with pytest.raises(expected): bo.run_bayesian_optimization( pipeline, n_trials=2, n_refusal_prompts=1, n_kl_prompts=1 ) assert len(opened.engines) == 1 assert opened.handles, "the durable SQLite study must have been opened" assert all(_is_closed(handle) for handle in opened.handles) assert opened.engines[0].pool.checkedout() == 0 opened.handles.clear() gc.collect() finally: bo._reset_bayesian_state(pipeline) leaks = [w for w in caught if issubclass(w.category, ResourceWarning)] assert leaks == [] assert list((tmp_path / ".obliteratus-bayesian").glob("*.sqlite3")) def test_snapshot_backup_closes_both_sqlite_connections(tmp_path, opened): source = tmp_path / "live.sqlite3" connection = sqlite3.connect(source) try: with connection: connection.execute("CREATE TABLE evidence (value TEXT)") connection.execute("INSERT INTO evidence VALUES ('completed')") finally: connection.close() opened.handles.clear() bo._atomic_sqlite_backup(source, tmp_path / "snapshot.sqlite3") assert len(opened.handles) == 2 assert all(_is_closed(handle) for handle in opened.handles)