From 907d4fdd8e7be5bd8d07f3eced5edce8d36fef52 Mon Sep 17 00:00:00 2001 From: yushi Date: Wed, 6 Nov 2024 23:18:26 +0800 Subject: [PATCH] handle race condition when loading db --- .../app/api/ada/lib/storage/database/utils.js | 2 +- .../extension/background/state/databaseManager.js | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/yoroi-extension/app/api/ada/lib/storage/database/utils.js b/packages/yoroi-extension/app/api/ada/lib/storage/database/utils.js index 44e7dd9a5d..1fa524d3d6 100644 --- a/packages/yoroi-extension/app/api/ada/lib/storage/database/utils.js +++ b/packages/yoroi-extension/app/api/ada/lib/storage/database/utils.js @@ -193,7 +193,7 @@ export async function raii( ) ).join('\n'); // eslint-disable-next-line no-console - console.error('rolling back Lovefield query for\n' + uniqueTableNames + ' with error ' + JSON.stringify(e)); + console.error('rolling back Lovefield query for\n' + uniqueTableNames + ' with error ' + e.message); await tx.rollback(); throw e; } diff --git a/packages/yoroi-extension/chrome/extension/background/state/databaseManager.js b/packages/yoroi-extension/chrome/extension/background/state/databaseManager.js index 50ecc4b9ed..d135da3bfc 100644 --- a/packages/yoroi-extension/chrome/extension/background/state/databaseManager.js +++ b/packages/yoroi-extension/chrome/extension/background/state/databaseManager.js @@ -6,24 +6,26 @@ import { migrateNoRefresh } from '../../../../app/api/common/migration'; import LocalStorageApi from '../../../../app/api/localStorage/index'; import type { lf$Database, } from 'lovefield'; -let dbCache = null; +let loadDbPromiseCache = null; let migratePromiseCache = null; export async function getDb(): Promise { const localStorageApi = new LocalStorageApi(); - if (!dbCache) { - dbCache = await loadLovefieldDB(schema.DataStoreType.INDEXED_DB); + if (!loadDbPromiseCache) { + loadDbPromiseCache = loadLovefieldDB(schema.DataStoreType.INDEXED_DB); } + const db = await loadDbPromiseCache; + if (!migratePromiseCache) { migratePromiseCache = migrateNoRefresh({ localStorageApi, - persistentDb: dbCache, + persistentDb: db, currVersion: environment.getVersion(), }); } await migratePromiseCache; - return dbCache; + return db; }