Skip to content

Commit 1f8cfe5

Browse files
mutex to protect mpool caches (#548)
Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent 0ec0155 commit 1f8cfe5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

core/storage/mpool/mpool.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ namespace fc::storage::mpool {
541541
[&](auto, auto bls, auto &cid, auto *smsg, auto *msg)
542542
-> outcome::result<void> {
543543
if (bls) {
544+
std::lock_guard bls_cache_lock{bls_cache_mutex_};
544545
if (auto sig{bls_cache.get(cid)}) {
545546
mpool::add(pending, {*msg, *sig});
546547
}
@@ -760,6 +761,7 @@ namespace fc::storage::mpool {
760761

761762
outcome::result<void> MessagePool::add(const SignedMessage &message) {
762763
if (message.signature.isBls()) {
764+
std::lock_guard bls_cache_lock{bls_cache_mutex_};
763765
bls_cache.insert(message.getCid(), message.signature);
764766
}
765767
OUTCOME_TRY(setCbor(ipld, message));
@@ -792,6 +794,7 @@ namespace fc::storage::mpool {
792794
remove(msg->from, msg->nonce);
793795
} else {
794796
if (bls) {
797+
std::lock_guard bls_cache_lock{bls_cache_mutex_};
795798
if (auto sig{bls_cache.get(cid)}) {
796799
OUTCOME_TRY(add({*msg, *sig}));
797800
}
@@ -833,9 +836,11 @@ namespace fc::storage::mpool {
833836
if (!address.isId()) {
834837
return ERROR_TEXT("Cannot resolve actor address to key address.");
835838
}
839+
std::unique_lock resloved_cache_lock{resolved_cache_mutex_};
836840
if (auto resolved{resolved_cache_.get(address)}) {
837841
return *resolved;
838842
}
843+
resloved_cache_lock.unlock();
839844

840845
std::shared_lock head_lock(head_mutex_);
841846
ChainEpoch height = head_->height();
@@ -846,6 +851,7 @@ namespace fc::storage::mpool {
846851
const auto maybe_resolved =
847852
resolveKeyAtHeight(address, height - kChainFinality, ts_branch);
848853
if (maybe_resolved) {
854+
std::lock_guard resloved_cache_lock_guard{resolved_cache_mutex_};
849855
resolved_cache_.insert(address, maybe_resolved.value());
850856
return maybe_resolved.value();
851857
}

core/storage/mpool/mpool.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,18 @@ namespace fc::storage::mpool {
171171
std::mutex publishing_mutex_;
172172

173173
mutable lru_cache<CID, Signature> bls_cache{0};
174+
mutable std::mutex bls_cache_mutex_;
175+
174176
boost::signals2::signal<Subscriber> signal;
175177
mutable std::default_random_engine generator;
176178
mutable std::normal_distribution<> distribution;
179+
177180
// cache of resolved addresses
178181
mutable lru_cache<Address, Address> resolved_cache_{kResolvedCacheSize};
179-
180-
mutable std::shared_mutex local_addresses_mutex_;
182+
mutable std::mutex resolved_cache_mutex_;
181183

182184
lru_cache<Address, Empty> local_addresses_{kLocalAddressesCacheSize};
185+
mutable std::shared_mutex local_addresses_mutex_;
183186

184187
common::Logger logger_;
185188
};

0 commit comments

Comments
 (0)