Skip to content

Commit

Permalink
fix(driver): prevent PerfObserver from being garbage collected (#2682)
Browse files Browse the repository at this point in the history
* fix(driver): add interval and timeout hacks to PerfObserver

* more sensible hack

* feedback

* more feedback
  • Loading branch information
patrickhulce committed Jul 14, 2017
1 parent a6bbcab commit 36c2df5
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,11 @@ class Driver {
}

let lastTimeout;
let cancelled = false;
let isResolvable = false;
function checkForQuiet(driver, resolve) {
if (cancelled) return;

const tryLater = timeToWait => {
lastTimeout = setTimeout(() => checkForQuiet(driver, resolve), timeToWait);
};
Expand All @@ -448,6 +451,8 @@ class Driver {

return driver.evaluateAsync(`(${checkTimeSinceLastLongTask.toString()})()`)
.then(timeSinceLongTask => {
if (cancelled) return;

if (typeof timeSinceLongTask === 'number' && timeSinceLongTask >= waitForCPUQuiet) {
log.verbose('Driver', `CPU has been idle for ${timeSinceLongTask} ms`);
resolve();
Expand All @@ -462,6 +467,7 @@ class Driver {
const promise = new Promise((resolve, reject) => {
checkForQuiet(this, resolve);
cancel = () => {
cancelled = true;
if (lastTimeout) clearTimeout(lastTimeout);
reject(new Error('Wait for CPU idle cancelled'));
};
Expand Down Expand Up @@ -1107,7 +1113,12 @@ function registerPerformanceObserverInPage() {
}
}
});

observer.observe({entryTypes: ['longtask']});
// HACK: A PerformanceObserver will be GC'd if there are no more references to it, so attach it to
// window to ensure we still receive longtask notifications. See https://crbug.com/742530.
// For an example test of this behavior see https://gist.github.com/patrickhulce/69d8bed1807e762218994b121d06fea6.
window.____lhPerformanceObserver = observer;
}


Expand Down

0 comments on commit 36c2df5

Please sign in to comment.