shawnliujw / puppeteer-lambda

Module for using Headless-Chrome by Puppeteer on AWS Lambda.
MIT License
117 stars 20 forks source link

_getBrowser in src/index.js stuck in infinite loop #8

Closed awanishraj closed 5 years ago

awanishraj commented 5 years ago

In src/index.js the _getBrowser routine gets stuck in an infinite loop when the browser element is closed and opened again by another lambda instance

browser.close();

// Lambda invoked again with reused context
puppeteerLambda.getBrowser({});    //Stuck forever

AWS Lambda reuses execution context, so if invoked too soon again, it gets stuck in a loop.

In src/index.js

if (null === globalBrowser && !getting) {
    //....;
}else{
    //infinite loop;
}

The if condition always fails if globalBrowser is null and the value of getting is true. On further inspection, the value of getting is not being set to false ever. Adding getting = false inside the if condition after the browser is successfully launched fixes the issue

shawnliujw commented 5 years ago

fixed and new version released , thanks

awanishraj commented 5 years ago

Hi Shawn, the issue is still present. getting = false needs to be present at the end of if condition and not else

if (null === globalBrowser && !getting) {
        //...
        const version = await globalBrowser.version();
        console.log(`Launch chrome: ${version}`);
        getting = false; //inside if condition
        return globalBrowser;
} else{
        do {
            await Promise.delay(50);
        } while (!globalBrowser);
        // getting = false; <- Not required here
        return globalBrowser;
}