From b223f5f16e784b27435bcc0334d0604a1cf19958 Mon Sep 17 00:00:00 2001 From: Federico Maccaroni Date: Tue, 7 Jun 2022 11:46:48 -0300 Subject: [PATCH] EC-255 fix crash when scanning TOTP; BeginInvokeOnMainThread doesn't bubble up the exception, just crashes because it throws the exception to the current main thread context; so it was changed to InvokeOnMainThreadAsync which does bubble up the exception. (#1942) --- src/App/Pages/Vault/ScanPage.xaml.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/App/Pages/Vault/ScanPage.xaml.cs b/src/App/Pages/Vault/ScanPage.xaml.cs index cf7e7a68e..d0a149621 100644 --- a/src/App/Pages/Vault/ScanPage.xaml.cs +++ b/src/App/Pages/Vault/ScanPage.xaml.cs @@ -45,6 +45,8 @@ namespace Bit.App.Pages _autofocusCts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); var autofocusCts = _autofocusCts; + // this task is needed to be awaited OnDisappearing to avoid some crashes + // when changing the value of _zxing.IsScanning _continuousAutofocusTask = Task.Run(async () => { try @@ -52,7 +54,7 @@ namespace Bit.App.Pages while (!autofocusCts.IsCancellationRequested) { await Task.Delay(TimeSpan.FromSeconds(2), autofocusCts.Token); - Device.BeginInvokeOnMainThread(() => + await Device.InvokeOnMainThreadAsync(() => { if (!autofocusCts.IsCancellationRequested) { @@ -73,7 +75,10 @@ namespace Bit.App.Pages { _autofocusCts?.Cancel(); - await _continuousAutofocusTask; + if (_continuousAutofocusTask != null) + { + await _continuousAutofocusTask; + } _zxing.IsScanning = false; base.OnDisappearing();