mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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 <iant@golang.org>
This commit is contained in:
parent
350b74bc4b
commit
897080d5cb
3 changed files with 20 additions and 1 deletions
|
|
@ -3127,6 +3127,9 @@ func TestIssue6081(t *testing.T) {
|
|||
// In the test, a context is canceled while the query is in process so
|
||||
// the internal rollback will run concurrently with the explicitly called
|
||||
// Tx.Rollback.
|
||||
//
|
||||
// The addition of calling rows.Next also tests
|
||||
// Issue 21117.
|
||||
func TestIssue18429(t *testing.T) {
|
||||
db := newTestDB(t, "people")
|
||||
defer closeDB(t, db)
|
||||
|
|
@ -3137,7 +3140,7 @@ func TestIssue18429(t *testing.T) {
|
|||
|
||||
const milliWait = 30
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
for i := 0; i < 1000; i++ {
|
||||
sem <- true
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
|
|
@ -3159,6 +3162,9 @@ func TestIssue18429(t *testing.T) {
|
|||
// reported.
|
||||
rows, _ := tx.QueryContext(ctx, "WAIT|"+qwait+"|SELECT|people|name|")
|
||||
if rows != nil {
|
||||
// Call Next to test Issue 21117 and check for races.
|
||||
for rows.Next() {
|
||||
}
|
||||
rows.Close()
|
||||
}
|
||||
// This call will race with the context cancel rollback to complete
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue