Skip to content

Commit

Permalink
Merge pull request #797 from gobuffalo/prevent-conn-leak-but-do-not-s…
Browse files Browse the repository at this point in the history
…wallow-panic

do not swallow panic when rolling back inner panic to prevent connection leak
  • Loading branch information
sio4 committed Nov 19, 2022
2 parents 479087d + ef29b26 commit 17f09c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
4 changes: 2 additions & 2 deletions connection.go
Expand Up @@ -168,9 +168,9 @@ func (c *Connection) Transaction(fn func(tx *Connection) error) error {
txlog(logging.SQL, cn, "ROLLBACK Transaction (inner function panic) ---")
dberr = cn.TX.Rollback()
if dberr != nil {
err = fmt.Errorf("database error while inner panic rollback: %w", dberr)
txlog(logging.Error, cn, "database error while inner panic rollback: %w", dberr)
}
err = fmt.Errorf("transaction was rolled back due to inner panic")
panic(ex)
}
}()

Expand Down
8 changes: 4 additions & 4 deletions connection_test.go
Expand Up @@ -138,10 +138,10 @@ func Test_Connection_Transaction(t *testing.T) {
})

t.Run("Panic", func(t *testing.T) {
err = c.Transaction(func(c *Connection) error {
panic("inner function panic")
r.PanicsWithValue("inner function panic", func() {
c.Transaction(func(c *Connection) error {
panic("inner function panic")
})
})
r.ErrorContains(err, "panic")
r.ErrorContains(err, "rolled back")
})
}
41 changes: 23 additions & 18 deletions logger.go
Expand Up @@ -50,30 +50,35 @@ var txlog = func(lvl logging.Level, anon interface{}, s string, args ...interfac
} else {
s = fmt.Sprintf("%s - %s", lvl, s)
}

connID := ""
txID := 0
switch typed := anon.(type) {
case *Connection:
connID = typed.ID
if typed.TX != nil {
txID = typed.TX.ID
}
case *Tx:
txID = typed.ID
case store:
tx, err := typed.Transaction()
if err == nil {
txID = tx.ID
}
}
s = fmt.Sprintf("%s (conn=%v, tx=%v)", s, connID, txID)
} else {
s = fmt.Sprintf(s, args...)
s = fmt.Sprintf("%s - %s", lvl, s)
}

connID := ""
txID := 0
switch typed := anon.(type) {
case *Connection:
connID = typed.ID
if typed.TX != nil {
txID = typed.TX.ID
}
case *Tx:
txID = typed.ID
case store:
tx, err := typed.Transaction()
if err == nil {
txID = tx.ID
}
}

if connID != "" || txID != 0 {
s = fmt.Sprintf("%s (conn=%v, tx=%v)", s, connID, txID)
}

if Color {
s = color.YellowString(s)
}

defaultStdLogger.Println(s)
}

0 comments on commit 17f09c0

Please sign in to comment.