Module: Lich::SQLiteBusyWait::Transactions

Defined in:
documented/common/sqlite_connection.rb

Overview

Extended onto the Sequel::Database that Lich.open_sequel_sqlite returns.

With interrupts deferred, a Thread#kill that arrives while BEGIN or COMMIT waits for the lock ends the wait, and is delivered the moment that statement returns, in place of its BusyException. Thread#kill skips Sequel's rescue clauses and runs only its ensure, so Sequel's own recovery for a failed BEGIN or COMMIT would not run:

  • a COMMIT that gave up would leave the transaction open on the pooled connection, holding the write lock until the process exits;
  • after a BEGIN that gave up, Sequel's ensure rolls back a transaction that never started. SQLite raises "cannot rollback - no transaction is active", and an exception raised in an ensure replaces the kill: the thread keeps running, and its later transactions are rolled back.

Two interrupts can be queued before the waiting thread runs again. The first is delivered when the statement returns and Sequel rolls back; the second is delivered as the next interrupt mask ends, which without protection is the one around ROLLBACK's prepare, so ROLLBACK never runs and the write transaction stays open. ROLLBACK therefore runs with interrupts deferred too, and a lock wait inside it does not give up because those interrupts are pending.