Repository navigation
ruflet_record: end the transaction on return, break and throw - #99
Merged
Merged
Conversation
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
This was referenced Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #93
Problem
SQLiteAdapter#transactionranCOMMITonly when the block returned normally, andROLLBACKonly inrescue Exception. Areturn,breakorthrowout of the block does neither. SoBEGIN IMMEDIATEstayed open and@transaction_depthstayed at 1. Every later write went into that transaction, and latertransactioncalls only opened savepoints inside it. Other connections gotSQLITE_BUSYuntil 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:ensure. Raising still rolls back, as before. Every other way out commits (or releases the savepoint): returning normally,next, and now alsoreturn,breakandthrow. Active Record 7.2 does the same.Thread#killrunsensurebut notrescue. 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'sensure. mruby has no native threads, and the VM's cooperativeThreadshim never reports"aborting".BEGINorSAVEPOINTraises its own error. Nothing was opened, so nothing is rolled back.raise error, not a bareraise. In mruby, a bareraiseraises an emptyRuntimeErrorinstead of the current error (mrubydoc/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, samecause.COMMIT/RELEASEandROLLBACKmoved into two private helpers,commit_transactionandrollback_transaction. A failedCOMMITstill rolls back and raises theCOMMITerror, as before.The only code used is
begin/rescue/ensure, local variables andObject.const_defined?. There is nodefined?, 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:
return,breakorthrowout of atransactionblock 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+.transactionblock 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.BEGIN IMMEDIATEnow raises its own error. For example:StatementInvalid: database is locked. Before, it raisedStatementInvalid: 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 rawBEGINopen 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.SAVEPOINTraises 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 nestedRELEASE SAVEPOINTno longer decrements the depth twice, and raises theRELEASEerror instead ofcannot rollback - no transaction is active; the end state is the same (nothing committed, lock free).transactionblock keeps its class and message. Before, it came out asRuntimeErrorwith an empty message. For example,rescue RufletRecord::RecordInvalidaround a transaction never matched on device. CRuby is unchanged.How verified
packages/ruflet_record/test/transaction_exit_test.rbhas 13 tests:return,breakandthrowout of the block; nestedreturn;returnthrough two levels;raiseafter an earlierreturn;nextand 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'sensurestill commits; failedBEGIN; failedSAVEPOINT; failedCOMMIT. "Closed" is checked from a second SQLite connection, which must be able to take the write lock and see the committed rows.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.ruflet_recordsuite. Each test file in its own process, plusrake test: 37 runs, 126 assertions, 0 failures (CRuby 4.0.5, sqlite3 2.9.5).git merge-treeagainst 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.host_vm3.4, with the patched adapter source loaded over the preloaded copy):return,break,throw,next,raise, nestedreturnand nestedraise, the depth is 0 and a rawBEGIN IMMEDIATEworks. After reopening the database, every committed row is there.BEGINfails with "cannot start a transaction within a transaction", and after reopening the database it is empty.RufletRecord::RecordInvalid: Validation failed: Title can't be blankwith the fix.RuntimeError: ""with the original adapter, and also with the copy preloaded in the current VM.test/mruby_smoke.rbgains transaction checks:return,breakandthrowcommit, 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.cd packages/ruflet && ruby test/compat/compat.rb --jobs 4checked 234 goldens and exited 0 on 083bb6b.mruby_smoke.rbandsql.rb) at 6390722: everyruflet_recordtest file in its own process passes on CRuby 4.0.5 (and on 3.4.5 at 6c45660;ruflet_recordis unchanged since);rake test: 47 runs, 169 assertions, 0 failures;compat.rb --jobs 4checked 292 goldens, exit 0;compat_goldens_test.rb300 runs, 0 failures;host_vmmruby running mergedsql.rb+ adapter +mruby_smoke.rbprintssmoke: ok.