fix: stop prepared statement cache cleanup goroutine on Close

The expirable LRU cache backing the prepared statement store starts a
background deleteExpired goroutine when it is created with a non-zero TTL.
Its Close method was commented out, so the goroutine kept running for the
lifetime of the process and leaked every time a PreparedStmtDB was
discarded (for example when a *sql.DB is closed and reopened), causing
steady goroutine and memory growth in long-running services.

Expose LRU.Close, add Close to the stmt_store.Store interface and its
lruStore implementation, and call it from PreparedStmtDB.Close so the
background cleanup goroutine is stopped together with the cached
statements. Close is idempotent and safe for concurrent use.

Refs #7788
This commit is contained in:
dualfroz committed 2026-09-02 11:03:59 +02:00
1 parent 6182f42ca4
commit 3461e6bce4
4 files changed
+74 -14

No files matched your search

+4 -1
View File
@@ -48,7 +48,9 @@ func (db *PreparedStmtDB) GetDBConn() (*sql.DB, error) {
return nil, ErrInvalidDB
}
// Close closes all prepared statements in the store
// Close closes all prepared statements in the store and stops the store's
// background maintenance goroutine to avoid leaking it for the lifetime of the
// process.
func (db *PreparedStmtDB) Close() {
db.Mux.Lock()
defer db.Mux.Unlock()
@@ -56,6 +58,7 @@ func (db *PreparedStmtDB) Close() {
for _, key := range db.Stmts.Keys() {
db.Stmts.Delete(key)
}
db.Stmts.Close()
}
// Reset Deprecated use Close instead