Skip to content

Commit fd17253

Browse files
kardianosrsc
authored andcommitted
[release-branch.go1.9] database/sql: prevent race in driver by locking dc in Next
Database drivers should be called from a single goroutine to ease driver's design. If a driver chooses to handle context cancels internally it may do so. The sql package violated this agreement when calling Next or NextResultSet. It was possible for a concurrent rollback triggered from a context cancel to call a Tx.Rollback (which takes a driver connection lock) while a Rows.Next is in progress (which does not tack the driver connection lock). The current internal design of the sql package is each call takes roughly two locks: a closemu lock which prevents an disposing of internal resources (assigning nil or removing from lists) and a driver connection lock that prevents calling driver code from multiple goroutines. Fixes #21117 Change-Id: Ie340dc752a503089c27f57ffd43e191534829360 Reviewed-on: https://go-review.googlesource.com/65731 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-on: https://go-review.googlesource.com/71510 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Daniel Theophanes <[email protected]>
1 parent 7e7cb30 commit fd17253

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/database/sql/fakedb_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ type rowsCursor struct {
943943
}
944944

945945
func (rc *rowsCursor) touchMem() {
946+
rc.parentMem.touchMem()
946947
rc.line++
947948
}
948949

src/database/sql/sql.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,6 +2454,12 @@ func (rs *Rows) nextLocked() (doClose, ok bool) {
24542454
if rs.lastcols == nil {
24552455
rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
24562456
}
2457+
2458+
// Lock the driver connection before calling the driver interface
2459+
// rowsi to prevent a Tx from rolling back the connection at the same time.
2460+
rs.dc.Lock()
2461+
defer rs.dc.Unlock()
2462+
24572463
rs.lasterr = rs.rowsi.Next(rs.lastcols)
24582464
if rs.lasterr != nil {
24592465
// Close the connection if there is a driver error.
@@ -2503,6 +2509,12 @@ func (rs *Rows) NextResultSet() bool {
25032509
doClose = true
25042510
return false
25052511
}
2512+
2513+
// Lock the driver connection before calling the driver interface
2514+
// rowsi to prevent a Tx from rolling back the connection at the same time.
2515+
rs.dc.Lock()
2516+
defer rs.dc.Unlock()
2517+
25062518
rs.lasterr = nextResultSet.NextResultSet()
25072519
if rs.lasterr != nil {
25082520
doClose = true

src/database/sql/sql_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3106,6 +3106,9 @@ func TestIssue6081(t *testing.T) {
31063106
// In the test, a context is canceled while the query is in process so
31073107
// the internal rollback will run concurrently with the explicitly called
31083108
// Tx.Rollback.
3109+
//
3110+
// The addition of calling rows.Next also tests
3111+
// Issue 21117.
31093112
func TestIssue18429(t *testing.T) {
31103113
db := newTestDB(t, "people")
31113114
defer closeDB(t, db)
@@ -3116,7 +3119,7 @@ func TestIssue18429(t *testing.T) {
31163119

31173120
const milliWait = 30
31183121

3119-
for i := 0; i < 100; i++ {
3122+
for i := 0; i < 1000; i++ {
31203123
sem <- true
31213124
wg.Add(1)
31223125
go func() {
@@ -3138,6 +3141,9 @@ func TestIssue18429(t *testing.T) {
31383141
// reported.
31393142
rows, _ := tx.QueryContext(ctx, "WAIT|"+qwait+"|SELECT|people|name|")
31403143
if rows != nil {
3144+
// Call Next to test Issue 21117 and check for races.
3145+
for rows.Next() {
3146+
}
31413147
rows.Close()
31423148
}
31433149
// This call will race with the context cancel rollback to complete

0 commit comments

Comments
 (0)