From e8e4a1ac74e8425d576063c5bf254396c7ed007b Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 4 Dec 2020 06:17:18 -0500 Subject: [PATCH] Wait for removal of storage entries to be completed Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/1365 When compiled data format changes, do not rely on order of operations at launch to assume deletion of storage occurs before attempts to access it. It's unclear this commit will fix the reported issue, as I could not reproduce it except when outright commenting out the code to prevent the storage deletion from occurring. --- src/js/assets.js | 13 +++++++------ src/js/start.js | 15 ++++++++++----- src/js/storage.js | 18 +++++------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/js/assets.js b/src/js/assets.js index 94fc4ce35..310c6ef77 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -603,14 +603,15 @@ const assetCacheRemove = async function(pattern) { delete cacheDict[assetKey]; } if ( removedContent.length !== 0 ) { - µBlock.cacheStorage.remove(removedContent); - µBlock.cacheStorage.set({ assetCacheRegistry }); + await Promise.all([ + µBlock.cacheStorage.remove(removedContent), + µBlock.cacheStorage.set({ assetCacheRegistry }), + ]); } for ( let i = 0; i < removedEntries.length; i++ ) { - fireNotification( - 'after-asset-updated', - { assetKey: removedEntries[i] } - ); + fireNotification('after-asset-updated', { + assetKey: removedEntries[i] + }); } }; diff --git a/src/js/start.js b/src/js/start.js index b4ee201db..a1298677a 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -185,10 +185,12 @@ const onUserSettingsReady = function(fetched) { // https://bugzilla.mozilla.org/show_bug.cgi?id=1588916 // Save magic format numbers into the cache storage itself. +// https://github.com/uBlockOrigin/uBlock-issues/issues/1365 +// Wait for removal of invalid cached data to be completed. -const onCacheSettingsReady = function(fetched) { +const onCacheSettingsReady = async function(fetched) { if ( fetched.compiledMagic !== µb.systemSettings.compiledMagic ) { - µb.assets.remove(/^compiled\//); + await µb.assets.remove(/^compiled\//); µb.compiledFormatChanged = true; µb.selfieIsInvalid = true; } @@ -292,15 +294,18 @@ try { ); log.info(`Backend storage for cache will be ${cacheBackend}`); + // https://github.com/uBlockOrigin/uBlock-issues/issues/1365 + // Wait for onCacheSettingsReady() to be fully ready. await Promise.all([ µb.loadSelectedFilterLists().then(( ) => { log.info(`List selection ready ${Date.now()-vAPI.T0} ms after launch`); }), µb.cacheStorage.get( { compiledMagic: 0, selfieMagic: 0 } - ).then(fetched => { - log.info(`Cache magic numbers ready ${Date.now()-vAPI.T0} ms after launch`); - onCacheSettingsReady(fetched); + ).then(fetched => + onCacheSettingsReady(fetched) + ).then(( ) => { + log.info(`Integrity of cached data processed ${Date.now()-vAPI.T0} ms after launch`); }), vAPI.storage.get(createDefaultProps()).then(fetched => { log.info(`First fetch ready ${Date.now()-vAPI.T0} ms after launch`); diff --git a/src/js/storage.js b/src/js/storage.js index 56d529f7b..3ff2359bb 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -737,7 +737,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { this.compiledFormatChanged === false && this.badLists.has(assetKey) === false ) { - let compiledDetails = await this.assets.get(compiledPath); + const compiledDetails = await this.assets.get(compiledPath); if ( compiledDetails.content !== '' ) { compiledDetails.assetKey = assetKey; return compiledDetails; @@ -763,19 +763,11 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { return { assetKey, content: '' }; } - // Fetching the raw content may cause the compiled content to be - // generated somewhere else in uBO, hence we try one last time to - // fetch the compiled content in case it has become available. - const compiledDetails = await this.assets.get(compiledPath); - if ( compiledDetails.content === '' ) { - compiledDetails.content = this.compileFilters(rawDetails.content, { - assetKey - }); - this.assets.put(compiledPath, compiledDetails.content); - } + const compiledContent = + this.compileFilters(rawDetails.content, { assetKey }); + this.assets.put(compiledPath, compiledContent); - compiledDetails.assetKey = assetKey; - return compiledDetails; + return { assetKey, content: compiledContent }; }; /******************************************************************************/