Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data race in folly::ConcurentHashMap. #1280

Open
Milerius opened this issue Dec 14, 2019 · 1 comment
Open

Data race in folly::ConcurentHashMap. #1280

Milerius opened this issue Dec 14, 2019 · 1 comment

Comments

@Milerius
Copy link

Milerius commented Dec 14, 2019

Hello the following use case will data-race:

// Thread 1:

void awesome_function() {
  if (my_map.empty()) { //! call size() internally;
      
  }
}

//! Thread 2:

void another_awesome_function() {
  //! Clear internally lock size_ with a mutex data race occur in the thread 1 because size_ is not locked
  my_map.clear(); //!
}

Potential solution (size() function can lock size_):

 size_t size() {
  	std::lock_guard<Mutex> g(m_);
    return size_;
  }

Where size_ is locked ?

void clear(hazptr_obj_batch<Atom>* batch) {
    size_t bcount = bucket_count_.load(std::memory_order_relaxed);
    Buckets* buckets;
    auto newbuckets = Buckets::create(bcount, batch);
    {
      std::lock_guard<Mutex> g(m_); //< here size_ is locked
      buckets = buckets_.load(std::memory_order_relaxed);
      buckets_.store(newbuckets, std::memory_order_release);
      size_ = 0;
    }
    buckets->retire(concurrenthashmap::HazptrTableDeleter(bcount));
  }

Another solution can be to have an std::atomicstd::size_t and returning size_.load(); when necessary.

@magedm
Copy link
Contributor

magedm commented Dec 15, 2019

Thank you for reporting this and the fix suggestions. We'll fix shortly.

facebook-github-bot pushed a commit that referenced this issue Jan 3, 2020
Summary: Eliminate data race reported in #1280

Reviewed By: davidtgoldblatt

Differential Revision: D19072137

fbshipit-source-id: ca2431b8b3b6114694f25024cac0cc0433acfe8b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants