mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-07 03:03:58 +01:00
Merge bitcoin/bitcoin#30697: Bugfix: Ensure Atomicity in Wallet Settings Updates from Chain Interface
1b41d45d46wallet: bugfix: ensure atomicity in settings updates (ismaelsadeeq) Pull request description: This PR fixes #30620. As outlined in the issue, creating two wallets with `load_on_startup=true` simultaneously results in only one wallet being added to the startup file. The current issue arises because the wallet settings update process involves: 1. Obtaining the settings value while acquiring the settings lock. 2. Modifying the settings value. 3. Overwriting the settings value while acquiring the settings lock again. This sequence is not thread-safe. Different threads could modify the same base value simultaneously, overwriting data from other workers without realizing it. The PR attempts to fix this by modifying the chain interface's `updateRwSetting` method to accept a function that will be called with the settings reference. This function will either update or delete the setting and return an enum indicating whether the settings need to be overwritten in this or not. Additionally, this PR introduces two new methods to the chain interface: - `overwriteRwSetting`: This method replaces the setting with a new value. Used in `VerifyWallets` - `deleteRwSettings`: This method completely erases a specified setting. This method is currently used only in `overwriteRwSetting`. These changes ensure that updates are race-free across all clients. ACKs for top commit: achow101: ACK1b41d45d46furszy: self-code-ACK1b41d45d46Tree-SHA512: 50cda612b782aeb5e03e2cf63cc44779a013de1c535b883b57af4de22f24b0de80b4edecbcda235413baec0a12bdf0e5750fb6731c9e67d32e742d8c63f08c13
This commit is contained in:
@@ -814,14 +814,32 @@ public:
|
||||
});
|
||||
return result;
|
||||
}
|
||||
bool updateRwSetting(const std::string& name, const common::SettingsValue& value, bool write) override
|
||||
bool updateRwSetting(const std::string& name,
|
||||
const interfaces::SettingsUpdate& update_settings_func) override
|
||||
{
|
||||
std::optional<interfaces::SettingsAction> action;
|
||||
args().LockSettings([&](common::Settings& settings) {
|
||||
auto* ptr_value = common::FindKey(settings.rw_settings, name);
|
||||
// Create value if it doesn't exist
|
||||
auto& value = ptr_value ? *ptr_value : settings.rw_settings[name];
|
||||
action = update_settings_func(value);
|
||||
});
|
||||
if (!action) return false;
|
||||
// Now dump value to disk if requested
|
||||
return *action == interfaces::SettingsAction::SKIP_WRITE || args().WriteSettingsFile();
|
||||
}
|
||||
bool overwriteRwSetting(const std::string& name, common::SettingsValue& value, bool write) override
|
||||
{
|
||||
if (value.isNull()) return deleteRwSettings(name, write);
|
||||
return updateRwSetting(name, [&](common::SettingsValue& settings) {
|
||||
settings = std::move(value);
|
||||
return write ? interfaces::SettingsAction::WRITE : interfaces::SettingsAction::SKIP_WRITE;
|
||||
});
|
||||
}
|
||||
bool deleteRwSettings(const std::string& name, bool write) override
|
||||
{
|
||||
args().LockSettings([&](common::Settings& settings) {
|
||||
if (value.isNull()) {
|
||||
settings.rw_settings.erase(name);
|
||||
} else {
|
||||
settings.rw_settings[name] = value;
|
||||
}
|
||||
settings.rw_settings.erase(name);
|
||||
});
|
||||
return !write || args().WriteSettingsFile();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user