Skip to content

Commit

Permalink
fix: re-write Chromium startup error with clear instructions
Browse files Browse the repository at this point in the history
This patch detects Chromium crash with a sandboxing error and re-writes
the error to surface information nicely.

```sh
pwuser@396166c88ec2:/root/playwright$ node a.js
(node:9916) UnhandledPromiseRejectionWarning: browserType.launch: Protocol error (Target.setAutoAttach): Target closed.
=========================== logs ===========================
[browser] <launching> /home/pwuser/.cache/ms-playwright/chromium-786218/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disab
le-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=TranslateUI,BlinkGenPropertyTrees,ImprovedCookieControls,SameSiteByDefaultCookies --disable-hang-monitor --disab
le-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --user-data-dir=/tmp/playwrig
ht_chromiumdev_profile-S7DvGF --remote-debugging-pipe --headless --hide-scrollbars --mute-audio --no-startup-window
[browser] <launched> pid=9993
[browser] [0721/210028.684682:FATAL:zygote_host_impl_linux.cc(117)] No usable sandbox! Update your kernel or see https://chromium.googlesource.com/chromium/src/+/master/docs/linux/suid_sandbox_development.md for more information on developing with the SUID sandbox. If you want to live
dangerously and need an immediate workaround, you can try using --no-sandbox.
[browser] #0 0x55604e10fca9 base::debug::CollectStackTrace()
[browser] #1 0x55604e08abb3 base::debug::StackTrace::StackTrace()
[browser] microsoft#2 0x55604e09c810 logging::LogMessage::~LogMessage()
[browser] microsoft#3 0x55604c7e68de content::ZygoteHostImpl::Init()
[browser] microsoft#4 0x55604dc56067 content::ContentMainRunnerImpl::Initialize()
[browser] microsoft#5 0x55604dca493a service_manager::Main()
[browser] microsoft#6 0x55604dc54681 content::ContentMain()
[browser] microsoft#7 0x55604dca365d headless::(anonymous namespace)::RunContentMain()
[browser] microsoft#8 0x55604dca335c headless::HeadlessShellMain()
[browser] microsoft#9 0x55604b5a0527 ChromeMain
[browser] microsoft#10 0x7fcbfda52b97 __libc_start_main
[browser] microsoft#11 0x55604b5a036a _start
[browser]
[browser] Received signal 6
[browser] #0 0x55604e10fca9 base::debug::CollectStackTrace()
[browser] #1 0x55604e08abb3 base::debug::StackTrace::StackTrace()
[browser] microsoft#2 0x55604e10f845 base::debug::(anonymous namespace)::StackDumpSignalHandler()
[browser] microsoft#3 0x7fcc03ee3890 (/lib/x86_64-linux-gnu/libpthread-2.27.so+0x1288f)
[browser] microsoft#4 0x7fcbfda6fe97 gsignal
[browser] microsoft#5 0x7fcbfda71801 abort
[browser] microsoft#6 0x55604e10e7a5 base::debug::BreakDebugger()
[browser] microsoft#7 0x55604e09cc7b logging::LogMessage::~LogMessage()
[browser] microsoft#8 0x55604c7e68de content::ZygoteHostImpl::Init()
[browser] microsoft#9 0x55604dc56067 content::ContentMainRunnerImpl::Initialize()
[browser] microsoft#10 0x55604dca493a service_manager::Main()
[browser] microsoft#11 0x55604dc54681 content::ContentMain()
[browser] microsoft#12 0x55604dca365d headless::(anonymous namespace)::RunContentMain()
[browser] microsoft#13 0x55604dca335c headless::HeadlessShellMain()
[browser] microsoft#14 0x55604b5a0527 ChromeMain
[browser] microsoft#15 0x7fcbfda52b97 __libc_start_main
[browser] microsoft#16 0x55604b5a036a _start
[browser]   r8: 0000000000000000  r9: 00007fff94da5d90 r10: 0000000000000008 r11: 0000000000000246
[browser]  r12: 00007fff94da7060 r13: 00007fff94da5ff0 r14: 00007fff94da7070 r15: aaaaaaaaaaaaaaaa
[browser]   di: 0000000000000002  si: 00007fff94da5d90  bp: 00007fff94da5fe0  bx: 00007fff94da6824
[browser]   dx: 0000000000000000  ax: 0000000000000000  cx: 00007fcbfda6fe97  sp: 00007fff94da5d90
[browser]   ip: 00007fcbfda6fe97 efl: 0000000000000246 cgf: 002b000000000033 erf: 0000000000000000
[browser]  trp: 0000000000000000 msk: 0000000000000000 cr2: 0000000000000000
[browser] [end of stack trace]
[browser] Calling _exit(1). Core file will not be generated.
============================================================
Note: use DEBUG=pw:api environment variable and rerun to capture Playwright logs.Error
    at /root/playwright/lib/chromium/crConnection.js:131:63
    at new Promise (<anonymous>)
    at CRSession.send (/root/playwright/lib/chromium/crConnection.js:130:16)
    at CRSession.send (/root/playwright/lib/helper.js:78:31)
    at Function.connect (/root/playwright/lib/chromium/crBrowser.js:54:27)
    at Chromium._connectToTransport (/root/playwright/lib/server/chromium.js:53:38)
    at Chromium._innerLaunch (/root/playwright/lib/server/browserType.js:87:36)
    at async ProgressController.run (/root/playwright/lib/progress.js:75:28)
    at async Chromium.launch (/root/playwright/lib/server/browserType.js:60:25)
    at async /root/playwright/a.js:4:19
(node:9916) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejec
tion, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:9916) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
```

