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

can not use browser.close() method properly #41

Closed
ibrahimmus opened this issue Apr 22, 2024 · 8 comments
Closed

can not use browser.close() method properly #41

ibrahimmus opened this issue Apr 22, 2024 · 8 comments

Comments

@ibrahimmus
Copy link

I am running multiple processes in parallel. Browser.close() does not have any effect which leads to multiple chrome processes hanging.

@zfcsoftware
Copy link
Owner

Please add your code. If you get an error, the program stops but chrome does not. It may be caused by this.

@ibrahimmus
Copy link
Author

The whole logic is in try catch and finally block, and in finally block I have await browser.close().
I will paste the code example later but FYI the same code was working with puppeteer and puppeteer stealth previously. I did not get into this situation of having multiple chrome processes hanging

@ibrahimmus
Copy link
Author

Just run these two scripts together. Only run the second one, first one needs to be in the same path.

////FIRST SCRIPT

import { connect } from 'puppeteer-real-browser'

const main = async () => {
const {browser, page} = await connect({
headless: 'auto',
args: [],
customConfig: {},
skipTarget: [],
fingerprint: false,
turnstile: false,
connectOption: {},
tf: true,
})
await page.goto('https://antoinevastel.com/bots/datadome');
await page.evaluate(() => {
return new Promise(resolve => {
setTimeout(resolve, 20000);
});
});
await browser.close();
process.exit(0);
}

main();

//SECOND SCRIPT

import { fork } from 'child_process';

const maxForks = 5; // Maximum number of forks allowed
let runningForks = 0; // Initialize the number of running forks

const startFork = () => {
const child = fork('./${NAME_OF_FIRST_SCRIPT}'); // Fork the main script

// Event handler for when the forked process exits
child.on('exit', () => {
    runningForks--; // Decrement the count of running forks
    if (runningForks < maxForks) {
        startFork(); // Start a new fork if the count is less than the maximum
    }
});

runningForks++; // Increment the count of running forks

}

// Start initial forks
for (let i = 0; i < maxForks; i++) {
startFork();
}

@zfcsoftware
Copy link
Owner

Hi, Puppeteer stops working when an error is received (even when closing the browser) but chromium does not. This causes the browser to hang as you mentioned. I am working on this bug and will fix it.

@ibrahimmus
Copy link
Author

@zfcsoftware is there any way I could help you with this to speed it up? This is the only thing that blocks me in using/testing this in my case. Thanks

@zfcsoftware
Copy link
Owner

@zfcsoftware is there any way I could help you with this to speed it up? This is the only thing that blocks me in using/testing this in my case. Thanks

Hello,

chrome: chrome,

You can use the chrome.close() method.

@ibrahimmus
Copy link
Author

@zfcsoftware there is only chrome.kill() option. So you are telling me to get chrome instance back from connect method response and work with it as a temp solution?

@zfcsoftware
Copy link
Owner

The chrome variable is already returned, yes you can use it that way. Normally this problem does not occur under normal conditions, but it may occur when an error is received, I need to examine it in detail.
https://github.com/zfcsoftware/cf-clearance-scraper/blob/main/module/browser.js
You can create a browser like this and try that too.

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

No branches or pull requests

2 participants