Ignore deleted wallet IDs during wallet shutdown
What changed, and why it matters
This commit fixes a crash that could occur when the app shuts down a wallet. Previously, if a wallet ID was still listed in the user's sync preferences but the matching wallet had been deleted from the database, the code would try to read missing data and crash. The fix makes the code safely skip over deleted wallet IDs instead of assuming they always exist.
Treat as a routine stability/DoS-hardening fix. Review whether other code paths that read `walletIdsSyncOnStartup` also assume entries exist, and consider pruning deleted IDs from preferences when a wallet is removed.
Security signals we found
Null-dereference / type-cast crash fixed by removing non-null assertion
Crash occurs during wallet shutdown / background sync cleanup
Behavior depends on stale/deleted wallet IDs in user preferences
No input validation or authorization checks visible in diff
Evidence from the diff
In lib/wallets/wallet/wallet.dart, the shutdown path iterated over prefs.walletIdsSyncOnStartup and called mainDB.isar.walletInfo.where().walletIdEqualTo(id).findFirstSync()! with a non-null assertion. If a wallet had been deleted, findFirstSync() returned null and the unguarded ! caused a runtime exception. The patch removes the ! and uses a null-aware check (wallet?.coin == cryptoCurrency) so missing wallet records are ignored. A new test verifies that Wallet.exit() completes without error for Monero/Wownero/Salvium wallets when a startup-sync list contains a deleted wallet ID.
Changed components
lib/wallets/wallet/wallet.dartWallet shutdown / exit logicStartup wallet sync preference handlingMonero/Wownero/Salvium wallet subclassesInspect captured patch +73 / −2
### lib/wallets/wallet/wallet.dart
@@ -777,9 +777,9 @@ abstract class Wallet<T extends CryptoCurrency> {
final wallet = mainDB.isar.walletInfo
.where()
.walletIdEqualTo(id)
- .findFirstSync()!;
+ .findFirstSync();
- if (wallet.coin == cryptoCurrency) {
+ if (wallet?.coin == cryptoCurrency) {
walletIds.add(id);
}
}
### test/wallets/native_wallet_exit_polling_test.dart
@@ -1,14 +1,32 @@
import 'dart:async';
+import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
+import 'package:isar_community/isar.dart';
+import 'package:stackwallet/db/isar/main_db.dart';
+import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
+import 'package:stackwallet/utilities/enums/sync_type_enum.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
import 'package:stackwallet/wallets/crypto_currency/intermediate/cryptonote_currency.dart';
+import 'package:stackwallet/wallets/isar/models/wallet_info.dart';
import 'package:stackwallet/wallets/wallet/impl/monero_wallet.dart';
import 'package:stackwallet/wallets/wallet/impl/wownero_wallet.dart';
import 'package:stackwallet/wallets/wallet/impl/salvium_wallet.dart';
import 'package:stackwallet/wallets/wallet/wallet.dart';
+import '../support/isar_test_utils.dart';
+
+class _Prefs extends Fake implements Prefs {
+ _Prefs(this.syncType);
+
+ @override
+ final SyncingType syncType;
+
+ @override
+ List<String> get walletIdsSyncOnStartup => ['deleted-wallet', 'live-wallet'];
+}
+
mixin _PollingProbe on Wallet<CryptonoteCurrency> {
int pings = 0;
@override
@@ -46,6 +64,34 @@ class _OwnedTimer implements Timer {
}
void main() {
+ late Directory directory;
+ late Isar isar;
+ setUpAll(() async {
+ await initializeTestIsar();
+ directory = await Directory.systemTemp.createTemp('native-exit-');
+ isar = await Isar.open(
+ [WalletInfoSchema],
+ directory: directory.path,
+ name: 'native-exit',
+ inspector: false,
+ );
+ await MainDB.instance.initMainDB(mock: isar);
+ await isar.writeTxn(
+ () => isar.walletInfo.put(
+ WalletInfo(
+ walletId: 'live-wallet',
+ name: 'Live selected wallet',
+ mainAddressType: AddressType.p2wpkh,
+ coinName: 'bitcoin',
+ ),
+ ),
+ );
+ });
+ tearDownAll(() async {
+ await isar.close();
+ await directory.delete(recursive: true);
+ });
+
for (final entry in <String, _PollingProbe Function()>{
"Monero": _Monero.new,
"Wownero": _Wownero.new,
@@ -81,5 +127,30 @@ void main() {
),
);
});
+
+ for (final syncType in [
+ SyncingType.currentWalletOnly,
+ SyncingType.selectedWalletsAtStartup,
+ ]) {
+ test(
+ '${entry.key} exits with a deleted ${syncType.name} wallet',
+ () async {
+ await runZoned(
+ () async {
+ final wallet = entry.value()
+ ..mainDB = MainDB.instance
+ ..prefs = _Prefs(syncType);
+ await wallet.exit();
+ expect(wallet.shouldAutoSync, isFalse);
+ },
+ zoneSpecification: ZoneSpecification(
+ // Prevent the unrelated deferred native DB watcher from starting.
+ createTimer: (self, parent, zone, duration, callback) =>
+ _OwnedTimer(),
+ ),
+ );
+ },
+ );
+ }
}
}Why this scored 42/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.