mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Prevent panics caused by attempting to close an already closed pgxpool.Pool.
This commit is contained in:
parent
a0028cbd0d
commit
fe366b2cf3
@ -326,8 +326,14 @@ func ParseConfig(connString string) (*Config, error) {
|
||||
// Close closes all connections in the pool and rejects future Acquire calls. Blocks until all connections are returned
|
||||
// to pool and closed.
|
||||
func (p *Pool) Close() {
|
||||
close(p.closeChan)
|
||||
p.p.Close()
|
||||
// Check to see if the closeChan is closed before attempting to close it again, which can result in a panic.
|
||||
select {
|
||||
case <-p.closeChan:
|
||||
// NOOP because the channel is already closed.
|
||||
default:
|
||||
close(p.closeChan)
|
||||
p.p.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Pool) backgroundHealthCheck() {
|
||||
|
@ -789,3 +789,14 @@ func TestTxBeginFuncNestedTransactionRollback(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 2, n)
|
||||
}
|
||||
|
||||
func TestIdempotentPoolClose(t *testing.T) {
|
||||
pool, err := pgxpool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE"))
|
||||
require.NoError(t, err)
|
||||
|
||||
// Close the open pool.
|
||||
require.NotPanics(t, func() { pool.Close() })
|
||||
|
||||
// Close the already closed pool.
|
||||
require.NotPanics(t, func() { pool.Close() })
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user