1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 01:27:12 +02:00

Fix race condition when saving cache registry

Related to new cache storage code.
This commit is contained in:
Raymond Hill 2024-02-27 18:41:10 -05:00
parent e8b7037ba8
commit b0fc5d3d21
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 23 additions and 21 deletions

View File

@ -696,15 +696,14 @@ function getAssetCacheRegistry() {
const saveAssetCacheRegistry = (( ) => {
const save = ( ) => {
timer.off();
cacheStorage.set({ assetCacheRegistry });
return cacheStorage.set({ assetCacheRegistry });
};
const timer = vAPI.defer.create(save);
return function(lazily) {
if ( lazily ) {
timer.offon({ sec: 30 });
} else {
save();
return (throttle = 0) => {
if ( throttle === 0 ) {
return save();
}
timer.offon({ sec: throttle });
};
})();
@ -741,7 +740,7 @@ async function assetCacheRead(assetKey, updateReadTime = false) {
entry.readTime = Date.now();
if ( updateReadTime ) {
saveAssetCacheRegistry(true);
saveAssetCacheRegistry(23);
}
return reportBack(bin[internalKey]);
@ -752,18 +751,20 @@ async function assetCacheWrite(assetKey, content, options = {}) {
return assetCacheRemove(assetKey);
}
const { resourceTime, url } = options;
const cacheDict = await getAssetCacheRegistry();
getAssetCacheRegistry().then(cacheDict => {
const entry = cacheDict[assetKey] || {};
cacheDict[assetKey] = entry;
entry.writeTime = entry.readTime = Date.now();
entry.resourceTime = resourceTime || 0;
if ( typeof url === 'string' ) {
entry.remoteURL = url;
}
cacheStorage.set({ assetCacheRegistry, [`cache/${assetKey}`]: content });
});
const { resourceTime, url } = options;
const entry = cacheDict[assetKey] || {};
entry.writeTime = entry.readTime = Date.now();
entry.resourceTime = resourceTime || 0;
if ( typeof url === 'string' ) {
entry.remoteURL = url;
}
cacheDict[assetKey] = entry;
await cacheStorage.set({ [`cache/${assetKey}`]: content });
saveAssetCacheRegistry(3);
const result = { assetKey, content };
// https://github.com/uBlockOrigin/uBlock-issues/issues/248

View File

@ -87,7 +87,7 @@ const cacheStorage = (( ) => {
data.length >= µbhs.cacheStorageCompressionThreshold;
const after = await scuo.serializeAsync(data, {
compress: isLarge && µbhs.cacheStorageCompression,
multithreaded: isLarge && µbhs.cacheStorageMultithread || 0,
multithreaded: isLarge && µbhs.cacheStorageMultithread || 2,
});
return { key, data: after };
};
@ -98,7 +98,7 @@ const cacheStorage = (( ) => {
const µbhs = µb.hiddenSettings;
const isLarge = data.length >= µbhs.cacheStorageCompressionThreshold;
bin[key] = await scuo.deserializeAsync(data, {
multithreaded: isLarge && µbhs.cacheStorageMultithread || 0,
multithreaded: isLarge && µbhs.cacheStorageMultithread || 2,
});
};

View File

@ -1300,6 +1300,7 @@ onBroadcast(msg => {
catch (reason) {
ubolog(reason);
}
ubolog('Selfie not available');
destroy();
return false;
};
@ -1308,7 +1309,7 @@ onBroadcast(msg => {
if ( µb.selfieIsInvalid === false ) {
io.remove(/^selfie\//, options);
µb.selfieIsInvalid = true;
ubolog(`Selfie was removed`);
ubolog('Selfie marked for invalidation');
}
if ( µb.wakeupReason === 'createSelfie' ) {
µb.wakeupReason = '';