Skip to content

Commit

Permalink
Merge 1b7c884 into a55d2ee
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jun 9, 2022
2 parents a55d2ee + 1b7c884 commit 6ce0fce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/browserless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@browserless/pdf": "^9.3.19",
"@browserless/screenshot": "^9.3.19",
"debug-logfmt": "~1.0.4",
"kill-process-group": "~1.0.0",
"mutexify": "~1.4.0",
"p-reflect": "~2.1.0",
"p-retry": "~4.6.1",
Expand Down
36 changes: 16 additions & 20 deletions packages/browserless/src/driver.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'

const killProcessGroup = require('kill-process-group')
const debug = require('debug-logfmt')('browserless')
const requireOneOf = require('require-one-of')
const pReflect = require('p-reflect')
const { promisify } = require('util')

const exec = promisify(require('child_process').exec)

// flags explained: https://peter.sh/experiments/chromium-command-line-switches
// default flags: https://github.com/puppeteer/puppeteer/blob/edb01972b9606d8b05b979a588eda0d622315981/src/node/Launcher.ts#L183
Expand Down Expand Up @@ -51,31 +49,29 @@ const spawn = ({
...launchOpts
} = {}) => puppeteer[mode]({ ignoreHTTPSErrors: true, args, ...launchOpts })

const getPid = childProcess => {
if (!childProcess) return null
if (childProcess.pid) return childProcess.pid
const browserProcess = childProcess.process ? childProcess.process() : undefined
if (!browserProcess) return null
return browserProcess.pid
const getProcess = subprocess => {
if (!subprocess) return
if ('process' in subprocess) return subprocess.process()
if ('pid' in subprocess) return subprocess
}

const killProcesssGroupPID = (pid, signal) =>
process.platform === 'win32'
? exec(`taskkill /pid ${this.proc.pid} /T /F`)
: Promise.resolve(process.kill(-pid, signal))
const getPid = input => {
const subprocess = getProcess(input)
return subprocess ? subprocess.pid : undefined
}

const close = async (childProcess, { signal = 'SIGKILL', ...debugOpts } = {}) => {
const pid = getPid(childProcess)
if (!pid) return
const close = async (input, { signal = 'SIGKILL', ...debugOpts } = {}) => {
const subprocess = getProcess(input)
if (!subprocess) return

// It's necessary to call `browser.close` for removing temporal files associated
// and remove listeners attached to the main process; check
// - https://github.com/puppeteer/puppeteer/blob/778ac92469d66c542c3c12fe0aa23703dd6315c2/src/node/BrowserRunner.ts#L146
// - https://github.com/puppeteer/puppeteer/blob/69d85e874416d62de6e821bef30e5cebcfd42f15/src/node/BrowserRunner.ts#L189
await pReflect(childProcess.close ? childProcess.close() : killProcesssGroupPID(pid, signal))
await pReflect('close' in subprocess ? subprocess.close() : killProcessGroup(subprocess, signal))

debug('close', { pid, signal, ...debugOpts })
return { pid }
debug('close', { pid: subprocess.pid, signal, ...debugOpts })
return { pid: subprocess.pid }
}

module.exports = { spawn, getPid, close, defaultArgs }
module.exports = { spawn, getPid, getProcess, close, defaultArgs }

0 comments on commit 6ce0fce

Please sign in to comment.