skip to content

A C# gateway shares one Dictionary<string, int> of rate-limit counters across request threads without a lock; what can concurrent writes do, and are concurrent reads safe?

level: seniorimportance: should knowfreq 42%

answer

  1. no synchronisation inside
  2. multi-step insert and resize
  3. silent loss or an exception
  4. readers need zero writers

basics

~10 s

Concurrent writes can corrupt Dictionary<TKey,TValue>: entries vanish silently, or calls throw exceptions such as IndexOutOfRangeException or InvalidOperationException. Concurrent reads are safe only while no thread writes. Use ConcurrentDictionary, or one lock around every access.

solid answer

~50 s

`Dictionary<TKey,TValue>` does no synchronisation. An insert updates its bucket array, its entries array and its count in separate steps, and a resize copies everything into new arrays, so two writers can overwrite each other's slot, link a bucket chain into a loop, or write into an array the other thread just replaced. On .NET 10 the same unsafe test gives different results per run: entries silently lost (a presized dictionary usually kept about a third of 100,000 distinct keys), `IndexOutOfRangeException`, or `InvalidOperationException` saying "Operations that change non-concurrent collections must have exclusive access." That last one comes from a collision counter in modern .NET that throws on a looped chain instead of spinning forever, as .NET Framework could. Many readers are safe only when nobody writes; one writer makes the readers unsafe too. Fix it with `ConcurrentDictionary<string,int>` and `AddOrUpdate`, or a `lock` around every access.

code

csharp · 18 lines
csharp
// Unsafe on purpose: four threads write one plain Dictionary.
var hits = new Dictionary<int, int>(100_000);

Task[] writers = Enumerable.Range(0, 4).Select(w => Task.Run(() =>
{
    for (int i = w * 25_000; i < (w + 1) * 25_000; i++)
        hits[i] = 1; // every key is distinct, written by one thread only
})).ToArray();

try
{
    await Task.WhenAll(writers);
    Console.WriteLine($"expected 100000, Count = {hits.Count}");
}
catch (Exception e)
{
    Console.WriteLine($"{e.GetType().Name}: {e.Message}");
}

go deeper

for a junior

Recall that Dictionary<TKey,TValue> is not thread-safe for writes, and that shared mutable state across request threads needs ConcurrentDictionary or a lock.

for a middle

Explain why: an insert and a resize are several unprotected steps, so writers can claim the same slot or write into replaced arrays, even with distinct keys.

for a senior

Diagnose it from the symptoms: counts that drift, IndexOutOfRangeException deep inside Dictionary, or the 'must have exclusive access' InvalidOperationException far from the racing write. Replace the instance, then fix the sharing.

for a principal

Decide how the codebase prevents it: analyzers or review rules for static mutable dictionaries, build-then-publish for read-only data, and a stated default type for shared maps.

