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

[Bug] Electron process becomes unresponsive to SIGINT #467

Closed
Androbin opened this issue Mar 30, 2021 · 15 comments
Closed

[Bug] Electron process becomes unresponsive to SIGINT #467

Androbin opened this issue Mar 30, 2021 · 15 comments
Labels
issue: bug report A bug has been reported needs triage

Comments

@Androbin
Copy link

Describe the bug

Sending a SIGINT signal to the Electron process does not kill the process.

Code Snippet

package.json

{
  "name": "test",
  "main": "main.js",
  "scripts": {
    "postinstall": "electron-builder install-app-deps",
    "start": "electron ."
  },
  "dependencies": {
    "puppeteer": "^8.0.0",
    "puppeteer-extra": "^3.1.16",
    "puppeteer-extra-plugin-stealth": "^2.7.6"
  },
  "devDependencies": {
    "electron": "^12.0.2",
    "electron-builder": "^22.10.5"
  }
}

main.js

const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
puppeteer.launch().then(browser => browser.close());

Versions

  System:
    OS: Linux 5.8 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
    Memory: 227.82 MB / 15.36 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 12.20.1 - ~/.nvm/versions/node/v12.20.1/bin/node
    npm: 7.6.3 - ~/.nvm/versions/node/v12.20.1/bin/npm
  npmPackages:
    puppeteer: ^8.0.0 => 8.0.0 
    puppeteer-extra: ^3.1.18 => 3.1.18 
    puppeteer-extra-plugin-stealth: ^2.7.6 => 2.7.6
@Androbin Androbin added issue: bug report A bug has been reported needs triage labels Mar 30, 2021
@berstend
Copy link
Owner

How about your electron app listening to SIGINT and closing the browser?

@berstend
Copy link
Owner

function handle(signal) {
  console.log(`Received ${signal}`);
  browser.close()
}

process.on('SIGINT', handle);
process.on('SIGTERM', handle);

@Androbin
Copy link
Author

@berstend Funny enough, when browser.close is not called at all, there is no issue.
The issue only happens, when both puppeteer.use and browser.close are called.

@Androbin
Copy link
Author

If browser.close is not called, the process responds to SIGINT and indeed kills the browser process.

@brunogaspar
Copy link
Collaborator

Did a quick test, and can only reproduce the issue if the puppeteer.use(StealthPlugin()) is being called and when it's being called, it does indeed become unresponsive.

@berstend
Copy link
Owner

berstend commented Mar 31, 2021

Does the problem go away if the user-agent-override evasion is deleted from the enabled stealth plugins?

// Remove specific evasion from enabled ones dynamically
const pluginStealth = require('puppeteer-extra-plugin-stealth')()
pluginStealth.enabledEvasions.delete('user-agent-override')
puppeteer.use(pluginStealth)

@berstend
Copy link
Owner

berstend commented Mar 31, 2021

The reason I'm thinking it might be related to user-agent-override: It uses the puppeteer-extra-plugin-user-data-dir plugin behind the scenes, which does some clean up when the browser is closing:

  async onClose() {
    debug('onClose')
    if (this.shouldDeleteDirectory) {
      this.deleteUserDataDir()
    }
  }

https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-user-data-dir/index.js#L111-L117

Enabling DEBUG logging (DEBUG=puppeteer-extra,puppeteer-extra-plugin:* node myscript.js) could be interesting here.

@brunogaspar
Copy link
Collaborator

@berstend Yes, disabling the user data dir plugin addresses it.

@berstend
Copy link
Owner

Would be great to see some logging output, the issue might be something like this.

@brunogaspar
Copy link
Collaborator

Running node script.js works as expected, meaning, it closes the browser and ends the execution, while running npm start as in the OP, it blocks the execution.

Here's the output anyway

