Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawning browser with detached = false #5729

Closed
KishanBagaria opened this issue Apr 23, 2020 · 0 comments · Fixed by #6011
Closed

Spawning browser with detached = false #5729

KishanBagaria opened this issue Apr 23, 2020 · 0 comments · Fixed by #6011

Comments

@KishanBagaria
Copy link

KishanBagaria commented Apr 23, 2020

Is it possible to always launch the child process with detached = false?

this.proc = childProcess.spawn(
this._executablePath,
this._processArguments,
{
// On non-windows platforms, `detached: true` makes child process a leader of a new
// process group, making it possible to kill child process tree with `.kill(-pid)` command.
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
detached: process.platform !== 'win32',
env,
stdio
}
);

In some instances, puppeteer browsers linger even after the parent process is closed/killed.

This can be reproduced by using a worker thread.

p1.js:

const { Worker, isMainThread } = require('worker_threads')

if (isMainThread) {
  new Worker(__filename)
  console.log('main thread')
} else {
  require('./p2')
}

p2.js:

const puppeteer = require('puppeteer')

console.log('worker thread')

puppeteer.launch({
  headless: false,
  executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
})

Run node p1, once the browser opens, hit Ctrl+C, you'll notice the node process has exited but the browser still lingers.

I'm working around this by sending a cleanup signal from the main thread to the worker thread and manually closing the puppeteer instance in the worker thread. This isn't ideal and has been error prone.

This might help solve #1825

mathiasbynens pushed a commit that referenced this issue Jun 15, 2020
Fix child process killing when the parent process SIGINTs.

If you `ctrl + c` the Puppeteer parent process, we would sometimes not properly handle killing of the child processes. This would then leave child processes behind, with running Chromium instances. This in turn could block Puppeteer from launching again and results in
cryptic errors.

Instead of using the generic `process.kill` with the process id (which for some reason is negative the pid, which I don't get), we can kill the child process directly by calling `proc.kill`.

Fixes #5729.
Fixes #4796.
Fixes #4963.
Fixes #4333.
Fixes #1825.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant