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