Repository navigation
Description
Expected Behavior
Removing a wallet while one of its shielded operations is running does not hold the caller for the length of that operation. Either the removal is refused at once with a typed, retryable error and changes nothing (what shielded Clear does from the pull request that references this issue), or the native removal runs off the main actor, as #5221 proposes for the shielded pass case.
Current Behavior
On v5.1-dev (3b4117c64d):
PlatformWalletManager::remove_wallet_with_teardown(packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs) takes the wallet generation's exclusive lifecycle gate first:candidate.generation().teardown_guard().await.- A shielded operation on that wallet holds the shared side of the same gate (
generation_payment_guard(),packages/rs-platform-wallet/src/wallet/platform_wallet.rs) from its liveness check to its result, so across note selection, the proof, the broadcast and the wait for the result. That coversshielded_token_purchase_from_pool,shielded_token_transfer,shielded_token_unshield,create_private_document_entry,write_dashpay_profile,bind_shielded,bind_shielded_from_persisted,shielded_add_accountandprepare_shielded_identity_address. - So the removal waits for the whole operation. The gate is a tokio
RwLock, which prefers writers: while the removal waits, every new payment operation on that wallet queues behind it. platform_wallet_manager_remove_wallet(packages/rs-platform-wallet-ffi/src/manager.rs) runs this withruntime().block_on(...)on the calling thread, insidePLATFORM_WALLET_MANAGER_STORAGE.with_item.- Swift's
PlatformWalletManager.deleteWallet(walletId:)is synchronous on the main actor, so the UI is frozen for that time. Its guardensureSyncNativeOpAllowedrefuses only while a native operation that Swift admitted is in flight, and the shielded spend wrappers are not admitted onv5.1-dev(swift-sdk: shielded spend wrappers on v5.1-dev run without native-operation admission, so Clear, wallet deletion or shutdown can overlap a payment #6060, swift-sdk/platform-wallet: hold native-op admission and the generation guard across shielded spends #5868), so it does not stop this case. Kotlin'sremoveWalletruns onDispatchers.IO: no frozen UI, but the same wait and the same queue behind it.
The wait itself is intended and tested (public_remove_wallet_waits_for_an_in_flight_payment_on_that_generation in packages/rs-platform-wallet-ffi/src/manager.rs): a removal must not complete between a payment's liveness check and its publication. What is not handled is how long a shielded payment keeps the gate, and that the caller is the main actor.
This is a different lock from the one a shielded Clear used. Clear waited on the coordinator's operation_admission; NetworkShieldedCoordinator::unregister_wallet does not take it. Removal waits on the per-wallet generation gate before it reaches the coordinator, then on the coordinator's lifecycle mutex (held by a bind's install transaction), then on the store's write lock (the shielded pass case, #5221).
Possible Solution
- Refuse instead of wait, as Clear does:
try_writeon the generation gate and a typed, retryable error when a payment operation holds it. The rule would live in Rust, so iOS and Android behave the same. Whether a removal may be refused at all needs a decision: a full wipe wants to end, and a refusal there needs a retry. - Or an async
deleteWallet(walletId:)in Swift that runs the native call off the main actor (swift-sdk: deleteWallet(walletId:) blocks the main actor while a shielded pass runs #5221's direction). The removal still waits and new payments still queue behind it. - swift-sdk: shielded spend wrappers on v5.1-dev run without native-operation admission, so Clear, wallet deletion or shutdown can overlap a payment #6060 and swift-sdk/platform-wallet: hold native-op admission and the generation guard across shielded spends #5868 (native operation admission for the Swift shielded spend wrappers) would make
deleteWalletthrowinvalidHandlewhile an admitted spend runs. That removes the freeze on iOS for the wrappers they cover, with an untyped error, and changes nothing on Android.
Steps to Reproduce (for bugs)
- Bind a wallet and start a token pool transfer from it (
shielded_token_transfer), which holdsgeneration_payment_guard()through the proof and the broadcast. - While it runs, call
deleteWallet(walletId:)(Swift) orremoveWallet(Kotlin) for that wallet. - The call returns only when the transfer has returned. On iOS the main actor is blocked until then.
Found by reading the code while changing shielded Clear to refuse during a shielded operation. Not reproduced on a device.
Context
- swift-sdk: deleteWallet(walletId:) blocks the main actor while a shielded pass runs #5221:
deleteWalletblocks the main actor while a shielded pass runs (the store write lock). Same symptom, different lock. - swift-sdk: shielded spend wrappers on v5.1-dev run without native-operation admission, so Clear, wallet deletion or shutdown can overlap a payment #6060, swift-sdk/platform-wallet: hold native-op admission and the generation guard across shielded spends #5868: native operation admission for the Swift shielded spend wrappers.
- platform-wallet: fence shielded host-key writes against wallet deletion #5936: fence shielded host-key writes against wallet deletion.
🤖 Generated with Claude Code
Activity