Skip to content

ruflet_record: return, break or throw out of a transaction block leaves the SQLite transaction open (later writes lost, database stays locked) #93

Closed

Description

@AdamMusa

Summary

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"

class TxnExitTest < RufletRecordTestCase
  def rename(user, name)
    User.transaction do
      user.update!(name: name)
      return :renamed if user.name == name
      :unchanged
    end
  end

  def test_return
    user = User.create!(name: "Ada")
    rename(user, "Grace")
    User.create!(name: "Linus") # a later, unrelated write

    other = SQLite3::Database.new(@database_path)
    other.busy_timeout = 100
    p RufletRecord.connection.instance_variable_get(:@transaction_depth)
    p other.execute("SELECT name FROM users ORDER BY id").flatten
    other.execute("INSERT INTO users (name) VALUES ('Other')")
  end
end

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.

Activity

  1. AdamMusa commented on Nov 20, 2026

    @AdamMusa
    OwnerAuthor

    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:

    require "ruflet_record"
    RufletRecord.establish_connection(database: ":memory:")
    RufletRecord::Schema.define { create_table(:notes) { |t| t.string :title, null: false } }
    class Note < RufletRecord::Base; validates_presence_of :title; end
    begin
      Note.transaction { Note.create!(title: nil) }
    rescue Exception => e
      puts "#{e.class}: #{e.message.inspect}"   # => RuntimeError: ""
    end

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions