]> Cypherpunks repositories - gostls13.git/commit
database/sql: fix race when canceling queries immediately
authorDaniel Theophanes <kardianos@gmail.com>
Sat, 21 Jan 2017 01:12:50 +0000 (17:12 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 26 Jan 2017 06:25:37 +0000 (06:25 +0000)
commit2b283cedef2a62e37b7422ef3badc7b758bd26c8
treecbcc0b61e8b932e8cbf2010c0d1ff1ffa74f619a
parent1cf08182f92698aeba8be040dafb5296707cdbf2
database/sql: fix race when canceling queries immediately

Previously the following could happen, though in practice it would
be rare.

Goroutine 1:
(*Tx).QueryContext begins a query, passing in userContext

Goroutine 2:
(*Tx).awaitDone starts to wait on the context derived from the passed in context

Goroutine 1:
(*Tx).grabConn returns a valid (*driverConn)
The (*driverConn) passes to (*DB).queryConn

Goroutine 3:
userContext is canceled

Goroutine 2:
(*Tx).awaitDone unblocks and calls (*Tx).rollback
(*driverConn).finalClose obtains dc.Mutex
(*driverConn).finalClose sets dc.ci = nil

Goroutine 1:
(*DB).queryConn obtains dc.Mutex in withLock
ctxDriverPrepare accepts dc.ci which is now nil
ctxCriverPrepare panics on the nil ci

The fix for this is to guard the Tx methods with a RWLock
holding it exclusivly when closing the Tx and holding a read lock
when executing a query.

Fixes #18719

Change-Id: I37aa02c37083c9793dabd28f7f934a1c5cbc05ea
Reviewed-on: https://go-review.googlesource.com/35550
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/database/sql/sql.go
src/database/sql/sql_test.go