Skip to content

Commit

Permalink
core: disconnect Puppeteer when started by Lighthouse (#14770)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Feb 9, 2023
1 parent efacda0 commit 89d9fb5
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions core/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,17 @@ async function _navigations(args) {
}

/**
* @param {{requestedUrl?: string, driver: Driver, resolvedConfig: LH.Config.ResolvedConfig}} args
* @param {{requestedUrl?: string, driver: Driver, resolvedConfig: LH.Config.ResolvedConfig, lhBrowser?: LH.Puppeteer.Browser, lhPage?: LH.Puppeteer.Page}} args
*/
async function _cleanup({requestedUrl, driver, resolvedConfig}) {
async function _cleanup({requestedUrl, driver, resolvedConfig, lhBrowser, lhPage}) {
const didResetStorage = !resolvedConfig.settings.disableStorageReset && requestedUrl;
if (didResetStorage) await storage.clearDataForOrigin(driver.defaultSession, requestedUrl);

await driver.disconnect();

// If Lighthouse started the Puppeteer instance then we are responsible for closing it.
await lhPage?.close();
await lhBrowser?.disconnect();
}

/**
Expand All @@ -338,17 +342,25 @@ async function navigationGather(page, requestor, options = {}) {
async () => {
const normalizedRequestor = isCallback ? requestor : UrlUtils.normalizeUrl(requestor);

/** @type {LH.Puppeteer.Browser|undefined} */
let lhBrowser = undefined;
/** @type {LH.Puppeteer.Page|undefined} */
let lhPage = undefined;

// For navigation mode, we shouldn't connect to a browser in audit mode,
// therefore we connect to the browser in the gatherFn callback.
if (!page) {
const {hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT} = flags;
const browser = await puppeteer.connect({browserURL: `http://${hostname}:${port}`});
page = await browser.newPage();
lhBrowser = await puppeteer.connect({browserURL: `http://${hostname}:${port}`});
lhPage = await lhBrowser.newPage();
page = lhPage;
}

const driver = new Driver(page);
const context = {
driver,
lhBrowser,
lhPage,
resolvedConfig,
requestor: normalizedRequestor,
};
Expand Down

0 comments on commit 89d9fb5

Please sign in to comment.