Fix race in SQLiteStmt.Close by holding conn lock across cache check

This commit is contained in:
Yasuhiro Matsumoto
2026-07-07 00:31:45 +09:00
parent a40eeff51e
commit 34c9c34da4

View File

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