You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ruflet_record: return, break or throw out of a transaction block leaves the SQLite transaction open (later writes lost, database stays locked) #93
A return, break or throw out of a RufletRecord transaction block leaves the SQLite transaction open. Later writes in the same process are never committed, and other connections get database is locked until the process exits. When the connection closes, SQLite rolls all of that work back, with no error.
Minimal repro
From packages/ruflet_record, with the test helper's User model:
require_relative"test/test_helper"require"sqlite3"classTxnExitTest < RufletRecordTestCasedefrename(user,name)User.transactiondouser.update!(name: name)return:renamedifuser.name == name:unchangedendenddeftest_returnuser=User.create!(name: "Ada")rename(user,"Grace")User.create!(name: "Linus")# a later, unrelated writeother=SQLite3::Database.new(@database_path)other.busy_timeout=100pRufletRecord.connection.instance_variable_get(:@transaction_depth)pother.execute("SELECT name FROM users ORDER BY id").flattenother.execute("INSERT INTO users (name) VALUES ('Other')")endend
bundle exec ruby -Itest txn_exit_test.rb prints:
1
["Ada"]
SQLite3::BusyException: database is locked
break (for example [1].each { User.transaction { ...; break } }) and throw (inside catch) do the same. A block that returns normally, or uses next, works correctly.
The embedded mruby VM behaves the same way: the depth is 1 after a return and 2 after a following break, and a raw BEGIN then fails with cannot start a transaction within a transaction.
Expected
The transaction ends when the block exits, however it exits. The README describes the API as shaped like the useful core of Active Record, and Active Record (7.2 and later) commits on return, break and throw. So Grace and Linus should both be committed, the depth should be 0, and other connections should be able to write.
Actual
BEGIN IMMEDIATE stays open and @transaction_depth stays at 1. Every later write goes into that open transaction, and later transaction calls only open savepoints inside it. Other connections get SQLITE_BUSY until the process exits, and then everything written after the BEGIN is rolled back.
Cause
packages/ruflet_record/lib/ruflet_record/adapters/sqlite_adapter.rb, SQLiteAdapter#transaction (lines 52-77 on main, unchanged since v0.0.23). COMMIT or RELEASE runs only after result = yield returns normally. ROLLBACK runs only in rescue Exception. A non-local exit is neither of these, so neither branch runs.
Related
If BEGIN IMMEDIATE itself fails, for example with SQLITE_BUSY because another connection holds the write lock, the rescue still issues ROLLBACK. The real error is then replaced by cannot rollback - no transaction is active. In a nested call, a failed SAVEPOINT rolls back the caller's outer transaction instead.
A related problem in the same method, found while fixing this. The rescue Exception branch re-raises with a bare raise. In mruby, a bare raise raises an empty RuntimeError instead of the current error (see mruby doc/limitations.md, "Kernel.raise in rescue clause"). So on the embedded VM, any error raised inside a transaction block comes out as RuntimeError: "".
To reproduce, use the host_vm mruby with the preloaded RufletRecord:
This prints RuntimeError: "". On CRuby it raises RufletRecord::RecordInvalid: Validation failed: Title can't be blank. #99 fixes both by re-raising with raise error.
Activity
AdamMusa commentedon Nov 20, 2026
A related problem in the same method, found while fixing this. The
rescue Exceptionbranch re-raises with a bareraise. In mruby, a bareraiseraises an emptyRuntimeErrorinstead of the current error (see mrubydoc/limitations.md, "Kernel.raise in rescue clause"). So on the embedded VM, any error raised inside a transaction block comes out asRuntimeError: "".To reproduce, use the
host_vmmruby with the preloaded RufletRecord:This prints
RuntimeError: "". On CRuby it raisesRufletRecord::RecordInvalid: Validation failed: Title can't be blank. #99 fixes both by re-raising withraise error.fix(ruflet_record): roll back on thread kill only when the block was …