Skip to content

Commit 17f09c0

Browse files
authored
Merge pull request #797 from gobuffalo/prevent-conn-leak-but-do-not-swallow-panic
do not swallow panic when rolling back inner panic to prevent connection leak
2 parents 479087d + ef29b26 commit 17f09c0

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

connection.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ func (c *Connection) Transaction(fn func(tx *Connection) error) error {
168168
txlog(logging.SQL, cn, "ROLLBACK Transaction (inner function panic) ---")
169169
dberr = cn.TX.Rollback()
170170
if dberr != nil {
171-
err = fmt.Errorf("database error while inner panic rollback: %w", dberr)
171+
txlog(logging.Error, cn, "database error while inner panic rollback: %w", dberr)
172172
}
173-
err = fmt.Errorf("transaction was rolled back due to inner panic")
173+
panic(ex)
174174
}
175175
}()
176176

connection_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ func Test_Connection_Transaction(t *testing.T) {
138138
})
139139

140140
t.Run("Panic", func(t *testing.T) {
141-
err = c.Transaction(func(c *Connection) error {
142-
panic("inner function panic")
141+
r.PanicsWithValue("inner function panic", func() {
142+
c.Transaction(func(c *Connection) error {
143+
panic("inner function panic")
144+
})
143145
})
144-
r.ErrorContains(err, "panic")
145-
r.ErrorContains(err, "rolled back")
146146
})
147147
}

logger.go

+23-18
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,35 @@ var txlog = func(lvl logging.Level, anon interface{}, s string, args ...interfac
5050
} else {
5151
s = fmt.Sprintf("%s - %s", lvl, s)
5252
}
53-
54-
connID := ""
55-
txID := 0
56-
switch typed := anon.(type) {
57-
case *Connection:
58-
connID = typed.ID
59-
if typed.TX != nil {
60-
txID = typed.TX.ID
61-
}
62-
case *Tx:
63-
txID = typed.ID
64-
case store:
65-
tx, err := typed.Transaction()
66-
if err == nil {
67-
txID = tx.ID
68-
}
69-
}
70-
s = fmt.Sprintf("%s (conn=%v, tx=%v)", s, connID, txID)
7153
} else {
7254
s = fmt.Sprintf(s, args...)
7355
s = fmt.Sprintf("%s - %s", lvl, s)
7456
}
57+
58+
connID := ""
59+
txID := 0
60+
switch typed := anon.(type) {
61+
case *Connection:
62+
connID = typed.ID
63+
if typed.TX != nil {
64+
txID = typed.TX.ID
65+
}
66+
case *Tx:
67+
txID = typed.ID
68+
case store:
69+
tx, err := typed.Transaction()
70+
if err == nil {
71+
txID = tx.ID
72+
}
73+
}
74+
75+
if connID != "" || txID != 0 {
76+
s = fmt.Sprintf("%s (conn=%v, tx=%v)", s, connID, txID)
77+
}
78+
7579
if Color {
7680
s = color.YellowString(s)
7781
}
82+
7883
defaultStdLogger.Println(s)
7984
}

0 commit comments

Comments
 (0)