Skip to content

Commit

Permalink
Harden compiled/selfie format change detection at launch
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#759

This commit adds code to rely less on the state of the
cache storage to decide whether filter lists should be
re-compiled or whether the selfie is currently valid
at launch time when a change in compiled/selfie format
is detected.
  • Loading branch information
gorhill committed Oct 27, 2019
1 parent b794453 commit d7b2d31
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ const µBlock = (( ) => { // jshint ignore:line
selfieMagic: 23, // Increase when selfie format changes
},

// https://github.com/uBlockOrigin/uBlock-issues/issues/759#issuecomment-546654501
// The assumption is that cache storage state reflects whether
// compiled or selfie assets are available or not. The properties
// below is to no longer rely on this assumption -- though it's still
// not clear how the assumption could be wrong, and it's still not
// clear whether relying on those properties will really solve the
// issue. It's just an attempt at hardening.
compiledFormatChanged: false,
selfieIsInvalid: false,

restoreBackupSettings: {
lastRestoreFile: '',
lastRestoreTime: 0,
Expand Down
8 changes: 4 additions & 4 deletions src/js/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ const onUserSettingsReady = function(fetched) {
// Housekeeping, as per system setting changes

const onSystemSettingsReady = function(fetched) {
let mustSaveSystemSettings = false;
if ( fetched.compiledMagic !== µb.systemSettings.compiledMagic ) {
µb.assets.remove(/^compiled\//);
mustSaveSystemSettings = true;
µb.compiledFormatChanged = true;
µb.selfieIsInvalid = true;
}
if ( fetched.selfieMagic !== µb.systemSettings.selfieMagic ) {
mustSaveSystemSettings = true;
µb.selfieIsInvalid = true;
}
if ( mustSaveSystemSettings ) {
if ( µb.selfieIsInvalid ) {
fetched.selfie = null;
µb.selfieManager.destroy();
vAPI.storage.set(µb.systemSettings);
Expand Down
18 changes: 12 additions & 6 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@

this.selfieManager.destroy();
this.lz4Codec.relinquish();
this.compiledFormatChanged = false;

loadingPromise = undefined;
};
Expand Down Expand Up @@ -712,10 +713,12 @@
µBlock.getCompiledFilterList = async function(assetKey) {
const compiledPath = 'compiled/' + assetKey;

let compiledDetails = await this.assets.get(compiledPath);
if ( compiledDetails.content !== '' ) {
compiledDetails.assetKey = assetKey;
return compiledDetails;
if ( this.compiledFormatChanged === false ) {
let compiledDetails = await this.assets.get(compiledPath);
if ( compiledDetails.content !== '' ) {
compiledDetails.assetKey = assetKey;
return compiledDetails;
}
}

const rawDetails = await this.assets.get(assetKey);
Expand All @@ -730,7 +733,7 @@
// 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.
compiledDetails = await this.assets.get(compiledPath);
let compiledDetails = await this.assets.get(compiledPath);
if ( compiledDetails.content === '' ) {
compiledDetails.content = this.compileFilters(
rawDetails.content,
Expand Down Expand Up @@ -1053,6 +1056,7 @@
),
]);
µb.lz4Codec.relinquish();
µb.selfieIsInvalid = false;
};

const loadMain = async function() {
Expand Down Expand Up @@ -1080,7 +1084,7 @@
};

const load = async function() {
if ( destroyTimer !== undefined ) {
if ( µb.selfieIsInvalid ) {
return false;
}
try {
Expand Down Expand Up @@ -1108,6 +1112,7 @@
const destroy = function() {
µb.cacheStorage.remove('selfie'); // TODO: obsolete, remove eventually.
µb.assets.remove(/^selfie\//);
µb.selfieIsInvalid = true;
createTimer = vAPI.setTimeout(( ) => {
createTimer = undefined;
create();
Expand All @@ -1127,6 +1132,7 @@
},
1019
);
µb.selfieIsInvalid = true;
};

return { load, destroy: destroyAsync };
Expand Down

0 comments on commit d7b2d31

Please sign in to comment.