From 1491872b626e4d674fcbd872e793e8038e1d8b91 Mon Sep 17 00:00:00 2001 From: Federico Maccaroni Date: Thu, 25 Jan 2024 17:56:28 -0300 Subject: [PATCH] PM-3350 Added check for state migration version before trying to migrate LiteDB values into Prefs when there's no need to and that may be inducing crashes on backgrounded iOS apps. --- src/Core/Constants.cs | 1 + src/Core/Services/MobileStorageService.cs | 8 ++++++++ src/Core/Services/StateMigrationService.cs | 6 ++---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Core/Constants.cs b/src/Core/Constants.cs index 44de67eb0..0b162ea03 100644 --- a/src/Core/Constants.cs +++ b/src/Core/Constants.cs @@ -74,6 +74,7 @@ namespace Bit.Core public const string DefaultFido2CredentialType = "public-key"; public const string DefaultFido2CredentialAlgorithm = "ECDSA"; public const string DefaultFido2CredentialCurve = "P-256"; + public const int LatestStateVersion = 7; public static readonly string[] AndroidAllClearCipherCacheKeys = { diff --git a/src/Core/Services/MobileStorageService.cs b/src/Core/Services/MobileStorageService.cs index bb2827bc8..48a8e7384 100644 --- a/src/Core/Services/MobileStorageService.cs +++ b/src/Core/Services/MobileStorageService.cs @@ -82,6 +82,14 @@ namespace Bit.App.Services private async Task TryMigrateLiteDbToPrefsAsync(string key) { + // Note: this is added to prevent searching a value in LiteDB when the migration has already run and it's in its latest version. + // If not, we could get several concurrent calls to the DB asking for values we already know they are not there, + // possible causing crashes on backgrounded apps. + if (await _preferencesStorageService.GetAsync(Constants.StateVersionKey) == Constants.LatestStateVersion) + { + return default; + } + var currentValue = await _liteDbStorageService.GetAsync(key); if (currentValue != null) { diff --git a/src/Core/Services/StateMigrationService.cs b/src/Core/Services/StateMigrationService.cs index 360c69e3b..56883ada2 100644 --- a/src/Core/Services/StateMigrationService.cs +++ b/src/Core/Services/StateMigrationService.cs @@ -10,8 +10,6 @@ namespace Bit.Core.Services { public class StateMigrationService : IStateMigrationService { - private const int StateVersion = 7; - private readonly DeviceType _deviceType; private readonly IStorageService _preferencesStorageService; private readonly IStorageService _liteDbStorageService; @@ -58,10 +56,10 @@ namespace Bit.Core.Services if (lastVersion == 0) { // fresh install, set current/latest version for availability going forward - lastVersion = StateVersion; + lastVersion = Constants.LatestStateVersion; await SetLastStateVersionAsync(lastVersion); } - return lastVersion < StateVersion; + return lastVersion < Constants.LatestStateVersion; } private async Task PerformMigrationAsync()