Await Monero wallet close before deleting its files (#3757)
What changed, and why it matters
This change fixes a bug in Cake Wallet's Monero wallet deletion. Previously, when a user deleted a wallet, the app started closing the wallet in the background and immediately tried to delete its files. Because the wallet was still writing its cache to disk, the deletion could fail or leave behind a broken wallet record in the app's database. The fix makes the app wait for the wallet to fully close before deleting anything, preventing incomplete deletions and leftover database entries.
Treat as a reliability and data-integrity fix. Verify that closeWalletAwaitIfShould is no longer used in deletion paths and that the Isolate.run close path properly handles errors. Consider whether other wallet types or platforms have similar fire-and-forget close behavior during deletion. No immediate exploit response is indicated, but users who previously deleted wallets may have orphaned database rows or leftover files.
Security signals we found
Race condition between wallet close and filesystem deletion
Potential leftover WalletInfo row without .keys file
Data integrity issue in wallet deletion path
Fix uses synchronous-style await via Isolate.run to ensure close completes
Evidence from the diff
In cw_monero/lib/monero_wallet_service.dart, the remove() method previously called closeWalletAwaitIfShould(wmaddr, waddr) and then proceeded to delete the wallet directory. On mobile, closeWalletAwaitIfShould appears to have been fire-and-forget or otherwise allowed the caller to continue before the Monero wallet close completed. The wallet close operation writes cache data, so concurrent deletion could race with that write, causing deletion failure and leaving a WalletInfo row without a corresponding .keys file. The patch removes the closeWalletAwaitIfShould call, clears currentWallet if it matches the removed wallet, and explicitly awaits an Isolate.run() that calls WalletManager_closeWallet and drains the error string, ensuring the close completes before file deletion proceeds.
Changed components
cw_monero/lib/monero_wallet_service.dartMonero wallet deletion/remove flowWalletInfo database stateInspect captured patch +9 / −2
### cw_monero/lib/monero_wallet_service.dart
@@ -221,13 +221,20 @@ class MoneroWalletService extends WalletService<
Future<void> remove(String wallet) async {
final path = await pathForWalletDir(name: wallet, type: getType());
if (openedWalletsByPath["$path/$wallet"] != null) {
- // NOTE: this is realistically only required on windows.
printV("closing wallet");
final w = openedWalletsByPath["$path/$wallet"]!;
final wmaddr = wmPtr.ffiAddress();
final waddr = w.ffiAddress();
openedWalletsByPath.remove("$path/$wallet");
- await closeWalletAwaitIfShould(wmaddr, waddr);
+ if (currentWallet?.ffiAddress() == waddr) {
+ currentWallet = null;
+ }
+ // must finish before deleting the files, a pending close stores the cache mid-deletion
+ await Isolate.run(() {
+ monero.WalletManager_closeWallet(
+ Pointer.fromAddress(wmaddr), Pointer.fromAddress(waddr), false);
+ monero.WalletManager_errorString(Pointer.fromAddress(wmaddr));
+ });
printV("wallet closed");
}
Why this scored 54/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.