Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not swallow panic when rolling back inner panic to prevent connection leak #797

Merged
merged 3 commits into from Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
sio4 marked this conversation as resolved.
Show resolved Hide resolved
}
}()

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)
}

sio4 marked this conversation as resolved.
Show resolved Hide resolved
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)
}