grafana/k6 · error

browser connect: %w

Error message

browser connect: %w

What it means

Returned by Browser.connect() when NewBrowserContext fails while creating the browser's internal default context (the implicit context every browser has before users call browser.newContext()). This happens immediately after the CDP connection is established, so it represents early per-context setup failing — option defaults being rejected or context creation CDP errors — rather than anything the script did with contexts.

Source

Thrown at internal/js/modules/k6/browser/common/browser.go:166

	// from doing unnecessary work.
	//
	// We need the connection to shutdown when browser.Close is called.
	// This is why we're using the internal context.
	var err error
	b.conn, err = NewConnection(
		b.browserCtx,
		b.browserProc.WsURL(),
		b.logger,
		b.connectionOnAttachedToTarget,
	)
	if err != nil {
		return fmt.Errorf("connecting to browser DevTools URL: %w", err)
	}

	// We don't need to lock this because `connect()` is called only in NewBrowser
	b.defaultContext, err = NewBrowserContext(b.vuCtx, b, "", DefaultBrowserContextOptions(), b.logger)
	if err != nil {
		return fmt.Errorf("browser connect: %w", err)
	}
	b.runOnClose = append(b.runOnClose, b.defaultContext.cleanup)

	return b.initEvents()
}

func (b *Browser) disposeContext(id cdp.BrowserContextID) error {
	b.logger.Debugf("Browser:disposeContext", "bctxid:%v", id)

	action := target.DisposeBrowserContext(id)
	if err := action.Do(cdp.WithExecutor(b.vuCtx, b.conn)); err != nil {
		return fmt.Errorf("disposing browser context ID %s: %w", id, err)
	}
	b.context = nil

	return nil
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Use a mainstream Chrome/Chromium version compatible with your k6 release (check k6 browser docs for supported versions)
  2. Retry once — transient connection drops during setup can surface here
  3. Run with K6_BROWSER_DEBUG=true and capture the wrapped cause; report to k6 if it reproduces on stock Chrome
  4. If on a custom Chromium fork, switch to standard Chrome to isolate the incompatibility
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const browser = chromium.launch();
} catch (e) {
  const m = String(e.message);
  if (m.includes('browser connect')) {
    // default-context creation failed during setup; usually version drift or a flaky connection
    console.error('Default browser context failed to initialize; try a standard Chrome build and retry:', m);
  }
  throw e;
}

Prevention

When it happens

Trigger: DefaultBrowserContextOptions() producing options the current Chrome rejects (version drift between k6's CDP layer and an unusual Chromium build); internal CDP errors while constructing the default context object on a flaky connection. Not directly triggerable by user API calls since the default context is created automatically.

Common situations: Exotic Chromium forks or very old/new Chrome versions whose context handling differs from what k6's bundled cdproto expects; connections that drop right after handshake; rarely seen on stock Chrome — treat as an environment/version mismatch signal.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/f1d0e8ad062b2b81. Report an issue: GitHub.