ps-lite icon indicating copy to clipboard operation
ps-lite copied to clipboard

The thread safety of `RunCallback`

Open ZiyueHuang opened this issue 3 years ago • 1 comments

Is KVWorker<Val>::RunCallback thread safe?

According to iterator invalidation rules, rehashing invalidates the iterators of unordered_map, which may occur during the execution of the callback (it->second();). I didn't encounter any error about "invalid iterator" in my application, but I think it would be better to fix this,

  Callback cb = nullptr;
  mu_.lock();
  auto it = callbacks_.find(timestamp);
  if (it != callbacks_.end()) {
    cb = it->second;
    CHECK(cb);
    callbacks_.erase(it);
  }
  mu_.unlock();
  if (cb) cb();

cc @eric-haibin-lin

ZiyueHuang avatar Nov 07 '22 03:11 ZiyueHuang

Let me add more context. ZPush/ZPull insert new elements to callbacks_, while RunCallback eliminates some elements from callbacks_ and it is called inside Process on another thread created by Customer at this line and this line.

ZiyueHuang avatar Jan 18 '23 03:01 ZiyueHuang