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

+10
View File
@@ -68,6 +68,11 @@ type Store interface {
// Parameters:
// key: The key associated with the Stmt object to be deleted.
Delete(key string)
// Close releases resources held by the store, stopping any background
// maintenance goroutines started for cache expiration. It is safe to call
// Close multiple times. The store should not be used after Close is called.
Close()
}
// defaultMaxSize defines the default maximum capacity of the cache.
@@ -137,6 +142,11 @@ func (s *lruStore) Delete(key string) {
s.lru.Remove(key)
}
// Close stops the underlying LRU cache's background expiration goroutine.
func (s *lruStore) Close() {
s.lru.Close()
}
type ConnPool interface {
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}