puppeteer-extra-plugin:base:stealth Initialized. +0ms
puppeteer-extra plugin registered stealth +0ms
puppeteer-extra dependencies missing Set(15) {
  'stealth/evasions/chrome.app',
  'stealth/evasions/chrome.csi',
  'stealth/evasions/chrome.loadTimes',
  'stealth/evasions/chrome.runtime',
  'stealth/evasions/iframe.contentWindow',
  'stealth/evasions/media.codecs',
  'stealth/evasions/navigator.hardwareConcurrency',
  'stealth/evasions/navigator.languages',
  'stealth/evasions/navigator.permissions',
  'stealth/evasions/navigator.plugins',
  'stealth/evasions/navigator.webdriver',
  'stealth/evasions/sourceurl',
  'stealth/evasions/user-agent-override',
  'stealth/evasions/webgl.vendor',
  'stealth/evasions/window.outerdimensions'
} +1ms
puppeteer-extra-plugin:base:stealth/evasions/chrome.app Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/chrome.app +2ms
puppeteer-extra-plugin:base:stealth/evasions/chrome.csi Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/chrome.csi +2ms
puppeteer-extra-plugin:base:stealth/evasions/chrome.loadTimes Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/chrome.loadTimes +0ms
puppeteer-extra-plugin:base:stealth/evasions/chrome.runtime Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/chrome.runtime +2ms
puppeteer-extra-plugin:base:stealth/evasions/iframe.contentWindow Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/iframe.contentWindow +1ms
puppeteer-extra-plugin:base:stealth/evasions/media.codecs Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/media.codecs +1ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.hardwareConcurrency Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.hardwareConcurrency +0ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.languages Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.languages +1ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.permissions Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.permissions +0ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.plugins Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.plugins +2ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.webdriver Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.webdriver +1ms
puppeteer-extra-plugin:base:stealth/evasions/sourceurl Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/sourceurl +0ms
puppeteer-extra-plugin:base:stealth/evasions/user-agent-override Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/user-agent-override +1ms
puppeteer-extra dependencies missing Set(3) {
  'stealth/evasions/webgl.vendor',
  'stealth/evasions/window.outerdimensions',
  'user-preferences'
} +0ms
puppeteer-extra-plugin:base:stealth/evasions/webgl.vendor Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/webgl.vendor +0ms
puppeteer-extra-plugin:base:stealth/evasions/window.outerdimensions Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/window.outerdimensions +1ms
puppeteer-extra-plugin:base:user-preferences Initialized. +0ms
puppeteer-extra plugin registered user-preferences +0ms
puppeteer-extra dependencies missing Set(1) { 'user-data-dir' } +1ms
puppeteer-extra-plugin:base:user-data-dir Initialized. +0ms
puppeteer-extra-plugin:user-data-dir initialized {
  deleteTemporary: true,
  deleteExisting: false,
  files: [],
  folderPath: '/var/folders/dj/h_jk17gd44q3hff34kl40gtw0000gn/T',
  folderPrefix: 'puppeteer_dev_profile-'
} +0ms
puppeteer-extra plugin registered user-data-dir +17ms
puppeteer-extra ignoring dependency 'stealth/evasions/webgl.vendor', which has been required already. +0ms
puppeteer-extra ignoring dependency 'stealth/evasions/window.outerdimensions', which has been required already. +0ms
puppeteer-extra orderPlugins:before [
  'stealth',
  'stealth/evasions/chrome.app',
  'stealth/evasions/chrome.csi',
  'stealth/evasions/chrome.loadTimes',
  'stealth/evasions/chrome.runtime',
  'stealth/evasions/iframe.contentWindow',
  'stealth/evasions/media.codecs',
  'stealth/evasions/navigator.hardwareConcurrency',
  'stealth/evasions/navigator.languages',
  'stealth/evasions/navigator.permissions',
  'stealth/evasions/navigator.plugins',
  'stealth/evasions/navigator.webdriver',
  'stealth/evasions/sourceurl',
  'stealth/evasions/user-agent-override',
  'stealth/evasions/webgl.vendor',
  'stealth/evasions/window.outerdimensions',
  'user-preferences',
  'user-data-dir'
] +0ms
puppeteer-extra orderPlugins:after [
  'stealth',
  'stealth/evasions/chrome.app',
  'stealth/evasions/chrome.csi',
  'stealth/evasions/chrome.loadTimes',
  'stealth/evasions/chrome.runtime',
  'stealth/evasions/media.codecs',
  'stealth/evasions/navigator.hardwareConcurrency',
  'stealth/evasions/navigator.languages',
  'stealth/evasions/navigator.permissions',
  'stealth/evasions/navigator.plugins',
  'stealth/evasions/navigator.webdriver',
  'stealth/evasions/sourceurl',
  'stealth/evasions/user-agent-override',
  'stealth/evasions/webgl.vendor',
  'stealth/evasions/window.outerdimensions',
  'stealth/evasions/iframe.contentWindow',
  'user-preferences',
  'user-data-dir'
] +0ms
puppeteer-extra-plugin:user-preferences _userPrefsFromPlugins { intl: { accept_languages: 'en-US,en' } } +0ms
puppeteer-extra-plugin:user-data-dir created custom dir /var/folders/dj/h_jk17gd44q3hff34kl40gtw0000gn/T/puppeteer_dev_profile-wAOVxF +3ms
puppeteer-extra-plugin:user-data-dir Wrote file /var/folders/dj/h_jk17gd44q3hff34kl40gtw0000gn/T/puppeteer_dev_profile-wAOVxF/Default/Preferences +3ms
puppeteer-extra-plugin:user-data-dir onClose +1s
puppeteer-extra-plugin:user-data-dir removeUserDataDir +0ms
puppeteer-extra-plugin:user-data-dir onClose +79ms
puppeteer-extra-plugin:user-data-dir removeUserDataDir +0ms

