Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,970 | Comments: 44,492

filter by tags archive

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

For example during an array expansion.

john wingfield

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

Alex Simkin

Just inner part works fine:

lock(items)

{

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

    {

        items.Add(key, val);

    }

}
Jon Wingfield

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

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

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

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

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

@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

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

Simone

ConcurrentDictionary is the solution, and has a great API too

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

@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

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.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. RavenDB On Linux–Status Update - 4 hours from now

There are posts all the way to Jul 31, 2015

RECENT SERIES

  1. Production postmortem (5):
    29 Jul 2015 - The evil licensing code
  2. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
  3. API Design (7):
    20 Jul 2015 - We’ll let the users sort it out
  4. What is new in RavenDB 3.5 (3):
    15 Jul 2015 - Exploring data in the dark
  5. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats