Merge bitcoin/bitcoin#24168: Fix some race conditions in BanMan::DumpBanlist()

99a6b699cd650f13d7200d344bf5e2d4b45b20ac Fix race condition for SetBannedSetDirty() calls (Hennadii Stepanov)
83c76467157bbca023bffda0f0bc2f01eb76a040 Avoid calling BanMan::SweepBanned() twice in a row (Hennadii Stepanov)
33bda6ab87cc1b569e96da337296eb3e9ce6db1a Fix data race condition in BanMan::DumpBanlist() (Hennadii Stepanov)
5e20e9ec3859205c220867ca49efb752b8edaacc Prevent possible concurrent CBanDB::Write() calls (Hennadii Stepanov)

Pull request description:

  This PR split from bitcoin/bitcoin#24097 with some additions. This makes the following switch from `RecursiveMutex` to `Mutex` a pure refactoring.

  See details in commit messages.

ACKs for top commit:
  w0xlt:
    reACK 99a6b69
  shaavan:
    ACK 99a6b699cd650f13d7200d344bf5e2d4b45b20ac

Tree-SHA512: da4e7268c7bd3424491f446145f18af4ccfc804023d0a7fe70e1462baab550a5e44f9159f8b9f9c7820d2c6cb6447b63883616199e4d9d439ab9ab1b67c7201b
This commit is contained in:
MarcoFalke 2022-01-31 08:55:33 +01:00 committed by pasta
parent 595a1b3a8e
commit 50287e2403
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984

View File

@ -7,6 +7,7 @@
#include <netaddress.h>
#include <node/ui_interface.h>
#include <sync.h>
#include <util/system.h>
#include <util/time.h>
#include <util/translation.h>
@ -39,18 +40,23 @@ BanMan::~BanMan()
void BanMan::DumpBanlist()
{
SweepBanned(); // clean unused entries (if bantime has expired)
if (!BannedSetIsDirty()) return;
int64_t n_start = GetTimeMillis();
static Mutex dump_mutex;
LOCK(dump_mutex);
banmap_t banmap;
GetBanned(banmap);
if (m_ban_db.Write(banmap)) {
{
LOCK(m_cs_banned);
SweepBanned();
if (!BannedSetIsDirty()) return;
banmap = m_banned;
SetBannedSetDirty(false);
}
int64_t n_start = GetTimeMillis();
if (!m_ban_db.Write(banmap)) {
SetBannedSetDirty(true);
}
LogPrint(BCLog::NET, "Flushed %d banned node addresses/subnets to disk %dms\n", banmap.size(),
GetTimeMillis() - n_start);
}