Using the above but with npm start never finishes and yields similar output

> start
> electron .

puppeteer-extra-plugin:base:stealth Initialized. +0ms
puppeteer-extra plugin registered stealth +0ms
puppeteer-extra dependencies missing Set(15) {
  'stealth/evasions/chrome.app',
  'stealth/evasions/chrome.csi',
  'stealth/evasions/chrome.loadTimes',
  'stealth/evasions/chrome.runtime',
  'stealth/evasions/iframe.contentWindow',
  'stealth/evasions/media.codecs',
  'stealth/evasions/navigator.hardwareConcurrency',
  'stealth/evasions/navigator.languages',
  'stealth/evasions/navigator.permissions',
  'stealth/evasions/navigator.plugins',
  'stealth/evasions/navigator.webdriver',
  'stealth/evasions/sourceurl',
  'stealth/evasions/user-agent-override',
  'stealth/evasions/webgl.vendor',
  'stealth/evasions/window.outerdimensions'
} +0ms
puppeteer-extra-plugin:base:stealth/evasions/chrome.app Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/chrome.app +4ms
puppeteer-extra-plugin:base:stealth/evasions/chrome.csi Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/chrome.csi +1ms
puppeteer-extra-plugin:base:stealth/evasions/chrome.loadTimes Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/chrome.loadTimes +0ms
puppeteer-extra-plugin:base:stealth/evasions/chrome.runtime Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/chrome.runtime +1ms
puppeteer-extra-plugin:base:stealth/evasions/iframe.contentWindow Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/iframe.contentWindow +1ms
puppeteer-extra-plugin:base:stealth/evasions/media.codecs Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/media.codecs +0ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.hardwareConcurrency Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.hardwareConcurrency +1ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.languages Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.languages +1ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.permissions Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.permissions +0ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.plugins Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.plugins +2ms
puppeteer-extra-plugin:base:stealth/evasions/navigator.webdriver Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/navigator.webdriver +0ms
puppeteer-extra-plugin:base:stealth/evasions/sourceurl Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/sourceurl +1ms
puppeteer-extra-plugin:base:stealth/evasions/user-agent-override Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/user-agent-override +0ms
puppeteer-extra dependencies missing Set(3) {
  'stealth/evasions/webgl.vendor',
  'stealth/evasions/window.outerdimensions',
  'user-preferences'
} +0ms
puppeteer-extra-plugin:base:stealth/evasions/webgl.vendor Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/webgl.vendor +1ms
puppeteer-extra-plugin:base:stealth/evasions/window.outerdimensions Initialized. +0ms
puppeteer-extra plugin registered stealth/evasions/window.outerdimensions +0ms
puppeteer-extra-plugin:base:user-preferences Initialized. +0ms
puppeteer-extra plugin registered user-preferences +1ms
puppeteer-extra dependencies missing Set(1) { 'user-data-dir' } +0ms
puppeteer-extra-plugin:base:user-data-dir Initialized. +0ms
puppeteer-extra-plugin:user-data-dir initialized {
  deleteTemporary: true,
  deleteExisting: false,
  files: [],
  folderPath: '/var/folders/dj/h_jk17gd44q3hff34kl40gtw0000gn/T',
  folderPrefix: 'puppeteer_dev_profile-'
} +0ms
puppeteer-extra plugin registered user-data-dir +17ms
puppeteer-extra ignoring dependency 'stealth/evasions/webgl.vendor', which has been required already. +0ms
puppeteer-extra ignoring dependency 'stealth/evasions/window.outerdimensions', which has been required already. +0ms
puppeteer-extra orderPlugins:before [
  'stealth',
  'stealth/evasions/chrome.app',
  'stealth/evasions/chrome.csi',
  'stealth/evasions/chrome.loadTimes',
  'stealth/evasions/chrome.runtime',
  'stealth/evasions/iframe.contentWindow',
  'stealth/evasions/media.codecs',
  'stealth/evasions/navigator.hardwareConcurrency',
  'stealth/evasions/navigator.languages',
  'stealth/evasions/navigator.permissions',
  'stealth/evasions/navigator.plugins',
  'stealth/evasions/navigator.webdriver',
  'stealth/evasions/sourceurl',
  'stealth/evasions/user-agent-override',
  'stealth/evasions/webgl.vendor',
  'stealth/evasions/window.outerdimensions',
  'user-preferences',
  'user-data-dir'
] +0ms
puppeteer-extra orderPlugins:after [
  'stealth',
  'stealth/evasions/chrome.app',
  'stealth/evasions/chrome.csi',
  'stealth/evasions/chrome.loadTimes',
  'stealth/evasions/chrome.runtime',
  'stealth/evasions/media.codecs',
  'stealth/evasions/navigator.hardwareConcurrency',
  'stealth/evasions/navigator.languages',
  'stealth/evasions/navigator.permissions',
  'stealth/evasions/navigator.plugins',
  'stealth/evasions/navigator.webdriver',
  'stealth/evasions/sourceurl',
  'stealth/evasions/user-agent-override',
  'stealth/evasions/webgl.vendor',
  'stealth/evasions/window.outerdimensions',
  'stealth/evasions/iframe.contentWindow',
  'user-preferences',
  'user-data-dir'
] +1ms
puppeteer-extra-plugin:user-preferences _userPrefsFromPlugins { intl: { accept_languages: 'en-US,en' } } +0ms
puppeteer-extra-plugin:user-data-dir created custom dir /var/folders/dj/h_jk17gd44q3hff34kl40gtw0000gn/T/puppeteer_dev_profile-OVg3Sf +19ms
puppeteer-extra-plugin:user-data-dir Wrote file /var/folders/dj/h_jk17gd44q3hff34kl40gtw0000gn/T/puppeteer_dev_profile-OVg3Sf/Default/Preferences +40ms
puppeteer-extra-plugin:user-data-dir onClose +1s
puppeteer-extra-plugin:user-data-dir removeUserDataDir +0ms