```sh
pwuser@396166c88ec2:/root/playwright$ node a.js
(node:10074) UnhandledPromiseRejectionWarning: Chromium sandboxing failed!
================================
To workaround sandboxing issues, do either of the following:
  - (preferred): Configure environment to support sandboxing: https://github.com/microsoft/playwright/blob/master/docs/troubleshooting.md
  - (alternative): Launch Chromium without sandbox using 'chromiumSandbox: false' option
================================
Error
    at /root/playwright/lib/chromium/crConnection.js:131:63
    at new Promise (<anonymous>)
    at CRSession.send (/root/playwright/lib/chromium/crConnection.js:130:16)
    at CRSession.send (/root/playwright/lib/helper.js:78:31)
    at Function.connect (/root/playwright/lib/chromium/crBrowser.js:54:27)
    at Chromium._connectToTransport (/root/playwright/lib/server/chromium.js:53:38)
    at Chromium._innerLaunch (/root/playwright/lib/server/browserType.js:87:36)
    at async ProgressController.run (/root/playwright/lib/progress.js:75:28)
    at async Chromium.launch (/root/playwright/lib/server/browserType.js:60:25)
    at async /root/playwright/a.js:4:19
(node:10074) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:10074) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
```
  • Loading branch information
aslushnikov committed Jul 21, 2020
1 parent 47e30f0 commit 18bde71
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/server/browserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export abstract class BrowserTypeBase implements BrowserType {
assert(!(options as any).port, 'Cannot specify a port without launching as a server.');
options = validateLaunchOptions(options);
const loggers = new Loggers(options.logger);
const browser = await runAbortableTask(progress => this._innerLaunch(progress, options, loggers, undefined), loggers.browser, TimeoutSettings.timeout(options), `browserType.launch`);
const browser = await runAbortableTask(progress => this._innerLaunch(progress, options, loggers, undefined), loggers.browser, TimeoutSettings.timeout(options), `browserType.launch`).catch(e => { throw this._rewriteStartupError(e); });
return browser;
}

Expand All @@ -99,7 +99,7 @@ export abstract class BrowserTypeBase implements BrowserType {
options = validateLaunchOptions(options);
const persistent = validateBrowserContextOptions(options);
const loggers = new Loggers(options.logger);
const browser = await runAbortableTask(progress => this._innerLaunch(progress, options, loggers, persistent, userDataDir), loggers.browser, TimeoutSettings.timeout(options), 'browserType.launchPersistentContext');
const browser = await runAbortableTask(progress => this._innerLaunch(progress, options, loggers, persistent, userDataDir), loggers.browser, TimeoutSettings.timeout(options), 'browserType.launchPersistentContext').catch(e => { throw this._rewriteStartupError(e); });
return browser._defaultContext!;
}

Expand Down Expand Up @@ -240,6 +240,7 @@ export abstract class BrowserTypeBase implements BrowserType {
abstract _startWebSocketServer(transport: ConnectionTransport, logger: Logger, port: number): WebSocketServer;
abstract _amendEnvironment(env: Env, userDataDir: string, executable: string, browserArguments: string[]): Env;
abstract _amendArguments(browserArguments: string[]): string[];
abstract _rewriteStartupError(error: Error): Error;
abstract _attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void;
}

Expand Down
16 changes: 16 additions & 0 deletions src/server/chromium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { CRBrowser } from '../chromium/crBrowser';
import * as ws from 'ws';
import { Env } from './processLauncher';
import { kBrowserCloseMessageId } from '../chromium/crConnection';
import { rewriteErrorMessage } from '../utils/stackTrace';
import { BrowserTypeBase } from './browserType';
import { ConnectionTransport, ProtocolRequest, ProtocolResponse } from '../transport';
import { Logger } from '../logger';
Expand Down Expand Up @@ -63,6 +64,20 @@ export class Chromium extends BrowserTypeBase {
return CRBrowser.connect(transport, options, devtools);
}

_rewriteStartupError(error: Error): Error {
if (!error.message.includes('--no-sandbox'))
return error;
return rewriteErrorMessage(error, [
`Chromium sandboxing failed!`,
`================================`,
`To workaround sandboxing issues, do either of the following:`,
` - (preferred): Configure environment to support sandboxing: https://github.com/microsoft/playwright/blob/master/docs/troubleshooting.md`,
` - (alternative): Launch Chromium without sandbox using 'chromiumSandbox: false' option`,
`================================`,
``,
].join('\n'));
}

_amendEnvironment(env: Env, userDataDir: string, executable: string, browserArguments: string[]): Env {
return env;
}
Expand All @@ -71,6 +86,7 @@ export class Chromium extends BrowserTypeBase {
// We currently only support Linux.
if (os.platform() !== 'linux')
return browserArguments;

// If there's already --no-sandbox passed in, do nothing.
if (browserArguments.indexOf('--no-sandbox') !== -1)
return browserArguments;
Expand Down
4 changes: 4 additions & 0 deletions src/server/firefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class Firefox extends BrowserTypeBase {
return FFBrowser.connect(transport, options);
}

_rewriteStartupError(error: Error): Error {
return error;
}

_amendEnvironment(env: Env, userDataDir: string, executable: string, browserArguments: string[]): Env {
return os.platform() === 'linux' ? {
...env,
Expand Down
4 changes: 4 additions & 0 deletions src/server/webkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export class WebKit extends BrowserTypeBase {
return browserArguments;
}

_rewriteStartupError(error: Error): Error {
return error;
}

_attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void {
transport.send({method: 'Playwright.close', params: {}, id: kBrowserCloseMessageId});
}
Expand Down

0 comments on commit 18bde71

Please sign in to comment.