Skip to content

InMemoryCacheProvider.GetOrAdd overwrites a concurrently Set value, and racing callers get an instance that isn't the cached one #85

Closed

Description

@matt-edmondson

What's wrong

InMemoryCacheProvider (Essentials.CacheProviders.InMemory/InMemoryCacheProvider.cs) doesn't declare GetOrAdd, so it inherits the default body at Essentials/ICacheProvider.cs:77-88:

if (TryGet(key, out TValue? value)) { return value!; }
TValue newValue = factory(key);
Set(key, newValue, expiration);   // unconditional cache[key] = ...
return newValue;

Check and write are separate, and Set overwrites unconditionally:

  1. A concurrent Set is lost. Thread A misses and runs a slow factory; thread B calls Set(key, fresh); A's Set then replaces B's value. Deterministic repro:
    string r = cache.GetOrAdd("k", key => { cache.Set(key, "fresh-from-concurrent-writer"); return "factory"; });
    // cache now holds "factory"; the concurrent writer's value is gone
  2. Racing callers diverge. Two threads calling GetOrAdd("k", _ => new object()) on a cold key (synchronised with a Barrier) each get a different instance; only the last writer's is cached. Measured: same instance: False; caller0 holds cached: False; caller1 holds cached: True.

Why it matters

The name and the ConcurrentDictionary backing both imply add-if-absent semantics: ConcurrentDictionary.GetOrAdd may run the factory twice but always returns the stored value and never overwrites. Callers using GetOrAdd to share a single instance (connections, locks, lazily built objects) silently get duplicates. #47 fixed the mirror-image race (expired-entry removal deleting a fresh Set) but did not touch GetOrAdd.

Suggested fix / acceptance criteria

  • Declare GetOrAdd on InMemoryCacheProvider: run the factory, then TryAdd; if an unexpired entry exists, return it and discard the factory result; if an expired entry exists, replace only that observed entry via TryUpdate(key, newEntry, observed) with retry.
  • GetOrAddAsync picks up the fix through its default.
  • Tests: a Set performed inside the factory (or via the InMemoryCacheProvider.TryGet can delete a fresh value that another thread just Set, when it sees an expired entry for the same key #47-style key type) survives GetOrAdd; two concurrent cold GetOrAdd calls return the same instance, which is the cached one.
  • Optionally document on ICacheProvider.GetOrAdd whether the default implementation is atomic.

Activity

  1. added
    bug
    on Oct 6, 2026
  2. matt-edmondson commented on Oct 6, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: Medium. This is a real concurrency correctness bug: a concurrent Set is lost, and racing callers get different instances. It only bites under contention, and callers relying on GetOrAdd for single-instance sharing are the ones affected.
    • Area / suggested assignment: Cache providers (Essentials.CacheProviders.InMemory, ICacheProvider default interface methods).
    • Duplicates / in progress: No duplicate found, and no open PR covers this. It is the mirror image of InMemoryCacheProvider.TryGet can delete a fresh value that another thread just Set, when it sees an expired entry for the same key #47, which is fixed.
    • Notes: Also check whether other cache providers inherit the same non-atomic default, and document the atomicity contract on ICacheProvider.GetOrAdd.

    Generated by Claude Code

  3. added a commit that references this issue on Oct 26, 2026
    9f26f7b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions