Merge pull request #1500 from cypherstack/fix/cryptonote-refresh-lock
What changed, and why it matters
This update fixes a lock-handling bug in the wallet refresh code for three privacy-focused cryptocurrencies (Monero, Wownero, Salvium). Previously, the refresh lock could be released in the wrong place or not released at all if an error occurred, which could leave the wallet stuck and unable to refresh. The change wraps the refresh work in a safer 'protect' block that guarantees the lock is released on every exit, including when something goes wrong.
Review and merge if not already merged; verify that refreshMutex.protect is implemented to release on both normal and exceptional exits. Consider adding automated tests that throw inside refresh() to confirm the lock is always released. No immediate user action is required beyond updating to the fixed release.
Security signals we found
Mutex lock not released on exception paths (potential denial-of-service / wallet freeze)
Manual lock release replaced with scoped protect() guard
Race condition between sync progress and rescan progress guard addressed
Applies consistently across three Cryptonote wallet implementations
Evidence from the diff
The commit refactors refreshMutex usage in lib_monero_wallet.dart, lib_salvium_wallet.dart, and lib_wownero_wallet.dart. It replaces manual acquire()/release() pairs with refreshMutex.protect(() async { … }), ensuring the mutex is released even if the guarded block throws. It also removes a now-redundant sync-progress-based release in the sync-status listener and adds a second reset of highestPercentCached after rescanBlockchain to prevent stale progress values from pinning the guard near 100%.
Changed components
lib/wallets/wallet/intermediate/lib_monero_wallet.dartlib/wallets/wallet/intermediate/lib_salvium_wallet.dartlib/wallets/wallet/intermediate/lib_wownero_wallet.dartInspect captured patch +50 / −77
### lib/wallets/wallet/intermediate/lib_monero_wallet.dart
@@ -394,6 +394,10 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
highestPercentCached = 0;
await csMonero.rescanBlockchain(wallet!);
+ // Reset again once the worker has accepted the rescan. A progress
+ // reading from before it can land in between and pin the guard near
+ // 100%, which hides every later reading until the rescan passes it.
+ highestPercentCached = 0;
await csMonero.startSyncing(wallet!);
unawaited(save());
});
@@ -882,10 +886,6 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
final _syncStatus = syncStatus;
if (_syncStatus != null) {
- if (_syncStatus.progress() == 1 && refreshMutex.isLocked) {
- refreshMutex.release();
- }
-
WalletSyncStatus? status;
xmrAndWowSyncSpecificFunctionThatShouldBeGottenRidOfInTheFuture(true);
@@ -1120,51 +1120,40 @@ abstract class LibMoneroWallet<T extends CryptonoteCurrency>
Logging.instance.t(
"$runtimeType refresh() refreshMutex.acquire() waiting...",
);
- await refreshMutex.acquire();
- Logging.instance.t(
- "$runtimeType refresh() refreshMutex.acquire() acquired!",
- );
-
- Logging.instance.t("$runtimeType refresh() final node = getCurrentNode();");
- final node = getCurrentNode();
-
- Logging.instance.i(
- "$runtimeType refresh() await _torNodeMismatchGuard(node)",
- );
- if (await _torNodeMismatchGuard(node)) {
- throw Exception("TOR – clearnet mismatch");
- }
+ await refreshMutex.protect(() async {
+ Logging.instance.t(
+ "$runtimeType refresh() final node = getCurrentNode();",
+ );
+ final node = getCurrentNode();
- Logging.instance.t(
- "$runtimeType refresh() it csMonero.startSyncing(wallet!);",
- );
- await csMonero.startSyncing(wallet!);
- Logging.instance.t(
- "$runtimeType refresh() _setSyncStatus(lib_monero_compat.StartingSyncStatus());",
- );
- _setSyncStatus(lib_monero_compat.StartingSyncStatus());
+ Logging.instance.i(
+ "$runtimeType refresh() await _torNodeMismatchGuard(node)",
+ );
+ if (await _torNodeMismatchGuard(node)) {
+ throw Exception("TOR – clearnet mismatch");
+ }
- Logging.instance.t("$runtimeType refresh() await updateTransactions();");
- await updateTransactions();
- Logging.instance.t("$runtimeType refresh() await updateBalance();");
- await updateBalance();
+ Logging.instance.t(
+ "$runtimeType refresh() it csMonero.startSyncing(wallet!);",
+ );
+ await csMonero.startSyncing(wallet!);
+ Logging.instance.t(
+ "$runtimeType refresh() _setSyncStatus(lib_monero_compat.StartingSyncStatus());",
+ );
+ _setSyncStatus(lib_monero_compat.StartingSyncStatus());
- Logging.instance.t(
- "$runtimeType refresh() await checkReceivingAddressForTransactions();",
- );
- if (info.otherData[WalletInfoKeys.reuseAddress] != true) {
- await checkReceivingAddressForTransactions();
- }
+ Logging.instance.t("$runtimeType refresh() await updateTransactions();");
+ await updateTransactions();
+ Logging.instance.t("$runtimeType refresh() await updateBalance();");
+ await updateBalance();
- Logging.instance.t(
- "$runtimeType refresh() refreshMutex.isLocked=${refreshMutex.isLocked} pre release.",
- );
- if (refreshMutex.isLocked) {
- refreshMutex.release();
Logging.instance.t(
- "$runtimeType refresh() refreshMutex.isLocked manually released.",
+ "$runtimeType refresh() await checkReceivingAddressForTransactions();",
);
- }
+ if (info.otherData[WalletInfoKeys.reuseAddress] != true) {
+ await checkReceivingAddressForTransactions();
+ }
+ });
Logging.instance.t(
"$runtimeType refresh() wallet != null && await csMonero.isSynced(wallet!)",
### lib/wallets/wallet/intermediate/lib_salvium_wallet.dart
@@ -863,10 +863,6 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
final _syncStatus = syncStatus;
if (_syncStatus != null) {
- if (_syncStatus.progress() == 1 && refreshMutex.isLocked) {
- refreshMutex.release();
- }
-
WalletSyncStatus? status;
xmrAndWowSyncSpecificFunctionThatShouldBeGottenRidOfInTheFuture(true);
@@ -1100,21 +1096,17 @@ abstract class LibSalviumWallet<T extends CryptonoteCurrency>
// this acquire should be almost instant due to above check.
// Slight possibility of race but should be irrelevant
- await refreshMutex.acquire();
-
- csSalvium.startSyncing(wallet!);
- _setSyncStatus(StartingSyncStatus());
-
- await updateTransactions();
- await updateBalance();
+ await refreshMutex.protect(() async {
+ csSalvium.startSyncing(wallet!);
+ _setSyncStatus(StartingSyncStatus());
- if (info.otherData[WalletInfoKeys.reuseAddress] != true) {
- await checkReceivingAddressForTransactions();
- }
+ await updateTransactions();
+ await updateBalance();
- if (refreshMutex.isLocked) {
- refreshMutex.release();
- }
+ if (info.otherData[WalletInfoKeys.reuseAddress] != true) {
+ await checkReceivingAddressForTransactions();
+ }
+ });
final synced = wallet != null && await csSalvium.isSynced(wallet!);
### lib/wallets/wallet/intermediate/lib_wownero_wallet.dart
@@ -899,10 +899,6 @@ abstract class LibWowneroWallet<T extends CryptonoteCurrency>
final _syncStatus = syncStatus;
if (_syncStatus != null) {
- if (_syncStatus.progress() == 1 && refreshMutex.isLocked) {
- refreshMutex.release();
- }
-
WalletSyncStatus? status;
xmrAndWowSyncSpecificFunctionThatShouldBeGottenRidOfInTheFuture(true);
@@ -1137,21 +1133,17 @@ abstract class LibWowneroWallet<T extends CryptonoteCurrency>
// this acquire should be almost instant due to above check.
// Slight possibility of race but should be irrelevant
- await refreshMutex.acquire();
-
- csWownero.startSyncing(wallet!);
- _setSyncStatus(lib_monero_compat.StartingSyncStatus());
-
- await updateTransactions();
- await updateBalance();
+ await refreshMutex.protect(() async {
+ csWownero.startSyncing(wallet!);
+ _setSyncStatus(lib_monero_compat.StartingSyncStatus());
- if (info.otherData[WalletInfoKeys.reuseAddress] != true) {
- await checkReceivingAddressForTransactions();
- }
+ await updateTransactions();
+ await updateBalance();
- if (refreshMutex.isLocked) {
- refreshMutex.release();
- }
+ if (info.otherData[WalletInfoKeys.reuseAddress] != true) {
+ await checkReceivingAddressForTransactions();
+ }
+ });
final synced = wallet != null && await csWownero.isSynced(wallet!);
Why this scored 46/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.