stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.57k stars 1.54k forks source link

If transaction rollback throws, it obscures the true cause #1203

Open tylerbrandt opened 1 year ago

tylerbrandt commented 1 year ago

Issues are used to track bugs and feature requests. Need help or have a general question? Ask on Stack Overflow (tag sqlite.swift).

Build Information

0.14.1

Xcode 14.2 / macOS 13.1

Swift Package manager (actually a fork based on 0.14.1, but it is present in latest code as well)

General guidelines

We recently ran into an issue where a small fraction of our users were logging an error "cannot rollback - no transaction is active (code: 1)" for a particular transaction, but we couldn't figure out why until we got access to one of the problematic DBs and could run it in debug mode with breakpoints. The line throwing (confirmed via breakpoint) was: https://github.com/stephencelis/SQLite.swift/blob/d7fe4499710014aa754d24ea87bedbad4bdd59d5/Sources/SQLite/Core/Connection.swift#L389

It turned out that the issue was that the "database disk image is malformed" (not sure how this happened), an error which only surfaced on attempting to commit the transaction (at least in our case). However, since it was causing try self.run(rollback) itself to throw, SQLite.swift was throwing out the "cannot rollback" error, and it took us a long time to track down the root cause.

sqlite-swift-transaction-rollback

Ideally it would always report the caught error, even if the rollback fails, in a way that client code can read.

matk04 commented 6 months ago

We faced a similar issue on our project where there was an ioerror when writing to the database and it was buried under the cannot rollback - no transaction is active error. It would be nice if it always reported the original error.