Skip to content

ruflet_record: end the transaction on return, break and throw - #99

Merged
AdamMusa merged 9 commits into
mainfrom
fix/record-transaction-nonlocal-exit
Nov 20, 2026
Merged

AdamMusa merged 9 commits into
mainfrom
fix/record-transaction-nonlocal-exit

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes #93

Problem

SQLiteAdapter#transaction ran COMMIT only when the block returned normally, and ROLLBACK only in rescue Exception. A return, break or throw out of the block does neither. So BEGIN IMMEDIATE stayed open and @transaction_depth stayed at 1. Every later write went into that transaction, and later transaction calls only opened savepoints inside it. Other connections got SQLITE_BUSY until the process exited, and closing the connection rolled everything back, with no error.

What changed

packages/ruflet_record/lib/ruflet_record/adapters/sqlite_adapter.rb:

  • The block now finishes in ensure. Raising still rolls back, as before. Every other way out commits (or releases the savepoint): returning normally, next, and now also return, break and throw. Active Record 7.2 does the same.
  • A killed thread rolls back. Thread#kill runs ensure but not rescue. When the kill cuts a block short (Thread.current.status == "aborting"), the transaction rolls back instead of committing half-done work. This follows Active Record. A block that runs to the end still commits, even inside a dying thread's ensure. mruby has no native threads, and the VM's cooperative Thread shim never reports "aborting".
  • A failed BEGIN or SAVEPOINT raises its own error. Nothing was opened, so nothing is rolled back.
  • Errors are re-raised with raise error, not a bare raise. In mruby, a bare raise raises an empty RuntimeError instead of the current error (mruby doc/limitations.md). This keeps the class and message of an error raised in the block on the embedded VM. In CRuby the two forms behave the same: same object, same backtrace, same cause.
  • COMMIT/RELEASE and ROLLBACK moved into two private helpers, commit_transaction and rollback_transaction. A failed COMMIT still rolls back and raises the COMMIT error, as before.

The only code used is begin/rescue/ensure, local variables and Object.const_defined?. There is no defined?, so it runs unchanged in the embedded mruby VM.

No app code changes. Blocks that complete normally and blocks that raise behave as before on CRuby.

Behavior changes

These are all bug fixes. Each one changes behavior only in a case that was broken before:

  1. return, break or throw out of a transaction block now commits. Before, the transaction stayed open for the rest of the process: later writes were lost when the connection closed, and the database stayed write-locked. This matches Active Record 7.2+.
  2. A CRuby thread killed while inside a transaction block now rolls back. Before, the transaction was left open, the same as item 1. A block that runs to the end commits as before, even while its thread is being killed.
  3. A failed BEGIN IMMEDIATE now raises its own error. For example: StatementInvalid: database is locked. Before, it raised StatementInvalid: cannot rollback - no transaction is active. The class is unchanged; only the message is now the real one. The same applies when the caller already has its own raw BEGIN open on the connection: the message is unchanged, but that raw transaction is no longer rolled back by the failed call and stays open for the caller.
  4. A failed nested SAVEPOINT raises its own error and leaves the caller's transaction open. Before, it rolled back the whole outer transaction while the outer block kept running. A failed nested RELEASE SAVEPOINT no longer decrements the depth twice, and raises the RELEASE error instead of cannot rollback - no transaction is active; the end state is the same (nothing committed, lock free).
  5. Embedded mruby VM only: an error raised inside a transaction block keeps its class and message. Before, it came out as RuntimeError with an empty message. For example, rescue RufletRecord::RecordInvalid around a transaction never matched on device. CRuby is unchanged.

