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] Error when using PM2 process manager in cluster mode #4461

Closed
rtrag opened this issue Nov 17, 2020 · 5 comments · Fixed by #4513
Closed

[BUG] Error when using PM2 process manager in cluster mode #4461

rtrag opened this issue Nov 17, 2020 · 5 comments · Fixed by #4513

Comments

@rtrag
Copy link

rtrag commented Nov 17, 2020

Context:

  • Playwright Version: 1.6.1
  • Operating System: Linux and Mac
  • Node.js version: v15.2.0
  • Browser: Chromium

Code Snippet

const browser_args = [
    "--disable-gpu",
    "--disable-web-security",
    "--disable-dev-shm-usage",
    "--disable-setuid-sandbox",
    "--no-first-run",
    "--no-sandbox",
    "--no-zygote",
    "--ignore-certificate-errors",
    "--ignore-certificate-errors-spki-list",
    "--enable-features=NetworkService",
]

 const browserServer = await playwright.chromium.launchServer({ headless: true, ignoreHTTPSErrors: true, args: browser_args })

Describe the bug

Getting error when using PM2 process manager in cluster mode, but works fine when using node to run

1|www | TypeError: Cannot read property 'port' of null
1|www | at new BrowserServerImpl (../node_modules/playwright/lib/browserServerImpl.js:52:108)
1|www | at BrowserServerLauncherImpl.launchServer (..node_modules/playwright/lib/browserServerImpl.js:41:16)

@rtrag rtrag changed the title [BUG] [BUG] Getting error when using PM2 process manager in cluster mode Nov 17, 2020
@aslushnikov
Copy link
Collaborator

@rtag what do I do to reproduce this locally? I tried just running browser server and launching it with

$ npx pm2 start a.js -i max

and this ran just fine.

@rtrag
Copy link
Author

rtrag commented Nov 17, 2020

can I get your a.js @aslushnikov

CODE THAT DOES NOT WORK

const http = require('http');
const playwright = require('playwright');
const browser_args = [
    "--disable-gpu",
    "--disable-web-security",
    "--disable-dev-shm-usage",
    "--disable-setuid-sandbox",
    "--no-first-run",
    "--no-sandbox",
    "--no-zygote",
    "--ignore-certificate-errors",
    "--ignore-certificate-errors-spki-list",
    "--enable-features=NetworkService",
]
const server = http.createServer(async  (req, res) => {

    const browserServer = await playwright.chromium.launchServer({ headless: true, ignoreHTTPSErrors: true, args: browser_args })
    const wsEndpoint = (await browserServer).wsEndpoint()
    console.log("wsEndpoint", wsEndpoint);

});

server.listen(6000);

console.log('Node.js web server at port 6000 is running..')

@rtrag
Copy link
Author

rtrag commented Nov 18, 2020

Not sure why but under PM2 address is null @aslushnikov

node_modules/playwright/lib/browserServerImpl.js

 this._server = new ws.Server({ port });
 const address = this._server.address();

@rtrag rtrag changed the title [BUG] Getting error when using PM2 process manager in cluster mode [BUG] Error when using PM2 process manager in cluster mode Nov 19, 2020
@rtrag
Copy link
Author

rtrag commented Nov 19, 2020

What I found so far is if cluster.fork() is used it breaks ws from require("ws"); @pavelfeldman . Maybe this is just a ws issue? Or I am doing something wrong?

node_modules/playwright/lib/browserServerImpl.js:52
this._wsEndpoint = typeof address === 'string' ? ${address}/${token} : ws://127.0.0.1:${address.port}/${token};

const http = require('http');
const playwright = require('playwright');
const cluster = require('cluster')

const browser_args = [
    "--disable-gpu",
    "--disable-web-security",
    "--disable-dev-shm-usage",
    "--disable-setuid-sandbox",
    "--no-first-run",
    "--no-sandbox",
    "--no-zygote",
    "--ignore-certificate-errors",
    "--ignore-certificate-errors-spki-list",
    "--enable-features=NetworkService",
]

const { chromium } = require('playwright');  // Or 'webkit' or 'firefox'.

const server = http.createServer(async  (req, res) => {

    const browserServer = await chromium.launchServer({ headless: true, ignoreHTTPSErrors: true, args: browser_args });
    const wsEndpoint = await browserServer.wsEndpoint();
    console.log(wsEndpoint);
    // Use web socket endpoint later to establish a connection.
    const browser = await chromium.connect({ wsEndpoint });
    const context = await browser.newContext();
    console.log("Number of contexts currently running in this browser: " + browser.contexts().length);

});


if (cluster.isMaster) {
    console.log(`Master ${process.pid} is running`);

    // Fork workers.
    for (let i = 0; i < 4; i++) {
        cluster.fork();
    }

    cluster.on('exit', (worker, code, signal) => {
        console.log(`worker ${worker.process.pid} died`);
    });
} else {
    // Workers can share any TCP connection
    // In this case it is an HTTP server
    server.listen(4000);

}

console.log('Node.js web server at port 4000 is running..')

@dgozman
Copy link
Contributor

dgozman commented Nov 23, 2020

I can repro this, thank you for the script.

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.

4 participants