Intermittent issue when loading HUD tools: "cannot read property 'data' of null"
See original GitHub issueHi All
I faced an intermittent issue when doing the following:
- Enable HUD via icon in toolbar
- Open configured browser (chrome in my case) by clicking on icon in toolbar
Sometimes it worked as expected and HUD is displayed, other times it would spike my CPU to 100% and chrome would freeze. Subsequent investigation showed literally thousands of errors being set in the chrome DevTools and ZAP console output, like so:
Some investigation revealed that basically the “data” of “null” came from the “loadTool” function in utils.js… the tools were not being saved in indexedDB.
Why that is turns out to be really subtle… if you check the serviceworker.js file, we used a “forEach” loop to in turn call importScripts
on each default tool:
var toolScripts = [
<<ZAP_HUD_TOOLS>>
];
// Load Tool Scripts
localforage.setItem("tools", [])
.then(() => {
toolScripts.forEach(script => { // <--------- here
importScripts(script);
});
})
What I think the issue is, is that forEach actually calls an async function for each loop. In other words, every “importScripts” command in that forEach loop was being processed async, so the promise would return before the importScripts were all processed. This and then causes the null errors.
Turning this into plain vanilla for loop like so seems to sort out the issue on my PC, but i’ll test some more and update this issue (which is serving as my braindump) to make sure it’s not a red herring.
var toolScripts = [
<<ZAP_HUD_TOOLS>>
];
localforage.setItem("tools", [])
.then(() => {
for (var toolScriptCounter = 0; toolScriptCounter < toolScripts.length; toolScriptCounter++){
var script = toolScripts[toolScriptCounter];
importScripts(script);
}
})
Other solutions (if this is really the root cause) would be to use async/await in conjunction with the forEach loop
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (6 by maintainers)
Top GitHub Comments
Hey @dvas0004 just catching up on this thread. Absolutely love all your details and notes! Thank you so much for including all of that, its super helpful.
I am a little confused about what would be async in this for loop: https://github.com/psiinon/zap-hud/blob/master/src/org/zaproxy/zap/extension/hud/files/hud/serviceworker.js#L46
Isn’t
forEach
synchronous as well asimportScripts
?forEach
:importScripts
:There are some major hacks/ ugly-ness in startup right now though. Trying to figure out why this bug would occur but not sure yet. I bet you’re right there is some async happening somewhere though…
yes absolutely. I had literally just installed it, and it was on my first run of the HUD, no other tabs and browser was launched through ZAP, not “remotely”
On Wed, 17 Oct 2018 at 09:08, David Scrobonia notifications@github.com wrote: