Skip to content

Commit 6cc136b

Browse files
committed
Merge bitcoin/bitcoin#27556: wallet: fix deadlock in bdb read write operation
69d4390 test: add coverage for wallet read write db deadlock (furszy) 12daf6f walletdb: scope bdb::EraseRecords under a single db txn (furszy) 043fcb0 wallet: bugfix, GetNewCursor() misses to provide batch ptr to BerkeleyCursor (furszy) Pull request description: Decoupled from #26644 so it can closed in favor of #26715. Basically, with bdb, we can't make a write operation while we are traversing the db with the same db handler. These two operations are performed in different txn contexts and cause a deadlock. Added coverage by using `EraseRecords()` which is the simplest function that executes this process. To replicate it, need bdb support and drop the first commit. The test will run forever. ACKs for top commit: achow101: ACK 69d4390 hebasto: re-ACK 69d4390 Tree-SHA512: b3773be78925f674e962f4a5c54b398a9d0cfe697148c01c3ec0d68281cc5c1444b38165960d219ef3cf1a57c8ce6427f44a876275958d49bbc0808486e19d7d
2 parents ce2440e + 69d4390 commit 6cc136b

File tree

6 files changed

+56
-18
lines changed

6 files changed

+56
-18
lines changed

src/wallet/bdb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,14 +668,14 @@ void BerkeleyDatabase::ReloadDbEnv()
668668
env->ReloadDbEnv();
669669
}
670670

671-
BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database, BerkeleyBatch* batch)
671+
BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch)
672672
{
673673
if (!database.m_db.get()) {
674674
throw std::runtime_error(STR_INTERNAL_BUG("BerkeleyDatabase does not exist"));
675675
}
676676
// Transaction argument to cursor is only needed when using the cursor to
677677
// write to the database. Read-only cursors do not need a txn pointer.
678-
int ret = database.m_db->cursor(batch ? batch->txn() : nullptr, &m_cursor, 0);
678+
int ret = database.m_db->cursor(batch.txn(), &m_cursor, 0);
679679
if (ret != 0) {
680680
throw std::runtime_error(STR_INTERNAL_BUG(strprintf("BDB Cursor could not be created. Returned %d", ret)));
681681
}
@@ -713,7 +713,7 @@ BerkeleyCursor::~BerkeleyCursor()
713713
std::unique_ptr<DatabaseCursor> BerkeleyBatch::GetNewCursor()
714714
{
715715
if (!pdb) return nullptr;
716-
return std::make_unique<BerkeleyCursor>(m_database);
716+
return std::make_unique<BerkeleyCursor>(m_database, *this);
717717
}
718718

719719
bool BerkeleyBatch::TxnBegin()
@@ -825,7 +825,7 @@ bool BerkeleyBatch::HasKey(DataStream&& key)
825825
bool BerkeleyBatch::ErasePrefix(Span<const std::byte> prefix)
826826
{
827827
if (!TxnBegin()) return false;
828-
auto cursor{std::make_unique<BerkeleyCursor>(m_database, this)};
828+
auto cursor{std::make_unique<BerkeleyCursor>(m_database, *this)};
829829
// const_cast is safe below even though prefix_key is an in/out parameter,
830830
// because we are not using the DB_DBT_USERMEM flag, so BDB will allocate
831831
// and return a different output data pointer

src/wallet/bdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class BerkeleyCursor : public DatabaseCursor
192192
Dbc* m_cursor;
193193

194194
public:
195-
explicit BerkeleyCursor(BerkeleyDatabase& database, BerkeleyBatch* batch=nullptr);
195+
explicit BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch);
196196
~BerkeleyCursor() override;
197197

198198
Status Next(DataStream& key, DataStream& value) override;

src/wallet/test/util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ namespace wallet {
2222
class CWallet;
2323
class WalletDatabase;
2424

25+
static const DatabaseFormat DATABASE_FORMATS[] = {
26+
#ifdef USE_SQLITE
27+
DatabaseFormat::SQLITE,
28+
#endif
29+
#ifdef USE_BDB
30+
DatabaseFormat::BERKELEY,
31+
#endif
32+
};
33+
2534
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
2635

2736
// Creates a copy of the provided database

src/wallet/test/wallet_tests.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,6 @@ BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
426426
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 5, 50, 600), 300);
427427
}
428428

