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

Ability to exit a script while keeping browser running #8261

Open
SuperPat45 opened this issue Apr 21, 2022 · 14 comments
Open

Ability to exit a script while keeping browser running #8261

SuperPat45 opened this issue Apr 21, 2022 · 14 comments

Comments

@SuperPat45
Copy link

SuperPat45 commented Apr 21, 2022

My java server need to convert some HTML pages to PDF.
Today I call a script who use process.launch().
All work fine except I take seconds to complete, and it is too slow.
After investigations, 1.5 to 3 seconds are needed just to launch() chromium and open newPage()...

Bug description

To speed up this, I try to create a new script that use puppeteer to launch a headless chromium instance, save the browserWSEndpoint on the file system and exiting the script leaving the browser opened.

So, other puppeteer scripts can connect to this browserWSEndpoint later to speed up it.

const puppeteer = require("puppeteer");

(async () => {
       const browser = await puppeteer.launch({
		handleSIGINT : false, 
		handleSIGTERM : false, 
		handleSIGHUP : false,
	});

	try {
		const browserWSEndpoint = browser.wsEndpoint();
		console.log("Endpoint: " + browserWSEndpoint);
 
               // Save browserWSEndpoint to the file system
         } finally {
		browser.disconnect();
                process.exit(0);
	}
})();

My main problem is that the script hang without calling browser.close();

So I try with the 3 parameters: handleSIGINT : false, handleSIGTERM : false, handleSIGHUP : false
They seem to have no effect, pressing Ctrl+c kill the script and chromium too.
I try with adding process.kill(0); at the end of the script function to force exit, but the chromium is killed too.

Are-there another way to let the chromium process alive after script execution?

Puppeteer version

13.4.0

Node.js version

v16.14.2

npm version

8.5.0

What operating system are you seeing the problem on?

Windows

Relevant log output

No response

@SuperPat45 SuperPat45 added the bug label Apr 21, 2022
@SuperPat45
Copy link
Author

SuperPat45 commented May 4, 2022

I found why:

As I am on Windows, the chromium process is not launched as "detached" in BrowserRunner:

        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,
        });

After forcing detached to true, It works fine.

I don't understand why it is not detached on Windows?
At least, can you add an option in puppeteer.launch() to allow the sub-process to be detached or not?

@passionInfinite
Copy link

Looks like same issue is happening on the Linux platform as well.

@SuperPat45 SuperPat45 changed the title [Bug]: puppeteer.launch({handleSIGINT:false,handleSIGTERM:false,handleSIGHUP:false}) seem to have no effect Ability to exit a script while keeping browser running Jun 16, 2022
@SuperPat45
Copy link
Author

Launching a detached browser and using exiting the script process.exit(0) work, but only on Windows.
On Linux, the browser is still killed when the script is closed...

@stale
Copy link

stale bot commented Aug 15, 2022

We're marking this issue as unconfirmed because it has not had recent activity and we weren't able to confirm it yet. It will be closed if no further activity occurs within the next 30 days.

@stale stale bot added the unconfirmed label Aug 15, 2022
@pahar0
Copy link

pahar0 commented Aug 29, 2022

Launching a detached browser and using exiting the script process.exit(0) work, but only on Windows. On Linux, the browser is still killed when the script is closed...

for me this doesn't work on windows. i'm using these args to launch a detached browser, maybe i'm doing it incorrectly:

['--no-sandbox', '--disable-setuid-sandbox', '--single-process', '--no-zygote']

Asking same question op did:
Are-there another way to let the chromium process alive after script execution?

@OrKoN
Copy link
Collaborator

OrKoN commented Sep 5, 2022

Why do you want to kill the parent process that launched Chromium (you can just keep it running as long as you need the shared chromium instance)? I don't think we will support detaching child processes though so marking it as not planned for now.

@OrKoN OrKoN closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2022
@SuperPat45
Copy link
Author

SuperPat45 commented Sep 5, 2022

To keep file image and script files in cache and to connect fastly to a dynamic page (because pupetter and chromium are too slow to launch and open a page: almost 2 seconds).
When chromium is still stared, it takes some only 100ms.
We can see a huge difference if I need to convert 10 pages to different PDF.

@OrKoN
Copy link
Collaborator

OrKoN commented Sep 5, 2022

@SuperPat45 what I mean is you can start node launchChrome.js and keep it running as well as run node printPDF.js that connects to the launched chrome and saves the startup time. So there is no need to exit launchChrome.js and keep only the browser process running since you can keep launchChrome.js running as well?

@SuperPat45
Copy link
Author

To avoid keeping a superflux nodejs process in memory.

@OrKoN
Copy link
Collaborator

OrKoN commented Sep 5, 2022

@SuperPat45 in that case I suggest that you launch the chromium binary directly

@OrKoN
Copy link
Collaborator

OrKoN commented Sep 5, 2022

Let me re-classify this a feature but it'd be a very low priority for us.

@sbuddala-fraedom
Copy link

Hi, is there alternate approach on windows? I am running a script using powershell which launches browser using puppeteer. I run some automated puppeteer commands but after that I want to keep the browser running and disconnect puppeteer.

@Abdurrurrab-Ajmal
Copy link

Yes I am facing the same issue, does anyone have any solution? I have already posted this on stackoverflow too but no response. Searched on chat gpt too but no use

@shirser121
Copy link

handleSIGINT : false,
handleSIGTERM : false,
handleSIGHUP : false,

I want to keep the browser open while I update my process and then reconnect to it without losing any information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants