mirror of https://github.com/jackc/pgx.git
Ensure pgxpool.Pool.QueryRow.Scan releases connection on panic
Otherwise a connection would be leaked and closing the pool would block. https://github.com/jackc/pgx/issues/1628pull/1631/head
parent
229d2aaa49
commit
608f39f426
|
@ -698,6 +698,25 @@ func TestPoolQueryRowErrNoRows(t *testing.T) {
|
||||||
require.Equal(t, pgx.ErrNoRows, err)
|
require.Equal(t, pgx.ErrNoRows, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/jackc/pgx/issues/1628
|
||||||
|
func TestPoolQueryRowScanPanicReleasesConnection(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
pool, err := pgxpool.New(ctx, os.Getenv("PGX_TEST_DATABASE"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer pool.Close()
|
||||||
|
|
||||||
|
require.Panics(t, func() {
|
||||||
|
var greeting *string
|
||||||
|
pool.QueryRow(ctx, "select 'Hello, world!'").Scan(greeting) // Note lack of &. This means that a typed nil is passed to Scan.
|
||||||
|
})
|
||||||
|
|
||||||
|
// If the connection is not released this will block forever in the defer pool.Close().
|
||||||
|
}
|
||||||
|
|
||||||
func TestPoolSendBatch(t *testing.T) {
|
func TestPoolSendBatch(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,14 @@ func (row *poolRow) Scan(dest ...any) error {
|
||||||
return row.err
|
return row.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
panicked := true
|
||||||
|
defer func() {
|
||||||
|
if panicked && row.c != nil {
|
||||||
|
row.c.Release()
|
||||||
|
}
|
||||||
|
}()
|
||||||
err := row.r.Scan(dest...)
|
err := row.r.Scan(dest...)
|
||||||
|
panicked = false
|
||||||
if row.c != nil {
|
if row.c != nil {
|
||||||
row.c.Release()
|
row.c.Release()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue