Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/nooa/storage/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,11 +866,15 @@ def close(self) -> None:
if conn is not None:
if lock is not None:
with lock:
try:
conn.commit()
finally:
conn.close()
else:
try:
conn.commit()
finally:
conn.close()
else:
conn.commit()
conn.close()
finally:
if self._lock_fd is not None:
fcntl.flock(self._lock_fd, fcntl.LOCK_UN)
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_util_and_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,28 @@ def test_backend_uses_insertion_counter(self):
assert backend._insertion_counter == initial + 2
sm.close()

def test_close_closes_the_connection_when_commit_fails(self):
"""A raising commit() must not leak the connection.

close() sets _closed before committing, so a connection missed here can
never be closed by a later close() call -- it early-returns.
"""
import sqlite3

from nooa.storage.sqlite import SQLiteStorageManager

sm = SQLiteStorageManager(":memory:")
real_conn = sm._conn
try:
fake_conn = MagicMock(spec=sqlite3.Connection)
fake_conn.commit.side_effect = sqlite3.OperationalError("disk I/O error")
sm._conn = fake_conn
with pytest.raises(sqlite3.OperationalError):
sm.close()
assert fake_conn.close.called
finally:
real_conn.close()

def test_restore_snapshot_not_found_raises(self):
from nooa.errors.storage import SnapshotNotFoundError
from nooa.storage.sqlite import SQLiteStorageManager
Expand Down