@regseb
Copy link
Contributor

regseb commented May 3, 2021

I think I have the same problem (and this issue seems to be related to #421).

Code Snippet

const puppeteer = require('puppeteer-extra')
const StealthPlugin = require("puppeteer-extra-plugin-stealth");

puppeteer.use(StealthPlugin({
    enabledEvasions: new Set(["user-agent-override"])
}));

;(async () => {
  const browser = await puppeteer.launch()
  await browser.close();
  console.log("browser.close()");
})()

setTimeout(() => console.log("setTimeout()"), 10000);
  • If you run this code with DEBUG=puppeteer-extra,puppeteer-extra-plugin:* node index.js.
  • Wait until browser.close() is displayed.
  • Do CTRL + C in terminal.
  • The execution doesn't stop.
  puppeteer-extra-plugin:base:stealth Initialized. +0ms
  puppeteer-extra plugin registered stealth +0ms
  puppeteer-extra dependencies missing Set(1) { 'stealth/evasions/user-agent-override' } +1ms
  puppeteer-extra-plugin:base:stealth/evasions/user-agent-override Initialized. +0ms
  puppeteer-extra plugin registered stealth/evasions/user-agent-override +1ms
  puppeteer-extra dependencies missing Set(1) { 'user-preferences' } +0ms
  puppeteer-extra-plugin:base:user-preferences Initialized. +0ms
  puppeteer-extra plugin registered user-preferences +2ms
  puppeteer-extra dependencies missing Set(1) { 'user-data-dir' } +0ms
  puppeteer-extra-plugin:base:user-data-dir Initialized. +0ms
  puppeteer-extra-plugin:user-data-dir initialized {
  deleteTemporary: true,
  deleteExisting: false,
  files: [],
  folderPath: '/tmp',
  folderPrefix: 'puppeteer_dev_profile-'
} +0ms
  puppeteer-extra plugin registered user-data-dir +15ms
  puppeteer-extra orderPlugins:before [
  'stealth',
  'stealth/evasions/user-agent-override',
  'user-preferences',
  'user-data-dir'
] +0ms
  puppeteer-extra orderPlugins:after [
  'stealth',
  'stealth/evasions/user-agent-override',
  'user-preferences',
  'user-data-dir'
] +0ms
  puppeteer-extra-plugin:user-preferences _userPrefsFromPlugins { intl: { accept_languages: 'en-US,en' } } +0ms
  puppeteer-extra-plugin:user-data-dir created custom dir /tmp/puppeteer_dev_profile-rQYjju +3ms
  puppeteer-extra-plugin:user-data-dir Wrote file /tmp/puppeteer_dev_profile-rQYjju/Default/Preferences +1ms
  puppeteer-extra-plugin:user-data-dir onClose +104ms
  puppeteer-extra-plugin:user-data-dir removeUserDataDir +0ms
(node:5571) [DEP0147] DeprecationWarning: In future versions of Node.js, fs.rmdir(path, { recursive: true }) will be removed. Use fs.rm(path, { recursive: true }) instead
(Use `node --trace-deprecation ...` to show where the warning was created)
browser.close()
^C  puppeteer-extra-plugin:user-data-dir onClose +1s
  puppeteer-extra-plugin:user-data-dir removeUserDataDir +1ms
  puppeteer-extra-plugin:user-data-dir Error: ENOENT: no such file or directory, stat '/tmp/puppeteer_dev_profile-rQYjju'
  puppeteer-extra-plugin:user-data-dir     at Object.statSync (node:fs:1156:3)
  puppeteer-extra-plugin:user-data-dir     at __node_internal_ (node:internal/fs/utils:761:8)
  puppeteer-extra-plugin:user-data-dir     at Object.rmdirSync (node:fs:932:15)
  puppeteer-extra-plugin:user-data-dir     at Plugin.deleteUserDataDir (/home/regseb/dev/testcase/node_modules/puppeteer-extra-plugin-user-data-dir/index.js:72:11)
  puppeteer-extra-plugin:user-data-dir     at Plugin.onClose (/home/regseb/dev/testcase/node_modules/puppeteer-extra-plugin-user-data-dir/index.js:114:12)
  puppeteer-extra-plugin:user-data-dir     at process.emit (node:events:365:28) +0ms
setTimeout()
  puppeteer-extra-plugin:user-data-dir onClose +9s
  puppeteer-extra-plugin:user-data-dir removeUserDataDir +0ms
  puppeteer-extra-plugin:user-data-dir Error: ENOENT: no such file or directory, stat '/tmp/puppeteer_dev_profile-rQYjju'
  puppeteer-extra-plugin:user-data-dir     at Object.statSync (node:fs:1156:3)
  puppeteer-extra-plugin:user-data-dir     at __node_internal_ (node:internal/fs/utils:761:8)
  puppeteer-extra-plugin:user-data-dir     at Object.rmdirSync (node:fs:932:15)
  puppeteer-extra-plugin:user-data-dir     at Plugin.deleteUserDataDir (/home/regseb/dev/testcase/node_modules/puppeteer-extra-plugin-user-data-dir/index.js:72:11)
  puppeteer-extra-plugin:user-data-dir     at Plugin.onClose (/home/regseb/dev/testcase/node_modules/puppeteer-extra-plugin-user-data-dir/index.js:114:12)
  puppeteer-extra-plugin:user-data-dir     at process.emit (node:events:365:28) +0ms

Without puppeteer.use(StealthPlugin(...)) or await browser.close(), the program stops at CTRL + C.

Versions

System:
  OS: Linux 5.4 Ubuntu 20.04.2 LTS (Focal Fossa)
  CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
  Memory: 6.96 GB / 15.38 GB
  Container: Yes
  Shell: 5.8 - /usr/bin/zsh
Binaries:
  Node: 16.0.0 - /usr/bin/node
  npm: 7.11.2 - /usr/bin/npm
npmPackages:
  puppeteer: ^9.1.0 => 9.1.0 
  puppeteer-extra: ^3.1.18 => 3.1.18 
  puppeteer-extra-plugin-stealth: ^2.7.8 => 2.7.8

@Tyler-Murphy
Copy link

Tyler-Murphy commented Jun 18, 2021

I've run into problems like this before, where a process.on listener traps SIGINT every time it's sent.

Reproduce the problem with this script. Run it and try hitting ctrl-C repeatedly. It'll be trapped every time and the process won't exit.

process.on(`SIGINT`, () => console.log(`caught SIGINT`))
setInterval(() => {}, 1e3) // do work to prevent immediate exit

I've fixed this a couple ways in the past.

  1. Use process.once, and after cleanup work is complete, print a message saying something like "send SIGINT again to exit".
  2. Re-emit the kill signal after cleanup work is complete.

Option 2 seems like a better fit in this case, and it works better in conjunction with other, unrelated cleanup scripts with their own SIGINT listeners. Here's what it looks like:

process.once(`SIGINT`, () => {
	console.log(`caught SIGINT`)
	process.kill(process.pid, `SIGINT`) // send `SIGINT` back to self
})
setInterval(() => {}, 1e3) // do work

I tried replacing the puppeteer-extra-plugin process.on listeners in the index file with process.once listeners as described, and it seems to fix the problem for me.

For example:

if (this.onClose) {
                if (opts.options.handleSIGINT !== false) {
                    process.once('SIGINT', () => {
                        this.onClose()
                        process.kill(process.pid, `SIGINT`)
                    });
                }
                ...

Note: I haven't tested this against the other test cases in this thread, but it solved my problem. Maybe others can see if it works for them, too.

@Androbin
Copy link
Author

Androbin commented Jun 20, 2021

@Tyler-Murphy I have created a PR based on your example. I have added you to the commit via Co-authored-by for attribution.

Tyler-Murphy added a commit to Tyler-Murphy/signalwire-node that referenced this issue Jul 21, 2021
Right now, it seems like this library is preventing the `SIGINT` (ctrl-C) signal from shutting down my project.

I made these changes using github's edit button, so I haven't tested/linted anything, and there could be mistakes.

See this comment for an explanation: berstend/puppeteer-extra#467 (comment). Here are the comment's contents at the time of writing, in case the link stops working:

I've run into problems like this before, where a `process.on` listener traps `SIGINT` every time it's sent.

Reproduce the problem with this script. Run it and try hitting `ctrl-C` repeatedly. It'll be trapped every time and the process won't exit.

```node
process.on(`SIGINT`, () => console.log(`caught SIGINT`))
setInterval(() => {}, 1e3) // do work to prevent immediate exit
```

I've fixed this a couple ways in the past.
1. Use `process.once`, and after cleanup work is complete, print a message saying something like "send SIGINT again to exit".
2. Re-emit the kill signal after cleanup work is complete.

Option 2 seems like a better fit in this case, and it works better in conjunction with other, unrelated cleanup scripts with their own `SIGINT` listeners. Here's what it looks like:

```node
process.once(`SIGINT`, () => {
	console.log(`caught SIGINT`)
	process.kill(process.pid, `SIGINT`) // send `SIGINT` back to self
})
setInterval(() => {}, 1e3) // do work
```

I tried replacing [the `puppeteer-extra-plugin` `process.on` listeners in the `index` file](https://github.com/berstend/puppeteer-extra/blob/0049d6010311505f27e7f3be804bb198e2c09aa2/packages/puppeteer-extra-plugin/src/index.ts#L506-L519) with `process.once` listeners as described, and it seems to fix the problem for me.

For example:

```node
if (this.onClose) {
                if (opts.options.handleSIGINT !== false) {
                    process.once('SIGINT', () => {
                        this.onClose()
                        process.kill(process.pid, `SIGINT`)
                    });
                }
                ...
```

**Note:** I haven't tested this against the other test cases in this thread, but it solved my problem. Maybe others can see if it works for them, too.
@dev-hyperweb
Copy link
Contributor

maybe we are on same problem
try check on me, I thing it better than change "process" event

but maybe other still use puppeteer lower than version 1.6.0 ?
Replace onClose to onDisconnected (Version 1.6.0+)

#530
or maybe need still support lower version ?

@berstend
Copy link
Owner

This should be fixed through #530

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug report A bug has been reported needs triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants