Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(driver): checkForQuiet bugfix to properly handle setTimeout() #16155

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions core/gather/driver/wait-for-condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,32 +225,41 @@ function waitForCPUIdle(session, waitForCPUQuiet) {
};
}

/** @type {NodeJS.Timeout|undefined} */
let lastTimeout;
let canceled = false;

/**
* @param {ExecutionContext} executionContext
* @param {() => void} resolve
* @return {Promise<void>}
*/
async function checkForQuiet(executionContext, resolve) {
if (canceled) return;
const timeSinceLongTask =
await executionContext.evaluate(
checkTimeSinceLastLongTaskInPage, {args: [], useIsolation: true});
if (canceled) return;

if (typeof timeSinceLongTask === 'number') {
if (timeSinceLongTask >= waitForCPUQuiet) {
log.verbose('waitFor', `CPU has been idle for ${timeSinceLongTask} ms`);
resolve();
} else {
log.verbose('waitFor', `CPU has been idle for ${timeSinceLongTask} ms`);
const timeToWait = waitForCPUQuiet - timeSinceLongTask;
lastTimeout = setTimeout(() => checkForQuiet(executionContext, resolve), timeToWait);
}
function checkForQuiet(executionContext) {
if (canceled) {
return Promise.resolve();
}

return executionContext.evaluate(
checkTimeSinceLastLongTaskInPage, {args: [], useIsolation: true})
.then((timeSinceLongTask) => {
if (canceled) {
return;
}

if (typeof timeSinceLongTask === 'number') {
if (timeSinceLongTask >= waitForCPUQuiet) {
log.verbose('waitFor', `CPU has been idle for ${timeSinceLongTask} ms`);
return;
} else {
log.verbose('waitFor', `CPU has been idle for ${timeSinceLongTask} ms`);
const timeToWait = waitForCPUQuiet - timeSinceLongTask;
return new Promise((resolve, reject) => {
setTimeout(() => {
checkForQuiet(executionContext)
.then(resolve)
.catch(reject);
}, timeToWait);
});
}
}
});
}

/** @type {(() => void)} */
Expand All @@ -262,12 +271,12 @@ function waitForCPUIdle(session, waitForCPUQuiet) {
/** @type {Promise<void>} */
const promise = new Promise((resolve, reject) => {
executionContext.evaluate(registerPerformanceObserverInPage, {args: [], useIsolation: true})
.then(() => checkForQuiet(executionContext, resolve))
.then(() => checkForQuiet(executionContext))
.then(resolve)
.catch(reject);
cancel = () => {
if (canceled) return;
canceled = true;
if (lastTimeout) clearTimeout(lastTimeout);
reject(new Error('Wait for CPU idle canceled'));
};
});
Expand Down