diff --git a/TalerWallet1/Views/Actions/Banking/DepositAmountAction.swift b/TalerWallet1/Views/Actions/Banking/DepositAmountAction.swift index 8ffae2ec..48fb7f37 100644 --- a/TalerWallet1/Views/Actions/Banking/DepositAmountAction.swift +++ b/TalerWallet1/Views/Actions/Banking/DepositAmountAction.swift @@ -22,7 +22,22 @@ struct DepositAmountAction: View { @EnvironmentObject private var controller: Controller @EnvironmentObject private var model: WalletModel + @AppStorage("developerMode") var developerMode: Bool = false + @State private var balanceIndex = 0 + /// The deposit in flight, so "Abort" can stop waiting for it. + /// + /// It could not: `Abort`'s action is `dismissTop`, which hides the + /// screen and leaves the `Task` running. Issue 11983. + /// + /// What this does NOT do, and the comment is here so nobody reads more + /// into it: cancelling the Swift task stops the UI waiting, and does + /// not cancel the operation inside wallet-core. Aborting that needs a + /// wallet-core-side abort for the deposit group, which the app has no + /// request for. + @State private var depositTask: Task? = nil + /// Why the deposit did not start, if it did not. + @State private var depositError: Error? = nil /// Bumped on every `.BalanceChange`, and folded into the task id below. /// /// The maximum this screen shows is read once per balance selection: @@ -97,24 +112,80 @@ struct DepositAmountAction: View { } } + /// How long to wait for wallet-core to create the deposit group. + /// + /// Nothing on this path bounded the wait: not this call site, not + /// `WalletModel.sendRequest`, and not + /// `WalletCore.sendFormattedRequest`, which is a bare + /// `withCheckedThrowingContinuation` -- if wallet-core never calls + /// back, the `await` never returns and "Depositing..." stays for as + /// long as the user looks at it. Issue 11983. + /// + /// 30 s because the exchange round trips inside `createDepositGroup` + /// are the ones a withdrawal also makes, and a full bank-integrated + /// withdrawal reaches `Done` in about 30 s on this harness -- of which + /// 30 s is one deliberate `timeout_ms=30000` long poll on `/reserves`. + /// A deposit that has not answered in 30 s is not a slow network. + private static let depositDeadlineS: UInt64 = 30 + @MainActor private func startDeposit(_ scope: ScopeInfo) { if let paytoUri { depositStarted = true // don't run twice - Task { + depositError = nil + depositTask = Task { symLog.log("Deposit") - if let result = try? await model.createDepositGroup(paytoUri, - scope: scope, - amount: amountToTransfer) { + do { + let result = try await withDeadline { + try await model.createDepositGroup(paytoUri, + scope: scope, + amount: amountToTransfer) + } + guard let result else { + symLog.log("createDepositGroup did not answer in " + + "\(Self.depositDeadlineS)s") + depositError = WalletBackendError.deserializationError + depositStarted = false + return + } symLog.log(result.transactionId) txId = result.transactionId - } else { + } catch is CancellationError { + // Abort. The user asked to stop waiting. + depositStarted = false + } catch { + symLog.log("createDepositGroup failed: \(error)") + depositError = error depositStarted = false } } } } + /// `work()`, or nil if it has not answered within the deadline. + /// + /// A race rather than a timeout on the request, because the deadline + /// this is fixing is missing from the whole channel and a per-request + /// budget cannot be one number -- a withdrawal's own long poll is 30 s + /// by design. Deciding that properly belongs in + /// `sendFormattedRequest`; this bounds the one screen that removes its + /// own exits while it waits. + private func withDeadline( + _ work: @escaping @Sendable () async throws -> DepositGroupResult + ) async throws -> DepositGroupResult? { + try await withThrowingTaskGroup(of: DepositGroupResult?.self) { group in + group.addTask { try await work() } + group.addTask { + try? await Task.sleep(nanoseconds: + Self.depositDeadlineS * 1_000_000_000) + return nil + } + let first = try await group.next() ?? nil + group.cancelAll() + return first + } + } + var body: some View { #if PRINT_CHANGES let _ = Self._printChanges() @@ -128,6 +199,9 @@ struct DepositAmountAction: View { showDone: .prominent, // TODO: isKYC ? .bordered : .prominent url: nil, withActions: false) + } else if let depositError { + ErrorView(stack.push(), error: depositError, + devMode: developerMode) { self.depositError = nil } } else if depositStarted { let message = String(localized: "Depositing...", comment: "loading") LoadingView(stack: stack.push(), scopeInfo: nil, message: message) @@ -135,7 +209,13 @@ struct DepositAmountAction: View { .interactiveDismissDisabled() // can only use "Abort" button to dismiss .safeAreaInset(edge: .bottom) { let buttonTitle = String(localized: "Abort", comment: "deposit") - Button(buttonTitle) { dismissTop(stack.push()) } + Button(buttonTitle) { + // Cancel, then leave. `dismissTop` alone hid the + // screen and left the task running (11983). + depositTask?.cancel() + depositStarted = false + dismissTop(stack.push()) + } .buttonStyle(TalerButtonStyle(type: .prominent)) .padding(.horizontal) }