karma-chrome-launcher icon indicating copy to clipboard operation
karma-chrome-launcher copied to clipboard

ChromeHeadless can't run as root with --no-sandbox anymore

Open boboldehampsink opened this issue 6 years ago • 25 comments

After updating to Chrome 62 and Chromedriver 2.33, I get the following error when running karma tests with ChromeHeadless:

  | 25 10 2017 08:47:40.197:ERROR [launcher]: Cannot start ChromeHeadless
  | [1025/084740.006078:ERROR:zygote_host_impl_linux.cc(88)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

This is in Docker running Ubuntu 16

boboldehampsink avatar Oct 25 '17 08:10 boboldehampsink

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

boboldehampsink avatar Oct 25 '17 09:10 boboldehampsink

@boboldehampsink Is it assumed that your actual configuration also has --headless and --remote-debugging=9222 in the flags array?

tariqbuilds avatar Oct 25 '17 10:10 tariqbuilds

When specifying the browser as ChromeHeadless this is automatically added, yes.

boboldehampsink avatar Oct 25 '17 10:10 boboldehampsink

This worked for us. Thanks, @boboldehampsink

tariqbuilds avatar Oct 25 '17 10:10 tariqbuilds

Worked for me too. Thanks.

sebastian-burdun avatar Nov 28 '17 10:11 sebastian-burdun

Worked for me too. Thanks guys!

Keywords: CodeBuild, Karma, Chrome

germs12 avatar Dec 19 '17 17:12 germs12

boboldehampsink, sorry, but where should this workaround be written? Is it about docker?

myluckagain avatar Dec 23 '17 16:12 myluckagain

In karma config

boboldehampsink avatar Dec 24 '17 08:12 boboldehampsink

Sorry for the rookie question: and where should I put this text?

browsers: ['ChromeHeadlessNoSandbox'], customLaunchers: { ChromeHeadlessNoSandbox: { base: 'ChromeHeadless', flags: ['--no-sandbox'] } },

Where is this karma config?

DragonRus avatar Dec 31 '17 11:12 DragonRus

@DragonRus By default, most projects have Karma configuration stored in a file named "karma.conf.js." However, that is just the default; the configuration file could be named anything and passed to Karma like this: karma start some-karma-config.js.

If your project doesn't have a configuration file, you can generate it by running karma init from a command line. See the Karma documentation.

alexbainter avatar Jan 02 '18 19:01 alexbainter

I was not aware of the ability to define custom launchers. I would consider it very helpful if this was added to the README.

oliversalzburg avatar Mar 22 '18 00:03 oliversalzburg

I keep getting smth like this in bitbucket pipeline.

02 05 2018 19:40:16.221:WARN [karma]: No captured browser, open http://localhost:9876/
02 05 2018 19:40:16.230:INFO [karma]: Karma v2.0.2 server started at http://0.0.0.0:9876/
02 05 2018 19:40:16.230:INFO [launcher]: Launching browser Chrome with unlimited concurrency
02 05 2018 19:40:16.236:INFO [launcher]: Starting browser Chrome
02 05 2018 19:40:16.656:ERROR [launcher]: Cannot start Chrome
	[79:79:0502/194016.598825:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.656:ERROR [launcher]: Chrome stdout: 
02 05 2018 19:40:16.656:ERROR [launcher]: Chrome stderr: [79:79:0502/194016.598825:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.676:INFO [launcher]: Trying to start Chrome again (1/2).
02 05 2018 19:40:16.883:ERROR [launcher]: Cannot start Chrome
	[90:90:0502/194016.759919:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.883:ERROR [launcher]: Chrome stdout: 
02 05 2018 19:40:16.883:ERROR [launcher]: Chrome stderr: [90:90:0502/194016.759919:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.919:INFO [launcher]: Trying to start Chrome again (2/2).
02 05 2018 19:40:17.145:ERROR [launcher]: Cannot start Chrome
	[101:101:0502/194017.008599:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:17.145:ERROR [launcher]: Chrome stdout: 
02 05 2018 19:40:17.145:ERROR [launcher]: Chrome stderr: [101:101:0502/194017.008599:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:17.152:ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.
02 05 2018 19:40:23.683:WARN [karma]: No captured browser, open http://localhost:9876/

AlexanderKozhevin avatar May 02 '18 19:05 AlexanderKozhevin

^^ I had something like that in Jenkins. Turned out I had to set the --browsers flag in the command as it was still defaulting to plain headless Chrome without the --no-sandbox flag. Here's one example of what I used: yarn test --single-run --no-progress --browsers Chrome_without_security

Where Chrome_without_security was set up as this:

module.exports = function (config) {
  process.env.CHROME_BIN = require('puppeteer').executablePath()

  config.set({

    // You may use 'ChromeCanary', 'Chromium' or any other supported browser
    browsers: ['Chrome_without_security'], 
    // you can define custom flags
    customLaunchers: {
      Chrome_without_security: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    }
  });
};

Puppeteer is there to handle any missing Chrome package, of course.

marksyzm avatar May 08 '18 10:05 marksyzm

If you're running Puppeteer directly from Node code, you can provide this flag as follows: const browser = await puppeteer.launch({headless: true, args:['--no-sandbox']});

yuri-wisestamp avatar Jun 13 '18 12:06 yuri-wisestamp

Thanks! @yuri-wisestamp

victorguerra95 avatar Jun 20 '18 07:06 victorguerra95

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

Don't forget to add --headless ChromeHeadlessNoSandbox to your test command. For me, that meant changing

ng test ${library} --browsers ChromeHeadless --watch=${watch} to

ng test ${library} --browsers ChromeHeadlessNoSandbox --watch=${watch}

It's probably obvious to you, but it took me a minute to figure that out. I hope that helps.

westandy-dcp avatar May 21 '19 16:05 westandy-dcp

Hello I just tried the following line code and it works perfectly :100:

run this line in the terminal: google-chrome --no-sandbox --user-data-dir

or you go here : https://stackoverflow.com/questions/12258086/how-do-i-run-google-chrome-as-root

Zicrou avatar Aug 06 '19 08:08 Zicrou

@Zicrou or any can give some advice about How it works for you?

i will show mine config for compare

karma.config

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
const process = require('process');
process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine', '@angular-devkit/build-angular'],
        plugins: [
            require('karma-jasmine'),
            require('karma-chrome-launcher'),
            require('karma-jasmine-html-reporter'),
            require('karma-coverage-istanbul-reporter'),
            require('@angular-devkit/build-angular/plugins/karma')
        ],
        client: {
            clearContext: false // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
            dir: require('path').join(__dirname, './coverage/angCICD'),
            reports: ['html', 'lcovonly', 'text-summary'],
            fixWebpackSourcePaths: true
        },
        reporters: ['progress', 'kjhtml'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['ChromeHeadlessNoSandbox'],
        customLaunchers: {
            ChromeHeadlessNoSandbox: {
                base: 'ChromeHeadless',
                flags: ['--no-sandbox']
            }

        },
        singleRun: false,
        restartOnFileChange: true,
        concurrency: Infinity
    });
};

protractor.config

// @ts-check
// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts
const puppeteer = require('puppeteer');
const { SpecReporter } = require('jasmine-spec-reporter');

/**
 * @type { import("protractor").Config }
 */
exports.config = {
    allScriptsTimeout: 11000,
    specs: [
        './src/**/*.e2e-spec.ts'
    ],
    capabilities: {
        browserName: 'chrome',
        chromeOptions: {
            args: ['--headless', '--no-sandbox'],
            binary: puppeteer.executablePath()
        }
    },
    directConnect: true,
    baseUrl: 'http://localhost:4200/',
    framework: 'jasmine',
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 30000,
        print: function() {}
    },
    onPrepare() {
        require('ts-node').register({
            project: require('path').join(__dirname, './tsconfig.json')
        });
        jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
    }
};

gitlab-ci.yml

variables:
  IMG_BUILD: node:latest
  IMG_TEST: trion/ng-cli-karma
  IMG_TESTING: alekzonder/puppeteer:latest
  IMG_TESTING_FINAL: node:8.9.1
  IMG_GITLAB_CI: juristr/angular-ci-build:1.0.0

deploy_test:
  stage: test
  image: ${IMG_BUILD}
  environment: Production
  artifacts:
    paths:
      - node_modules/
  only:
    - master
  script:
    - rm ./package-lock.json
    - apt-get update
    - apt-get install gconf-service libasound2 libatk1.0–0 libc6 libcairo2 libcups2 libdbus-1–3 libexpat1 libfontconfig1 libgcc1 libgconf-2–4 libgdk-pixbuf2.0–0 libglib2.0–0 libgtk-3–0 libnspr4 libpango-1.0–0 libpangocairo-1.0–0 libstdc++6 libx11–6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget -y
    - npm install
    - npm run test-ci

definition for npm run test-ci is:

"test-ci": "ng test --no-watch --no-progress --browsers=ChromeHeadlessNoSandbox",

and that is the error give me gitlab jobs in console

21 08 2019 17:30:16.241:WARN [karma]: No captured browser, open http://localhost:9876/
21 08 2019 17:30:16.357:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
21 08 2019 17:30:16.358:INFO [launcher]: Launching browsers ChromeHeadlessNoSandbox with concurrency unlimited
21 08 2019 17:30:16.361:INFO [launcher]: Starting browser ChromeHeadless
21 08 2019 17:30:17.276:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.276:ERROR [launcher]: ChromeHeadless stdout: 
21 08 2019 17:30:17.277:ERROR [launcher]: ChromeHeadless stderr: /app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.307:INFO [launcher]: Trying to start ChromeHeadless again (1/2).
21 08 2019 17:30:17.358:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.359:ERROR [launcher]: ChromeHeadless stdout: 
21 08 2019 17:30:17.359:ERROR [launcher]: ChromeHeadless stderr: /app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.382:INFO [launcher]: Trying to start ChromeHeadless again (2/2).
21 08 2019 17:30:17.436:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.437:ERROR [launcher]: ChromeHeadless stdout: 
21 08 2019 17:30:17.437:ERROR [launcher]: ChromeHeadless stderr: /app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.469:ERROR [launcher]: ChromeHeadless failed 2 times (cannot start). Giving up.
21 08 2019 17:30:22.314:WARN [karma]: No captured browser, open http://localhost:9876/

I have this version of puppeteer --> "puppeteer": "^1.19.0",

i am missing something?

thx

^^ I had something like that in Jenkins. Turned out I had to set the --browsers flag in the command as it was still defaulting to plain headless Chrome without the --no-sandbox flag. Here's one example of what I used: yarn test --single-run --no-progress --browsers Chrome_without_security

Where Chrome_without_security was set up as this:

module.exports = function (config) {
  process.env.CHROME_BIN = require('puppeteer').executablePath()

  config.set({

    // You may use 'ChromeCanary', 'Chromium' or any other supported browser
    browsers: ['Chrome_without_security'], 
    // you can define custom flags
    customLaunchers: {
      Chrome_without_security: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    }
  });
};

Puppeteer is there to handle any missing Chrome package, of course.

rabindranathforcast avatar Aug 21 '19 19:08 rabindranathforcast

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

Don't forget to add --headless ChromeHeadlessNoSandbox to your test command. For me, that meant changing

ng test ${library} --browsers ChromeHeadless --watch=${watch} to

ng test ${library} --browsers ChromeHeadlessNoSandbox --watch=${watch}

It's probably obvious to you, but it took me a minute to figure that out. I hope that helps.

about this solution i have this setup

karma.config

const process = require('process');
process.env.CHROME_BIN = require('puppeteer').executablePath();

.
.
.
.

 browsers: ['ChromeHeadlessNoSandbox'],
        customLaunchers: {
            ChromeHeadlessNoSandbox: {
                base: 'ChromeHeadless',
                flags: ['--no-sandbox', '--headless']
            }

        },

in package.json apply npm run test-ci

"test-ci": "ng test --no-watch --no-progress --browsers=ChromeHeadlessNoSandbox",

gitlab-ci results

21 08 2019 20:01:29.751:WARN [karma]: No captured browser, open http://localhost:9876/
21 08 2019 20:01:29.822:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
21 08 2019 20:01:29.822:INFO [launcher]: Launching browsers ChromeHeadlessNoSandbox with concurrency unlimited
21 08 2019 20:01:29.849:INFO [launcher]: Starting browser ChromeHeadless
21 08 2019 20:01:30.732:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

rabindranathforcast avatar Aug 21 '19 20:08 rabindranathforcast

i solved after see this videos https://youtu.be/tc379GTTgys

100% problem has been fixed :) ..

xinnxz avatar Apr 26 '20 00:04 xinnxz

That results in a SIGSEGV for me, although the tests appear to pass.

01 05 2020 15:15:01.930:INFO [karma-server]: Karma v5.0.2 server started at http://0.0.0.0:9876/
01 05 2020 15:15:01.932:INFO [launcher]: Launching browsers ChromeNoSandbox with concurrency unlimited
01 05 2020 15:15:01.951:INFO [launcher]: Starting browser ChromeHeadless
01 05 2020 15:15:02.241:INFO [Chrome Headless 81.0.4044.129 (Mac OS 10.14.6)]: Connected on socket PylVnfHy8jmDMhm2AAAA with id 97738507
Chrome Headless 81.0.4044.129 (Mac OS 10.14.6): Executed 13 of 14 (skipped 1) SUCCESS (0.01 secs / 0.042 secs)
TOTAL: 13 SUCCESS
error Command failed with signal "SIGSEGV".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Command failed: /Users/lpender/.yvm/versions/v1.22.4/bin/yarn.js test --browsers ChromeNoSandbox

lpender avatar May 01 '20 22:05 lpender

For me, my karma.config.js was missing this:

 customLaunchers: {
      chromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

github4es avatar Jun 24 '20 17:06 github4es

My karma.conf.js looks like // Karma configuration file, see link for more information // https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) { config.set({ basePath: '', frameworks: ['jasmine', '@angular-devkit/build-angular'], plugins: [ require('karma-jasmine'), require('karma-chrome-launcher'), require('karma-jasmine-html-reporter'), require('karma-coverage-istanbul-reporter'), require('@angular-devkit/build-angular/plugins/karma') ], client:{ clearContext: false // leave Jasmine Spec Runner output visible in browser }, coverageIstanbulReporter: { dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ], fixWebpackSourcePaths: true },

reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadlessNoSandbox'],
  customLaunchers:{
    ChromeHeadlessNoSandbox:{
      base: 'ChromeHeadless',
      flags: ['--no-sandbox']
    }
},
singleRun: false

}); };

but after running npm test getting error as

Disconnected (0 times) , because no message in 30000 ms. Chrome Headless 92.0.4515.131 (Windows 10) ERROR Disconnected , because no message in 30000 ms.

Chrome Headless 92.0.4515.131 (Windows 10) ERROR Disconnected , because no message in 30000 ms.

npm ERR! Test failed. See above for more details.

ankitkpathak04 avatar Aug 18 '21 14:08 ankitkpathak04

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

bro where i have to save this code i mean can you share location

deepansh39 avatar Aug 23 '21 18:08 deepansh39

i solved after see this videos https://youtu.be/tc379GTTgys

100% problem has been fixed :) ..

THANKS BRO FOR THIS VIDEO MY PROBLEM SOLVED; <<<print("thanks for this")

deepansh39 avatar Aug 23 '21 18:08 deepansh39