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

The thread safety of RunCallback #189

Open
ZiyueHuang opened this issue Nov 7, 2022 · 1 comment · May be fixed by #191
Open

The thread safety of RunCallback #189

ZiyueHuang opened this issue Nov 7, 2022 · 1 comment · May be fixed by #191

Comments

@ZiyueHuang
Copy link
Member

Is KVWorker::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 ZiyueHuang linked a pull request Jan 11, 2023 that will close this issue
@ZiyueHuang
Copy link
Member Author

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.

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

Successfully merging a pull request may close this issue.

1 participant