mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix(Launcher): consume protocol errors when initiating browser.close() (#2332)
Handle errors properly while sending Browser.close() Fixes #1429.
This commit is contained in:
@@ -22,7 +22,7 @@ const {Connection} = require('./Connection');
|
||||
const Browser = require('./Browser');
|
||||
const readline = require('readline');
|
||||
const fs = require('fs');
|
||||
const {helper} = require('./helper');
|
||||
const {helper, debugError} = require('./helper');
|
||||
const ChromiumRevision = require(path.join(helper.projectRoot(), 'package.json')).puppeteer.chromium_revision;
|
||||
|
||||
const mkdtempAsync = helper.promisify(fs.mkdtemp);
|
||||
@@ -140,13 +140,13 @@ class Launcher {
|
||||
});
|
||||
});
|
||||
|
||||
const listeners = [ helper.addEventListener(process, 'exit', forceKillChrome) ];
|
||||
const listeners = [ helper.addEventListener(process, 'exit', killChrome) ];
|
||||
if (options.handleSIGINT !== false)
|
||||
listeners.push(helper.addEventListener(process, 'SIGINT', forceKillChrome));
|
||||
listeners.push(helper.addEventListener(process, 'SIGINT', killChrome));
|
||||
if (options.handleSIGTERM !== false)
|
||||
listeners.push(helper.addEventListener(process, 'SIGTERM', killChrome));
|
||||
listeners.push(helper.addEventListener(process, 'SIGTERM', gracefullyCloseChrome));
|
||||
if (options.handleSIGHUP !== false)
|
||||
listeners.push(helper.addEventListener(process, 'SIGHUP', killChrome));
|
||||
listeners.push(helper.addEventListener(process, 'SIGHUP', gracefullyCloseChrome));
|
||||
/** @type {?Connection} */
|
||||
let connection = null;
|
||||
try {
|
||||
@@ -158,27 +158,31 @@ class Launcher {
|
||||
} else {
|
||||
connection = Connection.createForPipe(/** @type {!NodeJS.WritableStream} */(chromeProcess.stdio[3]), /** @type {!NodeJS.ReadableStream} */ (chromeProcess.stdio[4]), connectionDelay);
|
||||
}
|
||||
return Browser.create(connection, options, chromeProcess, killChrome);
|
||||
return Browser.create(connection, options, chromeProcess, gracefullyCloseChrome);
|
||||
} catch (e) {
|
||||
forceKillChrome();
|
||||
killChrome();
|
||||
throw e;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {Promise}
|
||||
*/
|
||||
function killChrome() {
|
||||
function gracefullyCloseChrome() {
|
||||
helper.removeEventListeners(listeners);
|
||||
if (temporaryUserDataDir) {
|
||||
forceKillChrome();
|
||||
killChrome();
|
||||
} else if (connection) {
|
||||
// Attempt to close chrome gracefully
|
||||
connection.send('Browser.close');
|
||||
connection.send('Browser.close').catch(error => {
|
||||
debugError(error);
|
||||
killChrome();
|
||||
});
|
||||
}
|
||||
return waitForChromeToClose;
|
||||
}
|
||||
|
||||
function forceKillChrome() {
|
||||
// This method has to be sync to be used as 'exit' event handler.
|
||||
function killChrome() {
|
||||
helper.removeEventListeners(listeners);
|
||||
if (chromeProcess.pid && !chromeProcess.killed && !chromeClosed) {
|
||||
// Force kill chrome.
|
||||
@@ -221,7 +225,7 @@ class Launcher {
|
||||
static async connect(options = {}) {
|
||||
const connectionDelay = options.slowMo || 0;
|
||||
const connection = await Connection.createForWebSocket(options.browserWSEndpoint, connectionDelay);
|
||||
return Browser.create(connection, options, null, () => connection.send('Browser.close'));
|
||||
return Browser.create(connection, options, null, () => connection.send('Browser.close').catch(debugError));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user