Skip to content

Commit 897080d

Browse files
committed
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]>
1 parent 350b74b commit 897080d

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
@@ -2491,6 +2491,12 @@ func (rs *Rows) nextLocked() (doClose, ok bool) {
24912491
if rs.lastcols == nil {
24922492
rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
24932493
}
2494+
2495+
// Lock the driver connection before calling the driver interface
2496+
// rowsi to prevent a Tx from rolling back the connection at the same time.
2497+
rs.dc.Lock()
2498+
defer rs.dc.Unlock()
2499+
24942500
rs.lasterr = rs.rowsi.Next(rs.lastcols)
24952501
if rs.lasterr != nil {
24962502
// Close the connection if there is a driver error.
@@ -2540,6 +2546,12 @@ func (rs *Rows) NextResultSet() bool {
25402546
doClose = true
25412547
return false
25422548
}
2549+
2550+
// Lock the driver connection before calling the driver interface
2551+
// rowsi to prevent a Tx from rolling back the connection at the same time.
2552+
rs.dc.Lock()
2553+
defer rs.dc.Unlock()
2554+
25432555
rs.lasterr = nextResultSet.NextResultSet()
25442556
if rs.lasterr != nil {
25452557
doClose = true

src/database/sql/sql_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3127,6 +3127,9 @@ func TestIssue6081(t *testing.T) {
31273127
// In the test, a context is canceled while the query is in process so
31283128
// the internal rollback will run concurrently with the explicitly called
31293129
// Tx.Rollback.
3130+
//
3131+
// The addition of calling rows.Next also tests
3132+
// Issue 21117.
31303133
func TestIssue18429(t *testing.T) {
31313134
db := newTestDB(t, "people")
31323135
defer closeDB(t, db)
@@ -3137,7 +3140,7 @@ func TestIssue18429(t *testing.T) {
31373140

31383141
const milliWait = 30
31393142

3140-
for i := 0; i < 100; i++ {
3143+
for i := 0; i < 1000; i++ {
31413144
sem <- true
31423145
wg.Add(1)
31433146
go func() {
@@ -3159,6 +3162,9 @@ func TestIssue18429(t *testing.T) {
31593162
// reported.
31603163
rows, _ := tx.QueryContext(ctx, "WAIT|"+qwait+"|SELECT|people|name|")
31613164
if rows != nil {
3165+
// Call Next to test Issue 21117 and check for races.
3166+
for rows.Next() {
3167+
}
31623168
rows.Close()
31633169
}
31643170
// This call will race with the context cancel rollback to complete

0 commit comments

Comments
 (0)