How verified

  • New regression tests. packages/ruflet_record/test/transaction_exit_test.rb has 13 tests: return, break and throw out of the block; nested return; return through two levels; raise after an earlier return; next and normal completion keep the block value; an error propagates as the same object; a killed thread rolls back; a block that completes in a dying thread's ensure still commits; failed BEGIN; failed SAVEPOINT; failed COMMIT. "Closed" is checked from a second SQLite connection, which must be able to take the write lock and see the committed rows.
    • With the original adapter restored: 7 failures and 2 errors.
    • With the fix: 13 runs, 44 assertions, 0 failures. Seeds 1, 7, 42, 999 and 31337 all pass.
    • The 4 tests that pass on both versions guard behavior that must not change: normal completion, error propagation, failed COMMIT, and a completed block in a dying thread. The last one also fails (1 failure) on a draft that rolled back whenever the thread was aborting.
  • Full ruflet_record suite. Each test file in its own process, plus rake test: 37 runs, 126 assertions, 0 failures (CRuby 4.0.5, sqlite3 2.9.5).
  • Merged with open PR ruflet_record: fix six bugs the coverage tests exposed; test the untested public API #90. That PR edits the same file. git merge-tree against ruflet_record: fix six bugs the coverage tests exposed; test the untested public API #90 and Release readiness: fix stale VM version checks and lockfiles #83 merges cleanly. Every test file on the ruflet_record: fix six bugs the coverage tests exposed; test the untested public API #90 merge result passes, including ruflet_record: fix six bugs the coverage tests exposed; test the untested public API #90's new files.
  • Embedded mruby VM (host_vm 3.4, with the patched adapter source loaded over the preloaded copy):
    • Fixed adapter: after return, break, throw, next, raise, nested return and nested raise, the depth is 0 and a raw BEGIN IMMEDIATE works. After reopening the database, every committed row is there.
    • Original adapter: the depth climbs 1, 2, 3, ..., 5, a raw BEGIN fails with "cannot start a transaction within a transaction", and after reopening the database it is empty.
    • An error from a transaction block: RufletRecord::RecordInvalid: Validation failed: Title can't be blank with the fix. RuntimeError: "" with the original adapter, and also with the copy preloaded in the current VM.
  • test/mruby_smoke.rb gains transaction checks: return, break and throw commit, and an error keeps its class and rolls back. It passes with the fixed adapter and fails on the original. It runs against the preloaded VM copy, so it passes there only once the VM is rebuilt from this source.
  • H1 goldens: cd packages/ruflet && ruby test/compat/compat.rb --jobs 4 checked 234 goldens and exited 0 on 083bb6b.
  • Merge gate, after merging main (incl. ruflet_record: open-ended ranges in where (18.., ..17, ...18) no longer match nothing #107, which also edits mruby_smoke.rb and sql.rb) at 6390722: every ruflet_record test file in its own process passes on CRuby 4.0.5 (and on 3.4.5 at 6c45660; ruflet_record is unchanged since); rake test: 47 runs, 169 assertions, 0 failures; compat.rb --jobs 4 checked 292 goldens, exit 0; compat_goldens_test.rb 300 runs, 0 failures; host_vm mruby running merged sql.rb + adapter + mruby_smoke.rb prints smoke: ok.
  • Not touched: engine, template, CLI and server.

SQLiteAdapter#transaction ran COMMIT only after the block returned
normally and ROLLBACK only in `rescue Exception`. A `return`, `break` or
`throw` out of the block is neither, so BEGIN IMMEDIATE stayed open and
the depth stayed at 1. Every later write went into that transaction and
was rolled back when the connection closed, and other connections got
SQLITE_BUSY in the meantime.

The block now finishes in `ensure`: raising rolls back as before, and
every other exit commits (Active Record 7.2 semantics). A thread killed
mid-block rolls back instead of committing half-done work.

Also:
- A failed BEGIN or SAVEPOINT now raises its own error. Before, the
  rescue issued ROLLBACK anyway, which replaced "database is locked"
  with "cannot rollback - no transaction is active", and a failed
  nested SAVEPOINT rolled back the caller's outer transaction.
- Re-raise with `raise error`. In mruby a bare `raise` raises an empty
  RuntimeError, so every error out of a transaction block on the
  embedded VM lost its class and message.

Fixes #93
…cut short

A block that runs to the end commits exactly as before, even when it runs
inside the ensure of a thread that is being killed. Only a non-local exit
caused by the kill rolls back.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant