Skip to content

Commit

Permalink
cli: stop treating chrome launcher kill() as async (#15998)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored May 14, 2024
1 parent 171ebdf commit 74294b1
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 45 deletions.
43 changes: 5 additions & 38 deletions cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import path from 'path';
import os from 'os';

import psList from 'ps-list';
import * as ChromeLauncher from 'chrome-launcher';
import yargsParser from 'yargs-parser';
import log from 'lighthouse-logger';
Expand Down Expand Up @@ -176,36 +175,6 @@ async function saveResults(runnerResult, flags) {
}
}

/**
* Attempt to kill the launched Chrome, if defined.
* @param {ChromeLauncher.LaunchedChrome=} launchedChrome
* @return {Promise<void>}
*/
async function potentiallyKillChrome(launchedChrome) {
if (!launchedChrome) return;

/** @type {NodeJS.Timeout} */
let timeout;
const timeoutPromise = new Promise((_, reject) => {
timeout = setTimeout(reject, 5000, new Error('Timed out waiting to kill Chrome'));
});

return Promise.race([
launchedChrome.kill(),
timeoutPromise,
]).catch(async err => {
const runningProcesses = await psList();
if (!runningProcesses.some(proc => proc.pid === launchedChrome.pid)) {
log.warn('CLI', 'Warning: Chrome process could not be killed because it already exited.');
return;
}

throw new Error(`Couldn't quit Chrome process. ${err}`);
}).finally(() => {
clearTimeout(timeout);
});
}

/**
* @param {string} url
* @param {LH.CliFlags} flags
Expand All @@ -214,12 +183,10 @@ async function potentiallyKillChrome(launchedChrome) {
*/
async function runLighthouse(url, flags, config) {
/** @param {any} reason */
async function handleTheUnhandled(reason) {
function handleTheUnhandled(reason) {
process.stderr.write(`Unhandled Rejection. Reason: ${reason}\n`);
await potentiallyKillChrome(launchedChrome).catch(() => {});
setTimeout(_ => {
process.exit(1);
}, 100);
launchedChrome?.kill();
process.exit(1);
}
process.on('unhandledRejection', handleTheUnhandled);

Expand Down Expand Up @@ -247,7 +214,7 @@ async function runLighthouse(url, flags, config) {
await saveResults(runnerResult, flags);
}

await potentiallyKillChrome(launchedChrome);
launchedChrome?.kill();
process.removeListener('unhandledRejection', handleTheUnhandled);

// Runtime errors indicate something was *very* wrong with the page result.
Expand All @@ -265,7 +232,7 @@ async function runLighthouse(url, flags, config) {

return runnerResult;
} catch (err) {
await potentiallyKillChrome(launchedChrome).catch(() => {});
launchedChrome?.kill();
return printErrorAndExit(err);
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/test/smokehouse/lighthouse-runners/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async function runBundledLighthouse(url, config, testRunnerOptions) {
};
} finally {
// Clean up and return results.
await launchedChrome.kill();
launchedChrome.kill();
}
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@
"metaviewport-parser": "0.3.0",
"open": "^8.4.0",
"parse-cache-control": "1.0.1",
"ps-list": "^8.0.0",
"puppeteer-core": "^22.6.5",
"robots-parser": "^3.0.1",
"semver": "^5.3.0",
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6017,11 +6017,6 @@ proxy-from-env@^1.1.0:
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

ps-list@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/ps-list/-/ps-list-8.0.0.tgz#ee622c04a16ac0b29be65d7d920c1fb36c8383d4"
integrity sha512-rBBL6jp5Ccc5fy7p1Os5wDkXlJm5PeLCP2mVz1wLqEtKGo+d19oDApqVbePJoOxNlWeaxJxe28qahc3KWj9ePg==

psl@^1.1.28:
version "1.8.0"
resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24"
Expand Down

0 comments on commit 74294b1

Please sign in to comment.