[BUG] GM_setValue does not work properly with a modest volume of data.
See original GitHub issueDescription The function GM_setValue does not working when storing large amounts of data across different variables. If the script stores data using GM_setValue, the function seems to work as expected. However, after reloading the page and trying to retrieve the data previously written, it returns undefined, just as if the item has not been stored previously.
Steps to reproduce
I’ve attached here a small userscripts that shows the bug. The example webpage is also included: WEBPAGE:
<!DOCTYPE html>
<html lang="en">
<body>
<div id="main">
<button id="b1">Read</button>
<button id="b2">Create</button>
</div>
</body>
</html>
USERSCRIPT:
// ==UserScript==
// @name New script - localhost:44444
// @namespace Violentmonkey Scripts
// @match http://localhost:44444/empty.html
// @grant none
// @version 1.0
// @author -
// @grant GM.deleteValue
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.registerMenuCommand
// @description 19/9/2022, 20:08:06
// ==/UserScript==
function readdata()
{
;(async () => {
console.log("Results");
console.log(await GM.getValue('d1'));
console.log(await GM.getValue('d2'));
console.log(await GM.getValue('d3'));
console.log(await GM.getValue('d4'));
console.log(await GM.getValue('d5'));
console.log(await GM.getValue('d6'));
console.log(await GM.getValue('d7'));
console.log(await GM.getValue('d8'));
console.log(await GM.getValue('d9'));
})();
}
function createdata()
{
;(async () => {
var arr_temp=[];
for (i=0;i<1000000;i++)
{
arr_temp.push(i);
}
await GM.deleteValue('d1');
await GM.deleteValue('d2');
await GM.deleteValue('d3');
await GM.deleteValue('d4');
await GM.deleteValue('d5');
await GM.deleteValue('d6');
await GM.deleteValue('d7');
await GM.deleteValue('d8');
await GM.deleteValue('d9');
await GM.setValue('d1',arr_temp);
await GM.setValue('d2',arr_temp);
await GM.setValue('d3',arr_temp);
await GM.setValue('d4',arr_temp);
await GM.setValue('d5',arr_temp);
await GM.setValue('d6',arr_temp);
await GM.setValue('d7',arr_temp);
await GM.setValue('d8',arr_temp);
await GM.setValue('d9',arr_temp);
console.log("DONE");
location.reload();
})();
}
document.getElementById("b1").onclick=readdata;
document.getElementById("b2").onclick=createdata;
GM.registerMenuCommand( "read", readdata );
GM.registerMenuCommand( "create", createdata );
Steps:
- Click on the “Create” button. It will populate the d1,d2,… variables. It will also reload the page
- Click the “read” button. It will print data on the console
Expected behavior
The console prints 9 times an array with 1000000 values.
Actual behavior Sometimes, one or two of the d1,d2,… variables is undefined. Each time the steps are repeated, the undefined variables change. It reminds me of a race condition of sorts.
Environment:
- OS: Linux Ubuntu 22.04.1
- Kernel: Linux 5.15.0-47
- Browser: Firefox 104.0.2
- Violentmonkey Version: 2.13.1
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (3 by maintainers)
I’m still working on rewriting our handling of storage, it’ll take some time because we have to support both the old synchronous and the new asynchronous API and I’m trying various solutions to find the one that’s both fast and not overly complicated under the hood. Storing so much data is an edge case which doesn’t affect the majority of scripts, so maybe we should make a new release without waiting for this one, @gera2ld.
Thanks, can confirm this is fixed in v2.13.2 for
GM_setValue
(i.e. the synchronous version - the release notes make it sound like it only affected the async version 😃 🎉