Files
OBLITERATUS/tests/test_bayesian_storage_close.py
Joseph MaglyandClaude Opus 5.5 08a04e3bd4 fix(bayesian): close Optuna SQLite storage and backup connections
On Python 3.13+, an sqlite3 connection that is garbage-collected
without being closed raises ResourceWarning, which the project's
filterwarnings=error turns into test failures. pyproject declares
requires-python >=3.11 with no upper bound, so these versions must work.

- The durable study's RDBStorage is now owned by an ExitStack in both
  public entry points and closed (remove_session, then engine.dispose)
  on success, failure and cancellation.
- _atomic_sqlite_backup closes both handles with contextlib.closing;
  sqlite3's own context manager only ends the transaction.
- Two tests that reopened or connected to study databases now close
  their handles.
- New regression test tracks every sqlite3 handle opened during a real
  durable study (success, failure, KeyboardInterrupt) and the backup,
  asserting all are closed independent of the 3.13+ warning.

Storage URLs, file names, snapshots and resume behaviour are unchanged;
as with any edit to this module, the study identity (which hashes the
module source) changes, so pre-existing durable studies start fresh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:43:05 -05:00

129 lines
4.5 KiB
Python

"""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)