Skip to content

vmcp: validating cache discards loaded value without cleanup when losing concurrent Set race #4837

Closed as not planned

Description

@yrobla

Summary

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–135
if alreadySet, _ := c.lruCache.ContainsOrAdd(key, v); alreadySet {
    if winner, ok := c.lruCache.Get(key); ok {
        return result{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:

if alreadySet, _ := c.lruCache.ContainsOrAdd(key, v); alreadySet {
    if c.onEvict != nil {
        c.onEvict(key, v) // release resources held by the discarded value
    }
    if winner, ok := c.lruCache.Get(key); ok {
        return result{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.

References

Activity

  1. added
    bug
    go
    vmcp
    on Apr 15, 2026
  2. yrobla commented on Apr 15, 2026

    @yrobla
    ContributorAuthor

    Fixed directly in code — is now stored on the struct and called when a loaded value loses the race.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggovmcp

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions