From c46c41d89caefd1e1951055fb041521a2bcc933b Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 29 Sep 2017 12:21:24 -0700 Subject: [PATCH] fix(Launcher): Launcher should handle chrome process errors (#863) Fixes #750 --- lib/Launcher.js | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/Launcher.js b/lib/Launcher.js index a2a326bd..aacdbe07 100644 --- a/lib/Launcher.js +++ b/lib/Launcher.js @@ -116,22 +116,26 @@ class Launcher { */ function killChrome() { helper.removeEventListeners(listeners); + if (chromeProcess.pid) { + if (temporaryUserDataDir) { + // Force kill chrome. + if (process.platform === 'win32') + childProcess.execSync(`taskkill /pid ${chromeProcess.pid} /T /F`); + else + process.kill(-chromeProcess.pid, 'SIGKILL'); + } else { + // Terminate chrome gracefully. + if (process.platform === 'win32') + childProcess.execSync(`taskkill /pid ${chromeProcess.pid}`); + else + process.kill(-chromeProcess.pid, 'SIGTERM'); + } + } if (temporaryUserDataDir) { - // Force kill chrome. - if (process.platform === 'win32') - childProcess.execSync(`taskkill /pid ${chromeProcess.pid} /T /F`); - else - process.kill(-chromeProcess.pid, 'SIGKILL'); // Attempt to remove temporary profile directory to avoid littering. try { removeSync(temporaryUserDataDir); } catch (e) { } - } else { - // Terminate chrome gracefully. - if (process.platform === 'win32') - childProcess.execSync(`taskkill /pid ${chromeProcess.pid}`); - else - process.kill(-chromeProcess.pid, 'SIGTERM'); } return waitForChromeToClose; } @@ -166,15 +170,19 @@ function waitForWSEndpoint(chromeProcess, timeout) { let stderr = ''; const listeners = [ helper.addEventListener(rl, 'line', onLine), - helper.addEventListener(rl, 'close', onClose), - helper.addEventListener(chromeProcess, 'exit', onClose) + helper.addEventListener(rl, 'close', () => onClose()), + helper.addEventListener(chromeProcess, 'exit', () => onClose()), + helper.addEventListener(chromeProcess, 'error', error => onClose(error)) ]; const timeoutId = timeout ? setTimeout(onTimeout, timeout) : 0; - function onClose() { + /** + * @param {!Error=} error + */ + function onClose(error) { cleanup(); reject(new Error([ - 'Failed to launch chrome!', + 'Failed to launch chrome!' + (error ? ' ' + error.message : ''), stderr, '', 'TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md',