1
0
mirror of https://github.com/golang/go synced 2024-11-20 01:34:41 -07:00

database/sql: do not leak the connectionResetter goroutine

Before terminating the connectionResetter goroutine the connection
pool processes all of the connections on the channel to unlock the
driverConn instances so everthing can shutdown cleanly. However
the channel was never closed so the goroutine hangs on the range.
Close the channel prior to ranging over it. Also prevent additional
connections from being sent to the resetter after the connection
pool has been closed.

Fixes #22699

Change-Id: I440d2b13cbedec2e04621557f5bd0b1526933dd7
Reviewed-on: https://go-review.googlesource.com/77390
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Daniel Theophanes 2017-11-13 14:25:19 -08:00 committed by Brad Fitzpatrick
parent 4aea3e7135
commit f7df55d174

View File

@ -939,6 +939,7 @@ func (db *DB) connectionResetter(ctx context.Context) {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
close(db.resetterCh)
for dc := range db.resetterCh { for dc := range db.resetterCh {
dc.Unlock() dc.Unlock()
} }
@ -1171,6 +1172,11 @@ func (db *DB) putConn(dc *driverConn, err error, resetSession bool) {
if putConnHook != nil { if putConnHook != nil {
putConnHook(db, dc) putConnHook(db, dc)
} }
if db.closed {
// Connections do not need to be reset if they will be closed.
// Prevents writing to resetterCh after the DB has closed.
resetSession = false
}
if resetSession { if resetSession {
if _, resetSession = dc.ci.(driver.ResetSessioner); resetSession { if _, resetSession = dc.ci.(driver.ResetSessioner); resetSession {
// Lock the driverConn here so it isn't released until // Lock the driverConn here so it isn't released until