Merge pull request #489 from Projectplace/fix-cancel-race

Fix race in ExecContext
This commit is contained in:
mattn 2017-11-22 09:24:37 +09:00 committed by GitHub
commit d5ffb5c0cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -1171,9 +1171,13 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
defer close(done)
go func(db *C.sqlite3) {
select {
case <-ctx.Done():
C.sqlite3_interrupt(db)
case <-done:
case <-ctx.Done():
select {
case <-done:
default:
C.sqlite3_interrupt(db)
}
}
}(s.c.db)

View File

@ -134,3 +134,24 @@ func TestShortTimeout(t *testing.T) {
t.Fatal(ctx.Err())
}
}
func TestExecCancel(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err = db.Exec("create table foo (id integer primary key)"); err != nil {
t.Fatal(err)
}
for n := 0; n < 100; n++ {
ctx, cancel := context.WithCancel(context.Background())
_, err = db.ExecContext(ctx, "insert into foo (id) values (?)", n)
cancel()
if err != nil {
t.Fatal(err)
}
}
}