mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-02 04:46:18 -05:00
All three bugs confirmed; the two suggestions taken. [bug] `video_modeler` awaited ffprobe -- a subprocess per video, up to 20 per batch -- inside its `commit=True` session. With IMMEDIATE as the default that holds the write lock for the whole batch, which is the same starvation this branch is curing in `video_cleanup`. (It was already partly true before: the batch's first `session.flush([video])` took the lock ahead of the remaining ffprobes.) The batch is now read, ffprobed with no transaction open, then written. `get_or_create_ffprobe_json` in modules/videos/common.py is the path-only entry point that makes that possible; `Video.get_ffprobe_json` delegates to it and keeps appending the cache file to its FileGroup. `validate()` -- and the poster generation it can trigger -- is still inside the write transaction, and now says so. [bug] `test_download_dispatched_outside_immediate_transaction` probed `getattr(_immediate_txn, 'active', False)`, which a ContextVar never has, so it passed unconditionally. It now probes the actual write lock from a competing connection under `production_like_sessions`. Mutation-checked: moving the dispatch back inside the claim transaction fails it. [bug] `asyncio.create_task` snapshots the current Context, so a flag held for a write session's lifetime was inherited by every task spawned inside it -- and the parent's `reset()` cannot reach a child's copy, leaving that task issuing BEGIN IMMEDIATE for its *read* sessions. `create_downloads` spawns `dispatch_downloads` exactly that way, so every user-created download hit it. The flag now lives only across the session's opening `connection()` call, which also means the write lock is taken when the session opens rather than at the caller's first statement. [suggestion] `renew_recurring_downloads` scanned every recurring Download under the write lock, including the usual cycle where nothing is due. It reads first now and takes the lock only for the rows that change, re-checking status since the dispatcher may have claimed one in between. [nit] Parenthesized the directory OR in the claim join. `probe_write_lock_is_held` moved to conftest alongside `write_lock_held_briefly`; three suites use it now. Suite: 1712 passed, 5 skipped. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
162 lines
7.5 KiB
Python
162 lines
7.5 KiB
Python
import asyncio
|
|
import sqlite3
|
|
|
|
import pytest
|
|
|
|
from wrolpi.conftest import production_like_sessions, probe_write_lock_is_held
|
|
from wrolpi.db import get_db_session, _configure_sqlite_connection
|
|
|
|
|
|
class _FakeCursor:
|
|
"""A minimal DBAPI cursor that records executed statements and simulates WAL rejection.
|
|
|
|
A FAT/exFAT/NTFS drive rejects WAL either by raising `disk I/O error` (`fail_wal`) or by
|
|
silently keeping the current rollback journal — `PRAGMA journal_mode` returns the mode SQLite
|
|
actually selected, so a WAL request can return e.g. 'delete' without raising (`wal_returns`)."""
|
|
|
|
def __init__(self, fail_wal: bool, wal_returns: str = 'wal'):
|
|
self.fail_wal = fail_wal
|
|
self.wal_returns = wal_returns
|
|
self.executed = []
|
|
self._last_row = None
|
|
|
|
def execute(self, statement):
|
|
if statement == 'PRAGMA journal_mode=WAL':
|
|
if self.fail_wal:
|
|
raise sqlite3.OperationalError('disk I/O error')
|
|
self._last_row = (self.wal_returns,)
|
|
elif statement.startswith('PRAGMA journal_mode='):
|
|
self._last_row = (statement.split('=', 1)[1].lower(),)
|
|
else:
|
|
self._last_row = None
|
|
self.executed.append(statement)
|
|
|
|
def fetchone(self):
|
|
return self._last_row
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
|
|
class _FakeConnection:
|
|
def __init__(self, fail_wal: bool, wal_returns: str = 'wal'):
|
|
self._cursor = _FakeCursor(fail_wal, wal_returns)
|
|
self.isolation_level = 'DEFERRED'
|
|
|
|
def cursor(self):
|
|
return self._cursor
|
|
|
|
|
|
def test_configure_connection_uses_wal_when_supported():
|
|
"""On a normal filesystem the connection is configured for WAL + synchronous=NORMAL."""
|
|
conn = _FakeConnection(fail_wal=False)
|
|
mode = _configure_sqlite_connection(conn)
|
|
assert mode == 'WAL'
|
|
assert conn.isolation_level is None
|
|
assert 'PRAGMA journal_mode=WAL' in conn._cursor.executed
|
|
assert 'PRAGMA synchronous=NORMAL' in conn._cursor.executed
|
|
assert 'PRAGMA busy_timeout=30000' in conn._cursor.executed
|
|
assert 'PRAGMA foreign_keys=ON' in conn._cursor.executed
|
|
|
|
|
|
def test_configure_connection_falls_back_to_rollback_journal_on_wal_failure():
|
|
"""When WAL raises `disk I/O error` (exFAT/FAT/NTFS), fall back to a crash-safe rollback journal.
|
|
|
|
Regression test for the whole-API outage on 10.0.0.8: an exFAT media drive made
|
|
`PRAGMA journal_mode=WAL` raise on every connection. The connection must still come up, using
|
|
a TRUNCATE rollback journal with synchronous=FULL (WROLPi is off-grid; power loss is expected)."""
|
|
conn = _FakeConnection(fail_wal=True)
|
|
mode = _configure_sqlite_connection(conn)
|
|
assert mode == 'TRUNCATE'
|
|
assert 'PRAGMA journal_mode=TRUNCATE' in conn._cursor.executed
|
|
assert 'PRAGMA synchronous=FULL' in conn._cursor.executed
|
|
# WAL's less-durable synchronous setting must NOT be applied on a rollback journal.
|
|
assert 'PRAGMA synchronous=NORMAL' not in conn._cursor.executed
|
|
# The connection is still fully configured.
|
|
assert 'PRAGMA busy_timeout=30000' in conn._cursor.executed
|
|
assert 'PRAGMA foreign_keys=ON' in conn._cursor.executed
|
|
|
|
|
|
def test_configure_connection_falls_back_when_wal_silently_refused():
|
|
"""SQLite can refuse WAL WITHOUT raising, returning the still-active rollback mode instead.
|
|
|
|
The requested mode is not always the applied mode, so we must confirm `PRAGMA journal_mode`'s
|
|
return value. Here WAL 'succeeds' but reports 'delete'; that must trigger the durable fallback
|
|
(TRUNCATE + synchronous=FULL), not cache WAL with the weaker synchronous=NORMAL."""
|
|
conn = _FakeConnection(fail_wal=False, wal_returns='delete')
|
|
mode = _configure_sqlite_connection(conn)
|
|
assert mode == 'TRUNCATE'
|
|
assert 'PRAGMA journal_mode=TRUNCATE' in conn._cursor.executed
|
|
assert 'PRAGMA synchronous=FULL' in conn._cursor.executed
|
|
assert 'PRAGMA synchronous=NORMAL' not in conn._cursor.executed
|
|
|
|
|
|
def test_configure_connection_does_not_reattempt_wal_once_ruled_out():
|
|
"""Once WAL is known-unavailable for an engine, later connections skip (and don't re-log) it."""
|
|
conn = _FakeConnection(fail_wal=True)
|
|
# `journal_mode='TRUNCATE'` is the remembered decision from a previous connection.
|
|
mode = _configure_sqlite_connection(conn, journal_mode='TRUNCATE')
|
|
assert mode == 'TRUNCATE'
|
|
assert 'PRAGMA journal_mode=WAL' not in conn._cursor.executed
|
|
assert 'PRAGMA journal_mode=TRUNCATE' in conn._cursor.executed
|
|
|
|
|
|
def test_write_session_takes_write_lock_up_front(test_session):
|
|
"""`get_db_session(commit=True)` must acquire the SQLite write lock at BEGIN.
|
|
|
|
Regression test for `database is locked` on `UPDATE download SET last_download_attempt`:
|
|
a deferred (read-then-write) transaction that tries to upgrade its lock while another
|
|
connection holds the write lock gets SQLITE_BUSY *immediately* — busy_timeout is ignored
|
|
for lock upgrades to avoid deadlock. Taking the write lock up front (BEGIN IMMEDIATE)
|
|
lets busy_timeout absorb the contention instead of erroring instantly. `commit=True` is
|
|
the declaration of write intent, so it is what arms this — no opt-in helper to forget.
|
|
"""
|
|
db_file = test_session.get_bind().url.database
|
|
|
|
with production_like_sessions(test_session):
|
|
with get_db_session(commit=True) as session:
|
|
# Trigger the transaction's BEGIN with a trivial read (as the download dispatcher
|
|
# does before writing). This session already holds the write lock.
|
|
session.execute('SELECT 1')
|
|
assert probe_write_lock_is_held(db_file), \
|
|
'immediate session did not hold the write lock after BEGIN'
|
|
|
|
|
|
def test_read_session_does_not_take_write_lock(test_session):
|
|
"""A read-only `get_db_session()` must stay deferred so WAL read-concurrency is preserved."""
|
|
db_file = test_session.get_bind().url.database
|
|
|
|
with production_like_sessions(test_session):
|
|
with get_db_session() as session:
|
|
session.execute('SELECT 1')
|
|
assert not probe_write_lock_is_held(db_file), \
|
|
'read-only session unexpectedly holds the write lock'
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_background_task_does_not_inherit_write_intent(test_session):
|
|
"""A task spawned while a write session is open must not inherit its write intent.
|
|
|
|
`asyncio.create_task` copies the current Context, so a ContextVar set for the duration of a
|
|
write session is inherited by every background task the session's code starts -- permanently,
|
|
because the parent's `reset()` cannot reach the child's copy. `create_downloads` spawns
|
|
`dispatch_downloads` exactly that way, so every user-created download would leave that task
|
|
issuing BEGIN IMMEDIATE for its *read* sessions, serializing reads behind the write lock.
|
|
"""
|
|
db_file = test_session.get_bind().url.database
|
|
held_during_child_read = dict()
|
|
|
|
async def child():
|
|
with get_db_session() as session: # A read session: it must stay deferred.
|
|
session.execute('SELECT 1')
|
|
held_during_child_read['held'] = probe_write_lock_is_held(db_file)
|
|
|
|
with production_like_sessions(test_session):
|
|
with get_db_session(commit=True) as session:
|
|
session.execute('SELECT 1')
|
|
task = asyncio.create_task(child())
|
|
# The write session has committed; only the child's own session can hold the lock now.
|
|
await task
|
|
|
|
assert held_during_child_read['held'] is False, \
|
|
'a read session in a spawned task took the write lock, inheriting the parent write intent'
|