## Dictionary<TKey,TValue> has no thread safety `Dictionary<TKey,TValue>` in `System.Collections.Generic` is built for one thread at a time. The .NET docs on thread-safe collections say the generic collections "don't provide any thread synchronization; user code must provide all synchronization when items are added or removed on multiple threads concurrently." Internally it keeps: - an `int[]` of **buckets**, each holding the index of the first entry in its chain; - an array of **entries** (`hashCode`, `next`, `key`, `value` structs); - a **count**, a **free list** for removed slots, and a version number for enumerators. Adding a new key touches several of these in sequence: pick a slot (from the free list or at `count`), write the entry, link it into the bucket, bump the count. When the arrays are full, `Resize` allocates bigger arrays, copies every entry and rehashes the buckets. None of that is protected. ## What concurrent writers break Two threads interleaving those steps can: 1. **Claim the same slot.** Both read `count == 5`, both write entry 5, and one key silently disappears. 2. **Lose a whole resize.** One thread resizes and swaps in new arrays while the other writes into the old ones; the write is gone, or an index computed for one array is used on the other. 3. **Corrupt a chain.** A `next` link can end up pointing back into its own chain, so a later lookup would walk in a circle. Running four tasks that each write 25,000 distinct keys into one shared dictionary on .NET 10 produced all three kinds of outcome: | Variant | What happened across runs | |---|---| | `new Dictionary<int,int>()` | `IndexOutOfRangeException` most runs, `InvalidOperationException` in others | | `new Dictionary<int,int>(100_000)` (no resize) | no exception; `Count` usually around 30,000 to 40,000, once all 100,000 | Notice that the keys were all **distinct**: you do not need two threads writing the same key to lose data. ## The collision counter in modern .NET While walking a bucket chain, the .NET 10 `Dictionary` counts the entries it has visited. If the count exceeds the number of entries, the chain must contain a loop, and the source comment says it throws "rather than looping forever": `InvalidOperationException` with the message *"Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct."* .NET Framework's dictionary has no such check, so the same bug there could pin a thread at 100% CPU in an endless lookup. Two practical consequences: - **The exception is a symptom, not the bug.** It surfaces in whichever call next walks the broken chain, often a read far from the write that raced. - **The instance is finished.** Entries may be missing or duplicated; replace the dictionary and fix the sharing, never catch and retry. - **Tests rarely catch it.** The race needs truly parallel writes, so a single-threaded unit test passes, and even a parallel test can pass, as the presized variant once kept all 100,000 keys. ## Reads are safe only without writers Many threads may call `TryGetValue`, the indexer getter and `ContainsKey` at once, because reads do not change the structure. That holds **only while no thread writes**. A single writer can resize under a reader, which then reads half-updated arrays, gets a wrong answer or throws. The classic safe pattern is *build, then publish*: fill the dictionary on one thread at startup, hand it out as an `IReadOnlyDictionary<TKey,TValue>`, and never write again. For that read-only phase a `FrozenDictionary` (.NET 8) is an option too, which belongs with immutable and frozen collections. ## Fixing the gateway's counters Rate-limit counters are read and written by every request, so they need one of: - **`ConcurrentDictionary<string,int>`** with `counters.AddOrUpdate(tenant, 1, (_, n) => n + 1)`, which retries until its update lands. Writing `counters[tenant]++` on a `ConcurrentDictionary` is still a read and a separate write, and loses counts. - **A `lock` around every read and write** of a plain `Dictionary<string,int>`, simple and correct when the map is small or the critical section also touches other state. What does not work: making the field `readonly` (that freezes the reference, not the contents) or `volatile` (that orders reads and writes of the field itself, not the dictionary's internal steps).

  • Does declaring the field static readonly or volatile make the shared Dictionary safe?
    No. `readonly` stops the field from being reassigned; the dictionary behind it is still mutable. `volatile` affects reads and writes of the field itself, not the many steps inside `Add` or the indexer setter. Only a lock around every access, or a type built for concurrency such as `ConcurrentDictionary<TKey,TValue>`, protects the contents.
  • A Dictionary is filled at startup and only read afterwards; does it need ConcurrentDictionary?
    No. As long as every write finishes before other threads can see the dictionary, and nothing writes afterwards, concurrent reads of a plain `Dictionary` are safe and faster. Publish it as `IReadOnlyDictionary<TKey,TValue>` so nobody adds later, or build a `FrozenDictionary` on .NET 8 or later for faster lookups.
  • After the InvalidOperationException about concurrent updates, can the service keep using that dictionary?
    No. The message itself says the collection's state is no longer correct: entries may be lost, duplicated or linked in a loop. Retrying the call hides the corruption. Replace the instance and fix the root cause, which is unsynchronised writes, by switching to `ConcurrentDictionary` or adding a lock.

  • Distinct keys never collide, so concurrent writes of different keys are safe.
  • Concurrent reads are always safe, even while another thread is adding.
  • The worst case is an ArgumentException for a duplicate key.
  • Catching the InvalidOperationException and retrying the write fixes it.
  • Marking the dictionary field volatile makes its methods thread-safe.

Assumes .NET 10, whose Dictionary throws InvalidOperationException on a looped bucket chain; .NET Framework's Dictionary lacks that check and could loop forever.