Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
fix: roll back the ORM session when a migration section fails so its pending writes cannot leak or hold the sqlite write lock
Murtaza Nasira4b6b26dabbf
diff --git a/src/init_db.py b/src/init_db.pyindex 97b7289b..be0c678f 100644--- a/src/init_db.py+++ b/src/init_db.py@@ -159,6 +159,16 @@ def _migration_section(app, failures, name):failures.append((name, e))app.logger.error("Migration section '%s' failed: %s", name, e)app.logger.exception("Section '%s' traceback", name)+ # Discard whatever the failed section left pending in the ORM session.+ # Without this its open transaction keeps SQLite's write lock, so the+ # following sections fail with "database is locked", and its half-built+ # rows get flushed to disk by the next section that commits.+ try:+ db.session.rollback()+ except Exception as rollback_error:+ app.logger.warning(+ "Could not roll back the session after section '%s': %s", name, rollback_error+ )def _run_migrations(app, engine):diff --git a/tests/test_upgrade_path.py b/tests/test_upgrade_path.pyindex e0538b7b..e1a55b05 100644--- a/tests/test_upgrade_path.py+++ b/tests/test_upgrade_path.py@@ -276,6 +276,49 @@ class TestSectionIsolation:assert "recording, tag, speaker and processing columns" in app.config["MIGRATION_FAILED"]assert "poisoned migration" in app.config["MIGRATION_FAILED"]+ def test_a_failure_mid_session_does_not_poison_later_sections(self, old_db, monkeypatch):+ """A section failing with pending ORM writes must not damage the rest.++ Its open transaction holds SQLite's write lock, so later sections fail+ with "database is locked", and its half-built rows are flushed to disk+ by the next section that commits. The section handler rolls the session+ back to prevent both.+ """+ import src.init_db as init_db+ from src.database import db+ from src.models import SystemSetting++ real_index = init_db.create_index_if_not_exists+ fired = []++ def poisoned(*args, **kwargs):+ if not fired:+ fired.append(True)+ db.session.add(SystemSetting(key="poison-row", value="v"))+ db.session.flush()+ raise RuntimeError("poisoned mid-session")+ return real_index(*args, **kwargs)++ monkeypatch.setattr(init_db, "create_index_if_not_exists", poisoned)++ app = _fixture_app(old_db)+ with app.app_context():+ initialize_database(app)++ con = sqlite3.connect(old_db)+ leaked = con.execute(+ "SELECT COUNT(*) FROM system_setting WHERE key = 'poison-row'"+ ).fetchone()[0]+ recording_cols = [r[1] for r in con.execute('PRAGMA table_info("recording")')]+ con.close()++ assert leaked == 0, "the failed section's pending row was committed by a later section"+ # The section after the failure completed rather than dying on the lock.+ assert "file_hash" in recording_cols+ assert app.config["MIGRATION_FAILED"].count(";") == 0, (+ f"only one section should have failed: {app.config['MIGRATION_FAILED']}"+ )+def test_next_clean_startup_heals_the_skipped_section(self, old_db, monkeypatch):import src.init_db as init_db