[core] Fix the risk of iterator invalidation issue. (#20989)

We erase the elements from object_id_refs_ in the method `RemoveLocalReferenceInternal()` which may cause iterator invalidation issue.

Note that, normally flatmap will not trigger any iterator invalidation except triggering `rehash()`. But in this case, we may remove other elements(not only the current iterator), so there is still a risk of it.
This commit is contained in:
Qing Wang 2021-12-11 07:59:16 +08:00 committed by GitHub
parent 3a5dd9a10b
commit a3bf1af10e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -256,11 +256,15 @@ void ReferenceCounter::SetNestedRefInUseRecursive(ReferenceTable::iterator inner
void ReferenceCounter::ReleaseAllLocalReferences() {
absl::MutexLock lock(&mutex_);
std::vector<ObjectID> refs_to_remove;
for (auto &ref : object_id_refs_) {
for (int i = ref.second.local_ref_count; i > 0; --i) {
RemoveLocalReferenceInternal(ref.first, nullptr);
refs_to_remove.push_back(ref.first);
}
}
for (const auto &object_id_to_remove : refs_to_remove) {
RemoveLocalReferenceInternal(object_id_to_remove, nullptr);
}
}
void ReferenceCounter::RemoveLocalReference(const ObjectID &object_id,