Skip to content

Commit eacc0e8

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24168: Fix some race conditions in BanMan::DumpBanlist()
99a6b69 Fix race condition for SetBannedSetDirty() calls (Hennadii Stepanov) 83c7646 Avoid calling BanMan::SweepBanned() twice in a row (Hennadii Stepanov) 33bda6a Fix data race condition in BanMan::DumpBanlist() (Hennadii Stepanov) 5e20e9e 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 99a6b69 Tree-SHA512: da4e7268c7bd3424491f446145f18af4ccfc804023d0a7fe70e1462baab550a5e44f9159f8b9f9c7820d2c6cb6447b63883616199e4d9d439ab9ab1b67c7201b
2 parents 4efdbab + 99a6b69 commit eacc0e8

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/banman.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <netaddress.h>
99
#include <node/ui_interface.h>
10+
#include <sync.h>
1011
#include <util/system.h>
1112
#include <util/time.h>
1213
#include <util/translation.h>
@@ -39,18 +40,23 @@ BanMan::~BanMan()
3940

4041
void BanMan::DumpBanlist()
4142
{
42-
SweepBanned(); // clean unused entries (if bantime has expired)
43-
44-
if (!BannedSetIsDirty()) return;
45-
46-
int64_t n_start = GetTimeMillis();
43+
static Mutex dump_mutex;
44+
LOCK(dump_mutex);
4745

4846
banmap_t banmap;
49-
GetBanned(banmap);
50-
if (m_ban_db.Write(banmap)) {
47+
{
48+
LOCK(m_cs_banned);
49+
SweepBanned();
50+
if (!BannedSetIsDirty()) return;
51+
banmap = m_banned;
5152
SetBannedSetDirty(false);
5253
}
5354

55+
int64_t n_start = GetTimeMillis();
56+
if (!m_ban_db.Write(banmap)) {
57+
SetBannedSetDirty(true);
58+
}
59+
5460
LogPrint(BCLog::NET, "Flushed %d banned node addresses/subnets to disk %dms\n", banmap.size(),
5561
GetTimeMillis() - n_start);
5662
}

0 commit comments

Comments
 (0)