sql: fix missing mutex unlock in an error case

Fixes #2542

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5483054
This commit is contained in:
Brad Fitzpatrick 2011-12-12 13:56:56 -08:00
parent 3dc278d3e2
commit 06a9bc6835
2 changed files with 14 additions and 0 deletions

View File

@ -134,6 +134,7 @@ func (db *DB) maxIdleConns() int {
func (db *DB) conn() (driver.Conn, error) {
db.mu.Lock()
if db.closed {
db.mu.Unlock()
return nil, errors.New("sql: database is closed")
}
if n := len(db.freeConn); n > 0 {

View File

@ -228,3 +228,16 @@ func TestTxStmt(t *testing.T) {
t.Fatalf("Commit = %v", err)
}
}
// Tests fix for issue 2542, that we release a lock when querying on
// a closed connection.
func TestIssue2542Deadlock(t *testing.T) {
db := newTestDB(t, "people")
closeDB(t, db)
for i := 0; i < 2; i++ {
_, err := db.Query("SELECT|people|age,name|")
if err == nil {
t.Fatalf("expected error")
}
}
}