Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
) * Fix "cannot start a transaction within a transaction" issue [why] If db.BeginTx(ctx, nil) context is cancelled too fast, "BEGIN" statement can be completed inside DB, but we still try to cancel it with sqlite3_interrupt. In such case we get context.Cancelled or context.DeadlineExceeded from exec(), but operation really completed. Connection returned into pool, and returns "cannot start a transaction within a transaction" error for next db.BeginTx() call. [how] Handle status code returned from cancelled operation. [testing] Added unit-test which reproduces issue. * Reduce TestQueryRowContextCancelParallel concurrency [why] Tests times out in travis-ci when run with -race option.
- Loading branch information
1 parent
6a8b301
commit 862b959
Showing
3 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build go1.13,cgo | ||
|
||
package sqlite3 | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"database/sql/driver" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestBeginTxCancel(t *testing.T) { | ||
srcTempFilename := TempFilename(t) | ||
defer os.Remove(srcTempFilename) | ||
|
||
db, err := sql.Open("sqlite3", srcTempFilename) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
db.SetMaxOpenConns(10) | ||
db.SetMaxIdleConns(5) | ||
|
||
defer db.Close() | ||
initDatabase(t, db, 100) | ||
|
||
// create several go-routines to expose racy issue | ||
for i := 0; i < 1000; i++ { | ||
func() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
conn, err := db.Conn(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := conn.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
err = conn.Raw(func(driverConn interface{}) error { | ||
d, ok := driverConn.(driver.ConnBeginTx) | ||
if !ok { | ||
t.Fatal("unexpected: wrong type") | ||
} | ||
|
||
go cancel() // make it cancel concurrently with exec("BEGIN"); | ||
tx, err := d.BeginTx(ctx, driver.TxOptions{}) | ||
switch err { | ||
case nil: | ||
switch err := tx.Rollback(); err { | ||
case nil, sql.ErrTxDone: | ||
default: | ||
return err | ||
} | ||
case context.Canceled: | ||
default: | ||
// must not fail with "cannot start a transaction within a transaction" | ||
return err | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters