mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
[api] Launcher: Close gracefully when a userDataDir is specified (#700)
This patch: - makes `browser.close()` return a promise that resolves when browser gets closed - starts closing chrome gracefully if a custom `userDataDir` is supplied Fixes #527
This commit is contained in:
committed by
Andrey Lushnikov
parent
d7e673645a
commit
f398e69dbb
@@ -84,9 +84,14 @@ class Launcher {
|
||||
chromeProcess.stderr.pipe(process.stderr);
|
||||
}
|
||||
|
||||
// Cleanup as processes exit.
|
||||
if (temporaryUserDataDir)
|
||||
chromeProcess.once('close', () => removeSync(temporaryUserDataDir));
|
||||
const waitForChromeToClose = new Promise(fulfill => {
|
||||
chromeProcess.once('close', () => {
|
||||
// Cleanup as processes exit.
|
||||
if (temporaryUserDataDir)
|
||||
removeSync(temporaryUserDataDir);
|
||||
fulfill();
|
||||
});
|
||||
});
|
||||
|
||||
const listeners = [ helper.addEventListener(process, 'exit', killChrome) ];
|
||||
if (options.handleSIGINT !== false)
|
||||
@@ -102,12 +107,29 @@ class Launcher {
|
||||
throw e;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {Promise}
|
||||
*/
|
||||
function killChrome() {
|
||||
helper.removeEventListeners(listeners);
|
||||
if (process.platform === 'win32')
|
||||
childProcess.execSync(`taskkill /pid ${chromeProcess.pid} /T /F`);
|
||||
else
|
||||
process.kill(-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');
|
||||
// Attempt to remove temporary profile directory to avoid littering.
|
||||
try {
|
||||
removeSync(temporaryUserDataDir);
|
||||
} catch (e) { }
|
||||
} else {
|
||||
// Terminate chrome gracefully.
|
||||
if (process.platform === 'win32')
|
||||
chromeProcess.kill();
|
||||
else
|
||||
process.kill(-chromeProcess.pid, 'SIGTERM');
|
||||
}
|
||||
return waitForChromeToClose;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user