diff --git a/scripts/agentctl.py b/scripts/agentctl.py index 956888d2..c3b2b6ff 100755 --- a/scripts/agentctl.py +++ b/scripts/agentctl.py @@ -588,13 +588,18 @@ class Repo: db.execute('PRAGMA busy_timeout=5000') if write: db.execute('PRAGMA synchronous=FULL') + # DDL inside ensure_schema implicitly commits any open txn; take the + # exclusive lock only after schema is stable so claim/lease CAS holds. db.execute('BEGIN IMMEDIATE') ensure_schema(db) + if db.in_transaction: + db.execute('COMMIT') + db.execute('BEGIN IMMEDIATE') yield db - if write: + if write and db.in_transaction: db.commit() except BaseException: - if write: + if write and db.in_transaction: db.rollback() raise finally: