You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
vmcp: validating cache discards loaded value without cleanup when losing concurrent Set race #4837
In pkg/cache/validating_cache.go, when a concurrent Set wins the ContainsOrAdd race, the freshly loaded value v is silently discarded without invoking any cleanup or eviction callback. For resource-holding values such as vmcpsession.MultiSession (restored by Manager.loadSession), this can leak connections and other resources.
Root Cause
The load() path uses ContainsOrAdd to guard against concurrent writes. When alreadySet == true, the concurrent winner's value is returned and the loaded value v is dropped:
// pkg/cache/validating_cache.go lines 129–135ifalreadySet, _:=c.lruCache.ContainsOrAdd(key, v); alreadySet {
ifwinner, ok:=c.lruCache.Get(key); ok {
returnresult{v: winner}, nil
}
// v is discarded here — no cleanup invoked
}
The LRU cache's onEvict hook is only triggered for entries that are actually evicted from the cache. A value that is never stored (because it lost the race) never triggers onEvict, so any resources it holds are never released.
Impact
vmcpsession.MultiSession holds backend connections. A discarded session leaks those connections.
Any other resource-holding type stored in this cache is similarly affected.
The leak is non-deterministic and only occurs under concurrent load, making it difficult to reproduce in tests.
Proposed Fix
When a loaded value loses the ContainsOrAdd race, invoke the cleanup hook (e.g., onEvict, or a new dedicated onDiscard) before returning the winner:
ifalreadySet, _:=c.lruCache.ContainsOrAdd(key, v); alreadySet {
ifc.onEvict!=nil {
c.onEvict(key, v) // release resources held by the discarded value
}
ifwinner, ok:=c.lruCache.Get(key); ok {
returnresult{v: winner}, nil
}
}
Alternatively, introduce a separate onDiscard callback to distinguish between eviction (capacity/TTL) and load-race discard semantics, and document/test both code paths.
Activity
yrobla commentedon Apr 15, 2026
Fixed directly in code — is now stored on the struct and called when a loaded value loses the race.