facebook / facebook/folly

Data race in folly::ConcurentHashMap.

Open
#1,280 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
30.5k
Forks
5.9k
PR merge metrics
No merged PRs in 30d

Description

https://github.com/facebook/folly/blob/f6ac9ac15bce487b8a87b2a9e06949eb55074e99/folly/concurrency/detail/ConcurrentHashMap-detail.h#L247

Hello the following use case will data-race:

```cpp
// 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_):

```cpp
size_t size() {
std::lock_guard g(m_);
return size_;
}
```

Where size_ is locked ?
```cpp
void clear(hazptr_obj_batch* batch) {
size_t bcount = bucket_count_.load(std::memory_order_relaxed);
Buckets* buckets;
auto newbuckets = Buckets::create(bcount, batch);
{
std::lock_guard 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::atomic and returning size_.load(); when necessary.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.