database/sql: Fix connection leak and potential deadlock

CL 10726044 introduced a race condition which causes connections
to be leaked under certain circumstances. If SetMaxOpenConns is
used, the application eventually deadlocks. Otherwise, the number
of open connections just keep growing indefinitely.

Fixes #6593

R=golang-dev, bradfitz, tad.glines, bketelsen
CC=golang-dev
https://golang.org/cl/14611045
This commit is contained in:
Alberto García Hierro 2013-10-16 09:22:57 -07:00 committed by Brad Fitzpatrick
parent 478f4b6754
commit 37db880469
3 changed files with 63 additions and 3 deletions

View file

@ -593,9 +593,12 @@ func (db *DB) openNewConnection() {
db: db,
ci: ci,
}
db.addDepLocked(dc, dc)
db.numOpen++
db.putConnDBLocked(dc, err)
if db.putConnDBLocked(dc, err) {
db.addDepLocked(dc, dc)
db.numOpen++
} else {
ci.Close()
}
}
// connRequest represents one request for a new connection