database/sql: prevent race on Rows close with Tx Rollback

In addition to adding a guard to the Rows close, add a var
in the fakeConn that gets read and written to on each
operation, simulating writing or reading from the server.

TestConcurrency/TxStmt* tests have been commented out
as they now fail after checking for races on the fakeConn.
See issue #20646 for more information.

Fixes #20622

Change-Id: I80b36ea33d776e5b4968be1683ff8c61728ee1ea
Reviewed-on: https://go-review.googlesource.com/45275
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-06-10 22:02:53 -07:00
parent 3820191839
commit b0d592c3c9
3 changed files with 85 additions and 12 deletions

View file

@ -2471,6 +2471,8 @@ func TestManyErrBadConn(t *testing.T) {
// closing a transaction. Ensure Rows is closed while closing a trasaction.
func TestIssue20575(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
@ -2493,6 +2495,43 @@ func TestIssue20575(t *testing.T) {
}
}
// TestIssue20622 tests closing the transaction before rows is closed, requires
// the race detector to fail.
func TestIssue20622(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tx, err := db.BeginTx(ctx, nil)
if err != nil {
t.Fatal(err)
}
rows, err := tx.Query("SELECT|people|age,name|")
if err != nil {
t.Fatal(err)
}
count := 0
for rows.Next() {
count++
var age int
var name string
if err := rows.Scan(&age, &name); err != nil {
t.Fatal("scan failed", err)
}
if count == 1 {
cancel()
}
time.Sleep(100 * time.Millisecond)
}
rows.Close()
tx.Commit()
}
// golang.org/issue/5718
func TestErrBadConnReconnect(t *testing.T) {
db := newTestDB(t, "foo")
@ -2956,8 +2995,9 @@ func (c *concurrentRandomTest) init(t testing.TB, db *DB) {
new(concurrentStmtExecTest),
new(concurrentTxQueryTest),
new(concurrentTxExecTest),
new(concurrentTxStmtQueryTest),
new(concurrentTxStmtExecTest),
// golang.org/issue/20646
// new(concurrentTxStmtQueryTest),
// new(concurrentTxStmtExecTest),
}
for _, ct := range c.tests {
ct.init(t, db)
@ -3193,15 +3233,26 @@ func TestIssue18719(t *testing.T) {
}
func TestConcurrency(t *testing.T) {
doConcurrentTest(t, new(concurrentDBQueryTest))
doConcurrentTest(t, new(concurrentDBExecTest))
doConcurrentTest(t, new(concurrentStmtQueryTest))
doConcurrentTest(t, new(concurrentStmtExecTest))
doConcurrentTest(t, new(concurrentTxQueryTest))
doConcurrentTest(t, new(concurrentTxExecTest))
doConcurrentTest(t, new(concurrentTxStmtQueryTest))
doConcurrentTest(t, new(concurrentTxStmtExecTest))
doConcurrentTest(t, new(concurrentRandomTest))
list := []struct {
name string
ct concurrentTest
}{
{"Query", new(concurrentDBQueryTest)},
{"Exec", new(concurrentDBExecTest)},
{"StmtQuery", new(concurrentStmtQueryTest)},
{"StmtExec", new(concurrentStmtExecTest)},
{"TxQuery", new(concurrentTxQueryTest)},
{"TxExec", new(concurrentTxExecTest)},
// golang.org/issue/20646
// {"TxStmtQuery", new(concurrentTxStmtQueryTest)},
// {"TxStmtExec", new(concurrentTxStmtExecTest)},
{"Random", new(concurrentRandomTest)},
}
for _, item := range list {
t.Run(item.name, func(t *testing.T) {
doConcurrentTest(t, item.ct)
})
}
}
func TestConnectionLeak(t *testing.T) {
@ -3531,6 +3582,7 @@ func BenchmarkConcurrentTxExec(b *testing.B) {
}
func BenchmarkConcurrentTxStmtQuery(b *testing.B) {
b.Skip("golang.org/issue/20646")
b.ReportAllocs()
ct := new(concurrentTxStmtQueryTest)
for i := 0; i < b.N; i++ {
@ -3539,6 +3591,7 @@ func BenchmarkConcurrentTxStmtQuery(b *testing.B) {
}
func BenchmarkConcurrentTxStmtExec(b *testing.B) {
b.Skip("golang.org/issue/20646")
b.ReportAllocs()
ct := new(concurrentTxStmtExecTest)
for i := 0; i < b.N; i++ {