429-
static const DatabaseFormat DATABASE_FORMATS[] = {
430-
#ifdef USE_SQLITE
431-
DatabaseFormat::SQLITE,
432-
#endif
433-
#ifdef USE_BDB
434-
DatabaseFormat::BERKELEY,
435-
#endif
436-
};
437-
438429
void TestLoadWallet(const std::string& name, DatabaseFormat format, std::function<void(std::shared_ptr<CWallet>)> f)
439430
{
440431
node::NodeContext node;

src/wallet/test/walletdb_tests.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <clientversion.h>
77
#include <streams.h>
88
#include <uint256.h>
9+
#include <wallet/test/util.h>
10+
#include <wallet/wallet.h>
911

1012
#include <boost/test/unit_test.hpp>
1113

@@ -27,5 +29,31 @@ BOOST_AUTO_TEST_CASE(walletdb_readkeyvalue)
2729
BOOST_CHECK_THROW(ssValue >> dummy, std::ios_base::failure);
2830
}
2931

32+
BOOST_AUTO_TEST_CASE(walletdb_read_write_deadlock)
33+
{
34+
// Exercises a db read write operation that shouldn't deadlock.
35+
for (const DatabaseFormat& db_format : DATABASE_FORMATS) {
36+
// Context setup
37+
DatabaseOptions options;
38+
options.require_format = db_format;
39+
DatabaseStatus status;
40+
bilingual_str error_string;
41+
std::unique_ptr<WalletDatabase> db = MakeDatabase(m_path_root / strprintf("wallet_%d_.dat", db_format).c_str(), options, status, error_string);
42+
BOOST_ASSERT(status == DatabaseStatus::SUCCESS);
43+
44+
std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(db)));
45+
wallet->m_keypool_size = 4;
46+
47+
// Create legacy spkm
48+
LOCK(wallet->cs_wallet);
49+
auto legacy_spkm = wallet->GetOrCreateLegacyScriptPubKeyMan();
50+
BOOST_CHECK(legacy_spkm->SetupGeneration(true));
51+
wallet->Flush();
52+
53+
// Now delete all records, which performs a read write operation.
54+
BOOST_CHECK(wallet->GetLegacyScriptPubKeyMan()->DeleteRecords());
55+
}
56+
}
57+
3058
BOOST_AUTO_TEST_SUITE_END()
3159
} // namespace wallet

src/wallet/walletdb.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,9 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags)
11361136

11371137
bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
11381138
{
1139+
// Begin db txn
1140+
if (!m_batch->TxnBegin()) return false;
1141+
11391142
// Get cursor
11401143
std::unique_ptr<DatabaseCursor> cursor = m_batch->GetNewCursor();
11411144
if (!cursor)
@@ -1144,15 +1147,16 @@ bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
11441147
}
11451148

11461149
// Iterate the DB and look for any records that have the type prefixes
1147-
while (true)
1148-
{
1150+
while (true) {
11491151
// Read next record
11501152
DataStream key{};
11511153
DataStream value{};
11521154
DatabaseCursor::Status status = cursor->Next(key, value);
11531155
if (status == DatabaseCursor::Status::DONE) {
11541156
break;
11551157
} else if (status == DatabaseCursor::Status::FAIL) {
1158+
cursor.reset(nullptr);
1159+
m_batch->TxnAbort(); // abort db txn
11561160
return false;
11571161
}
11581162

@@ -1163,10 +1167,16 @@ bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
11631167
key >> type;
11641168

11651169
if (types.count(type) > 0) {
1166-
m_batch->Erase(key_data);
1170+
if (!m_batch->Erase(key_data)) {
1171+
cursor.reset(nullptr);
1172+
m_batch->TxnAbort();
1173+
return false; // erase failed
1174+
}
11671175
}
11681176
}
1169-
return true;
1177+
// Finish db txn
1178+
cursor.reset(nullptr);
1179+
return m_batch->TxnCommit();
11701180
}
11711181

11721182
bool WalletBatch::TxnBegin()

0 commit comments

Comments
 (0)