walletdb: ensure transactions are rolled back on panic

This commit is contained in:
Olaoluwa Osuntokun 2020-01-15 04:52:34 -08:00 committed by Conner Fromknecht
parent e7e46cd6f9
commit da2fe0e3b0
No known key found for this signature in database
GPG key ID: E7D737B67FA592C7

View file

@ -210,11 +210,20 @@ func View(db DB, f func(tx ReadTx) error) error {
if err != nil {
return err
}
// Make sure the transaction rolls back in the event of a panic.
defer func() {
if tx != nil {
tx.Rollback()
}
}()
err = f(tx)
rollbackErr := tx.Rollback()
if err != nil {
return err
}
if rollbackErr != nil {
return rollbackErr
}
@ -232,6 +241,14 @@ func Update(db DB, f func(tx ReadWriteTx) error) error {
if err != nil {
return err
}
// Make sure the transaction rolls back in the event of a panic.
defer func() {
if tx != nil {
tx.Rollback()
}
}()
err = f(tx)
if err != nil {
// Want to return the original error, not a rollback error if
@ -239,6 +256,7 @@ func Update(db DB, f func(tx ReadWriteTx) error) error {
_ = tx.Rollback()
return err
}
return tx.Commit()
}