don't wait for SharedPreferences writes when switching wallets (#3782)
What changed, and why it matters
This change makes two SharedPreferences (app settings) writes run in the background instead of waiting for them to finish when switching wallets. The goal is to speed up wallet switching on Android, where these writes were slow. The in-memory values are updated immediately, so the app should not read old data. There is no clear security bug here, but making disk writes asynchronous can slightly raise the risk of data loss or inconsistent state if the app crashes before the write completes.
No immediate security action required. If reviewing for robustness, verify that SharedPreferences handles concurrent writes safely and that the app does not rely on the persisted current-wallet name/type being on disk immediately after wallet switch (e.g., before a crash or forced stop).
Security signals we found
Async persistence of current wallet name/type metadata
Async persistence of background sync trigger timestamp
No input validation, cryptographic, or authentication changes
No vendor disclosure of security relevance
Evidence from the diff
The commit converts awaited SharedPreferences.setString/setInt calls to unawaited futures in WalletLoadingService and AppStoreBase. On Android, the legacy SharedPreferences implementation commits synchronously to disk, so awaiting these calls blocked wallet switch/open paths. The in-memory cache is updated before the async write begins, so readers see current values. This is a performance optimization, not a security fix. The security-relevant concern is a marginal increase in durability/consistency risk if the process terminates before the async write flushes, but no vulnerability or exploit path is evident from the diff.
Changed components
lib/core/wallet_loading_service.dartlib/store/app_store.dartInspect captured patch +7 / −5
### lib/core/wallet_loading_service.dart
@@ -63,8 +63,8 @@ class WalletLoadingService {
{String? password, bool isBackground = false}) async {
try {
if (!isBackground) {
- await sharedPreferences.setString(
- PreferencesKey.backgroundSyncLastTrigger(name), DateTime.now().toIso8601String());
+ unawaited(sharedPreferences.setString(
+ PreferencesKey.backgroundSyncLastTrigger(name), DateTime.now().toIso8601String()));
}
final walletService = walletServiceFactory.call(type);
final walletPassword = password ?? (await keyService.getWalletPassword(walletName: name));
### lib/store/app_store.dart
@@ -1,3 +1,5 @@
+import "dart:async";
+
import "package:cake_wallet/core/amount_parsing_proxy.dart";
import "package:cake_wallet/di.dart";
import "package:cake_wallet/entities/preferences_key.dart";
@@ -70,10 +72,10 @@ abstract class AppStoreBase with Store {
} else if (previousWalletType != null && isWalletConnectCompatibleChain(previousWalletType)) {
_queueWalletConnectAction(_disposeWalletConnect);
}
- await getIt.get<SharedPreferences>().setString(PreferencesKey.currentWalletName, wallet.name);
- await getIt
+ unawaited(getIt.get<SharedPreferences>().setString(PreferencesKey.currentWalletName, wallet.name));
+ unawaited(getIt
.get<SharedPreferences>()
- .setInt(PreferencesKey.currentWalletType, serializeToInt(wallet.type));
+ .setInt(PreferencesKey.currentWalletType, serializeToInt(wallet.type)));
}
Future<void> _lastWalletConnectAction = Future.value();Why this scored 19/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.