Skip to content

Commit c21e0e6

Browse files
committed
crimson/common/shared_lru: invalidate Deleter's cache
Once we destruct SharedLRU, SharedLRU::weak_refs map is destroyed. As a weak refernce might outlive the SharedLRU itself, when destroying the object via the custom Deleter, we try to access the already destroyed SharedLRU instance's weak ref map. Instead, invalidate the custom Deleter (Deleter::cache), when destructing the SharedLRU. Fixes: https://tracker.ceph.com/issues/66478 Signed-off-by: Matan Breizman <[email protected]>
1 parent 610e1a2 commit c21e0e6

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/crimson/common/shared_lru.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ class SharedLRU {
2525
SimpleLRU<K, shared_ptr_t, false> cache;
2626
std::map<K, std::pair<weak_ptr_t, V*>> weak_refs;
2727

28+
// Once all of the shared pointers are destoryed,
29+
// erase the tracked object from the weak_ref map
30+
// before actually destorying it
2831
struct Deleter {
2932
SharedLRU<K,V>* cache;
3033
const K key;
3134
void operator()(V* ptr) {
32-
cache->_erase_weak(key);
35+
if (cache) {
36+
cache->_erase_weak(key);
37+
}
3338
delete ptr;
3439
}
3540
};
@@ -42,9 +47,19 @@ class SharedLRU {
4247
{}
4348
~SharedLRU() {
4449
cache.clear();
50+
4551
// initially, we were assuming that no pointer obtained from SharedLRU
4652
// can outlive the lru itself. However, since going with the interruption
4753
// concept for handling shutdowns, this is no longer valid.
54+
// Moreover, before clearing weak_refs, invalidate each deleter
55+
// cache pointer as this SharedLRU is being destoryed.
56+
for (const auto& [key, value] : weak_refs) {
57+
shared_ptr_t val;
58+
val = value.first.lock();
59+
auto this_deleter = get_deleter<Deleter>(val);
60+
this_deleter->cache = nullptr;
61+
}
62+
4863
weak_refs.clear();
4964
}
5065
/**

0 commit comments

Comments
 (0)