Fixed memory leak after loading block cache

This commit is contained in:
SChernykh 2022-03-24 18:30:23 +01:00
parent 8b27faad6d
commit 028a5d0c88
2 changed files with 41 additions and 11 deletions

View file

@ -47,6 +47,7 @@ P2PServer::P2PServer(p2pool* pool)
, m_cache(pool->params().m_blockCache ? new BlockCache() : nullptr)
, m_cacheLoaded(false)
, m_initialPeerList(pool->params().m_p2pPeerList)
, m_cachedBlocks(nullptr)
, m_rng(RandomDeviceSeed::instance)
, m_block(new PoolBlock())
, m_timer{}
@ -128,18 +129,34 @@ void P2PServer::add_cached_block(const PoolBlock& block)
return;
}
PoolBlock* new_block = new PoolBlock(block);
m_cachedBlocks.insert({ new_block->m_sidechainId, new_block });
if (!m_cachedBlocks) {
m_cachedBlocks = new unordered_map<hash, PoolBlock*>();
}
if (m_cachedBlocks->find(block.m_sidechainId) == m_cachedBlocks->end()) {
PoolBlock* new_block = new PoolBlock(block);
m_cachedBlocks->insert({ new_block->m_sidechainId, new_block });
}
}
void P2PServer::clear_cached_blocks()
{
if (!m_cachedBlocks) {
return;
}
WriteLock lock(m_cachedBlocksLock);
for (auto it : m_cachedBlocks) {
if (!m_cachedBlocks) {
return;
}
for (auto it : *m_cachedBlocks) {
delete it.second;
}
m_cachedBlocks.clear();
delete m_cachedBlocks;
m_cachedBlocks = nullptr;
}
void P2PServer::store_in_cache(const PoolBlock& block)
@ -934,12 +951,14 @@ void P2PServer::download_missing_blocks()
return;
}
ReadLock lock2(m_cachedBlocksLock);
// Try to download each block from a random client
for (const hash& id : missing_blocks) {
P2PClient* client = clients[get_random64() % clients.size()];
{
MutexLock lock2(m_missingBlockRequestsLock);
MutexLock lock3(m_missingBlockRequestsLock);
const uint64_t truncated_block_id = *reinterpret_cast<const uint64_t*>(id.h);
if (!m_missingBlockRequests.insert({ client->m_peerId, truncated_block_id }).second) {
@ -949,6 +968,15 @@ void P2PServer::download_missing_blocks()
}
}
if (m_cachedBlocks) {
auto it = m_cachedBlocks->find(id);
if (it != m_cachedBlocks->end()) {
LOGINFO(5, "using cached block for id = " << id);
client->handle_incoming_block_async(it->second);
continue;
}
}
const bool result = send(client,
[&id](void* buf)
{
@ -1944,11 +1972,13 @@ void P2PServer::P2PClient::post_handle_incoming_block(const uint32_t reset_count
ReadLock lock(server->m_cachedBlocksLock);
for (const hash& id : missing_blocks) {
auto it = server->m_cachedBlocks.find(id);
if (it != server->m_cachedBlocks.end()) {
LOGINFO(5, "using cached block for id = " << id);
handle_incoming_block_async(it->second);
continue;
if (server->m_cachedBlocks) {
auto it = server->m_cachedBlocks->find(id);
if (it != server->m_cachedBlocks->end()) {
LOGINFO(5, "using cached block for id = " << id);
handle_incoming_block_async(it->second);
continue;
}
}
const bool result = m_owner->send(this,

View file

@ -149,7 +149,7 @@ private:
uint32_t m_maxIncomingPeers;
uv_rwlock_t m_cachedBlocksLock;
unordered_map<hash, PoolBlock*> m_cachedBlocks;
unordered_map<hash, PoolBlock*>* m_cachedBlocks;
private:
static void on_timer(uv_timer_t* timer) { reinterpret_cast<P2PServer*>(timer->data)->on_timer(); }