Bug 1382329: Part 4 - Hold mMonitor while accessing scripts in the write thread. r?erahm
MozReview-Commit-ID: 66se8G27sqQ
--- a/js/xpconnect/loader/ScriptPreloader.cpp
+++ b/js/xpconnect/loader/ScriptPreloader.cpp
@@ -267,20 +267,20 @@ ScriptPreloader::Cleanup()
}
// Wait for any pending parses to finish before clearing the mScripts
// hashtable, since the parse tasks depend on memory allocated by those
// scripts.
{
MonitorAutoLock mal(mMonitor);
FinishPendingParses(mal);
+
+ mScripts.Clear();
}
- mScripts.Clear();
-
AutoSafeJSAPI jsapi;
JS_RemoveExtraGCRootsTracer(jsapi.cx(), TraceOp, this);
UnregisterWeakMemoryReporter(this);
}
void
ScriptPreloader::InvalidateCache()
@@ -631,16 +631,21 @@ ScriptPreloader::WriteCache()
if (exists) {
NS_TRY(cacheFile->Remove(false));
}
{
AutoFDClose fd;
NS_TRY(cacheFile->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE, 0644, &fd.rwget()));
+ // We also need to hold mMonitor while we're touching scripts in
+ // mScripts, or they may be freed before we're done with them.
+ mMonitor.AssertNotCurrentThreadOwns();
+ MonitorAutoLock mal(mMonitor);
+
nsTArray<CachedScript*> scripts;
for (auto& script : IterHash(mScripts, Match<ScriptStatus::Saved>())) {
scripts.AppendElement(script);
}
// Sort scripts by load time, with async loaded scripts before sync scripts.
// Since async scripts are always loaded immediately at startup, it helps to
// have them stored contiguously.