[bugfix] temporarily replace modernc.org/sqlite-v1.29.5 with gitlab.com/NyaaaWhatsUpDoc/sqlite-v1.29.5-concurrency-workaround (#2811)

This commit is contained in:
kim 2024-04-05 10:58:28 +01:00 committed by GitHub
commit 85bc140b58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 31 additions and 95 deletions

102
vendor/modernc.org/sqlite/sqlite.go generated vendored
View file

@ -21,7 +21,6 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
@ -491,17 +490,6 @@ func toNamedValues(vals []driver.Value) (r []driver.NamedValue) {
func (s *stmt) exec(ctx context.Context, args []driver.NamedValue) (r driver.Result, err error) {
var pstmt uintptr
var done int32
if ctx != nil {
if ctxDone := ctx.Done(); ctxDone != nil {
select {
case <-ctxDone:
return nil, ctx.Err()
default:
}
defer interruptOnDone(ctx, s.c, &done)()
}
}
defer func() {
if pstmt != 0 {
@ -514,13 +502,13 @@ func (s *stmt) exec(ctx context.Context, args []driver.NamedValue) (r driver.Res
err = e
}
}
if ctx != nil && atomic.LoadInt32(&done) != 0 {
r, err = nil, ctx.Err()
}
}()
for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0 && atomic.LoadInt32(&done) == 0; {
for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0; {
if err := ctx.Err(); err != nil {
return nil, err
}
if pstmt, err = s.c.prepareV2(&psql); err != nil {
return nil, err
}
@ -528,6 +516,7 @@ func (s *stmt) exec(ctx context.Context, args []driver.NamedValue) (r driver.Res
if pstmt == 0 {
continue
}
err = func() (err error) {
n, err := s.c.bindParameterCount(pstmt)
if err != nil {
@ -604,17 +593,6 @@ func (s *stmt) Query(args []driver.Value) (driver.Rows, error) { //TODO StmtQuer
func (s *stmt) query(ctx context.Context, args []driver.NamedValue) (r driver.Rows, err error) {
var pstmt uintptr
var done int32
if ctx != nil {
if ctxDone := ctx.Done(); ctxDone != nil {
select {
case <-ctxDone:
return nil, ctx.Err()
default:
}
defer interruptOnDone(ctx, s.c, &done)()
}
}
var allocs []uintptr
@ -630,14 +608,16 @@ func (s *stmt) query(ctx context.Context, args []driver.NamedValue) (r driver.Ro
}
}
if ctx != nil && atomic.LoadInt32(&done) != 0 {
r, err = nil, ctx.Err()
} else if r == nil && err == nil {
if r == nil && err == nil {
r, err = newRows(s.c, pstmt, allocs, true)
}
}()
for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0 && atomic.LoadInt32(&done) == 0; {
for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0; {
if err := ctx.Err(); err != nil {
return nil, err
}
if pstmt, err = s.c.prepareV2(&psql); err != nil {
return nil, err
}
@ -755,10 +735,6 @@ func (t *tx) exec(ctx context.Context, sql string) (err error) {
defer t.c.free(psql)
//TODO use t.conn.ExecContext() instead
if ctx != nil && ctx.Done() != nil {
defer interruptOnDone(ctx, t.c, nil)()
}
if rc := sqlite3.Xsqlite3_exec(t.c.tls, t.c.db, psql, 0, 0, 0); rc != sqlite3.SQLITE_OK {
return t.c.errstr(rc)
}
@ -766,51 +742,10 @@ func (t *tx) exec(ctx context.Context, sql string) (err error) {
return nil
}
// interruptOnDone sets up a goroutine to interrupt the provided db when the
// context is canceled, and returns a function the caller must defer so it
// doesn't interrupt after the caller finishes.
func interruptOnDone(
ctx context.Context,
c *conn,
done *int32,
) func() {
if done == nil {
var d int32
done = &d
}
donech := make(chan struct{})
go func() {
select {
case <-ctx.Done():
// don't call interrupt if we were already done: it indicates that this
// call to exec is no longer running and we would be interrupting
// nothing, or even possibly an unrelated later call to exec.
if atomic.AddInt32(done, 1) == 1 {
c.interrupt(c.db)
}
case <-donech:
}
}()
// the caller is expected to defer this function
return func() {
// set the done flag so that a context cancellation right after the caller
// returns doesn't trigger a call to interrupt for some other statement.
atomic.AddInt32(done, 1)
close(donech)
}
}
type conn struct {
db uintptr // *sqlite3.Xsqlite3
tls *libc.TLS
// Context handling can cause conn.Close and conn.interrupt to be invoked
// concurrently.
sync.Mutex
writeTimeFormat string
beginMode string
}
@ -1333,13 +1268,7 @@ func (c *conn) prepareV2(zSQL *uintptr) (pstmt uintptr, err error) {
//
// void sqlite3_interrupt(sqlite3*);
func (c *conn) interrupt(pdb uintptr) (err error) {
c.Lock() // Defend against race with .Close invoked by context handling.
defer c.Unlock()
if c.tls != nil {
sqlite3.Xsqlite3_interrupt(c.tls, pdb)
}
sqlite3.Xsqlite3_interrupt(c.tls, pdb)
return nil
}
@ -1460,15 +1389,11 @@ func (c *conn) Close() (err error) {
dmesg("conn %p: err %v", c, err)
}()
}
c.Lock() // Defend against race with .interrupt invoked by context handling.
defer c.Unlock()
if c.db != 0 {
if err := c.closeV2(c.db); err != nil {
return err
}
c.db = 0
}
@ -1476,6 +1401,7 @@ func (c *conn) Close() (err error) {
c.tls.Close()
c.tls = nil
}
return nil
}