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.

This commit is contained in:
Federico Maccaroni 2024-01-25 17:56:28 -03:00
parent c74636ffa5
commit 1491872b62
No known key found for this signature in database
GPG key ID: 5D233F8F2B034536
3 changed files with 11 additions and 4 deletions

View file

@ -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 =
{

View file

@ -82,6 +82,14 @@ namespace Bit.App.Services
private async Task<T> TryMigrateLiteDbToPrefsAsync<T>(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<int?>(Constants.StateVersionKey) == Constants.LatestStateVersion)
{
return default;
}
var currentValue = await _liteDbStorageService.GetAsync<T>(key);
if (currentValue != null)
{

View file

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