Ayende @ Rahien

It's a girl

Why is this not thread safe?

Originally posted at 4/15/2011

Take a look at the following piece of code:

if(items.ContainsKey(key) == false)
{
    lock(items)
    {
        if(items.ContainsKey(key) == false)
        {
            items.Add(key, val);
        }
    }
}

The answer is quite simple, and it is located directly in the docs:

image

Yes, on the face of it, this is safe code, in the sense that we will never get DuplicateKeyException. But the implementation of ContainsKey() isn’t safe to run when Add() is also executing.

The actual behavior depends on the implementation, but it is easy enough to imagine a scenario where invariants that ContainsKey() relies on are broken for the duration of the Add() call.

Comments

tobi
04/26/2011 09:20 AM by
tobi

For example during an array expansion.

john wingfield
04/26/2011 01:09 PM by
john wingfield

so what's the solution? could you use a resetEvent to only block when writing?

Alex Simkin
04/26/2011 01:31 PM by
Alex Simkin

Just inner part works fine:

lock(items)

{

    if(items.ContainsKey(key) == false)

    {

        items.Add(key, val);

    }

}
Jon Wingfield
04/26/2011 01:49 PM by
Jon Wingfield

@Alex, that's the most pessimistic approach though.

Eber Irigoyen
04/26/2011 02:41 PM by
Eber Irigoyen

So this code is thread safe, the code that would not be thread safe would be the one enumerating the collection

Erik Juhlin
04/26/2011 06:06 PM by
Erik Juhlin

@Eber, no it's not. The ContainsKey call outside of the lock is not thread safe.

configurator
04/26/2011 06:16 PM by
configurator

A ReaderWriterLockSlim would be a good solution here.

ConcurrentDictionary is sometimes clunky, it all depends on the rest of the using code; in this example, it would be easy to replace it with TryAdd, but if anyone else uses this dictionary they'd need to change their code. Of course, with a ReaderWriterLock they'd need to acquire an appropriate lock, but it's much easier to avoid issues that way sometimes.

chibacity
04/27/2011 08:29 AM by
chibacity

The example code is not thread-safe as the data structure is mutable, and an unlocked read operation is allowed. At any given time other thread(s) may be adding new items to the collection and therefore mutating it. If ContainsKey is executing whist the collection is being mutated, the outcome is indeterminate. For instance ContainsKey may depend on intermediate results as it executes which could become invalid as the collection is mutated by the addition of a new item e.g. the collection may have to be re-arranged to maintain the load factor when a new item is added. This could produce an incorrect result or an exception during the execution of ContainsKey.

billy
04/27/2011 11:55 AM by
billy

@configurator you might want to consider that it is still several times slower to acquire and release a reader writer lock than a standard lock. If the thing you are doing inside the lock is fast, a simple lock should be fine.

You can test it out to see what performance gain you would actually get, my guess is hardly any.

Dotan
04/27/2011 11:57 AM by
Dotan

This is a pretty common pitfall. It might show itself as a livelock (very high or 100% CPU in production).

Simone
04/28/2011 08:47 PM by
Simone

ConcurrentDictionary is the solution, and has a great API too

Sony Mathew
05/02/2011 06:07 PM by
Sony Mathew

This code is perfectly thread safe and the documentation supplied does not invalidate the thread safety of the code as I see it. Once 'items' is locked successfully by a thread, no modifications can occur, your reads including enumerations and your if-check will be guaranteed.

Harry Steinhilber
05/03/2011 12:34 AM by
Harry Steinhilber

@Sony,

No, it isn't thread safe. If thread A enters the lock and ContainsKey returns false, it will call Add. Now if in the middle of the Add (before it completes), thread B calls ContainsKey on the dictionary, the invariants of ContainsKey can be broken. As Ayende states, this is implementation dependent. But consider, what if Add had finished adding the key (so ContainsKey returns true) but not the value. At this point the indexer could return null or throw an exception if you try to access a key you believe is already in the dictionary. Hence, not thread safe.

Sony Mathew
05/05/2011 07:14 PM by
Sony Mathew

I missed the last fine-print of Ayende's entry. Ok, I'll agree that in a bad implementation, thread B while executing its outer ContainsKey(o) enumerating can potentially break.

Comments have been closed on this topic.