Skip to content

platform-wallet: wallet removal waits for a shielded operation in flight, and Swift's synchronous deleteWallet holds the main actor for that time #6175

Backlog

Description

@QuantumExplorer

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):

  1. 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.
  2. 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 covers shielded_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_account and prepare_shielded_identity_address.
  3. 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.
  4. platform_wallet_manager_remove_wallet (packages/rs-platform-wallet-ffi/src/manager.rs) runs this with runtime().block_on(...) on the calling thread, inside PLATFORM_WALLET_MANAGER_STORAGE.with_item.
  5. Swift's PlatformWalletManager.deleteWallet(walletId:) is synchronous on the main actor, so the UI is frozen for that time. Its guard ensureSyncNativeOpAllowed refuses only while a native operation that Swift admitted is in flight, and the shielded spend wrappers are not admitted on v5.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's removeWallet runs on Dispatchers.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

Steps to Reproduce (for bugs)

  1. Bind a wallet and start a token pool transfer from it (shielded_token_transfer), which holds generation_payment_guard() through the proof and the broadcast.
  2. While it runs, call deleteWallet(walletId:) (Swift) or removeWallet (Kotlin) for that wallet.
  3. 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

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions