Merge pull request #1416 from mattn/fix-stmt-close-race

Fix race in SQLiteStmt.Close by holding conn lock across cache check
This commit is contained in:
mattn
2026-07-06 16:42:34 +00:00
committed by GitHub

View File

@@ -1970,6 +1970,10 @@ func (c *SQLiteConn) putCachedStmt(s *SQLiteStmt) bool {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
return c.putCachedStmtLocked(s)
}
func (c *SQLiteConn) putCachedStmtLocked(s *SQLiteStmt) bool {
if c.db == nil { if c.db == nil {
return false return false
} }
@@ -2164,12 +2168,20 @@ func (s *SQLiteStmt) Close() error {
s.c = nil s.c = nil
return nil return nil
} }
if !conn.dbConnOpen() { if s.cacheKey != "" {
conn.mu.Lock()
if conn.db == nil {
conn.mu.Unlock()
return errors.New("sqlite statement with already closed database connection") return errors.New("sqlite statement with already closed database connection")
} }
if s.cacheKey != "" && conn.putCachedStmt(s) { if conn.putCachedStmtLocked(s) {
conn.mu.Unlock()
return nil return nil
} }
conn.mu.Unlock()
} else if !conn.dbConnOpen() {
return errors.New("sqlite statement with already closed database connection")
}
s.s = nil s.s = nil
s.c = nil s.c = nil
rv := C.sqlite3_finalize(stmt) rv := C.sqlite3_finalize(stmt)