grafana/k6 · error

internal error while auto attaching to browser pages: %w

Error message

internal error while auto attaching to browser pages: %w

What it means

During page creation the CDP command Target.setAutoAttach (with flatten) failed, so chromium could not wire the new page's targets to the session. Wrapped as 'internal error' because a healthy browser should accept it; failure means the browser connection or target lifecycle is broken at NewPage time.

Source

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

	p.frameManager = NewFrameManager(ctx, s, &p, p.timeoutSettings, p.logger)
	p.mainFrameSession, err = NewFrameSession(p.ctx, p.teardownCtx, s, &p, nil, tid, p.logger, true)
	if err != nil {
		p.logger.Debugf("Page:NewPage:NewFrameSession:return", "sid:%v tid:%v err:%v",
			p.sessionID(), tid, err)

		return nil, err
	}
	p.frameSessionsMu.Lock()
	p.frameSessions[cdp.FrameID(tid)] = p.mainFrameSession
	p.frameSessionsMu.Unlock()
	p.Mouse = NewMouse(ctx, s, p.frameManager.MainFrame(), bctx.timeoutSettings, p.Keyboard)
	p.Touchscreen = NewTouchscreen(ctx, s, p.Keyboard)

	p.initEvents()

	action := target.SetAutoAttach(true, true).WithFlatten(true)
	if err := action.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {
		return nil, fmt.Errorf("internal error while auto attaching to browser pages: %w", err)
	}

	add := runtime.AddBinding(webVitalBinding)
	if err := add.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {
		return nil, fmt.Errorf("internal error while adding binding to page: %w", err)
	}

	if err := bctx.applyAllInitScripts(&p); err != nil {
		return nil, fmt.Errorf("internal error while applying init scripts to page: %w", err)
	}

	return &p, nil
}

func (p *Page) initEvents() {
	p.logger.Debugf("Page:initEvents",
		"sid:%v tid:%v", p.session.ID(), p.targetID)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Check for an earlier crash/disconnect in the logs — this error is a symptom; fix the root cause (memory, binary, network to the browser)
  2. Create pages early in the iteration and close them explicitly before it ends, avoiding creation/teardown races
  3. If using a remote browser, verify the endpoint URL and its availability/stability
  4. Reproduce with DEBUG=k6-browser to see the underlying CDP error, and report/version-upgrade if it persists on a healthy browser

Example fix

// before
export default async function () {
  // ... hours of work ...
  const page = await browser.newPage(); // races iteration teardown
}

// after
export default async function () {
  const page = await browser.newPage(); // create early
  await page.goto('https://test.k6.io/');
  await page.close(); // close deterministically
}
Defensive patterns

Strategy: retry

Validate before calling

// Cheap guard: ensure browser is usable before creating pages
if (browser === undefined) { throw new Error('browser module unavailable in this k6 build'); }

Try / catch

async function newPageSafe(browser, tries = 2) {
  for (let i = 0; i < tries; i++) {
    try { return await browser.newPage(); }
    catch (e) {
      if (!/auto attaching to browser pages/.test(String(e)) || i === tries - 1) throw e;
    }
  }
}

Prevention

When it happens

Trigger: browser.newPage()/newContext().newPage() executed when the browser is shutting down or has crashed, when the WS connection to the DevTools endpoint dropped, or when the target was destroyed between creation and the SetAutoAttach call (rapid open/close, redirects to a new process).

Common situations: Creating pages at the very end of an iteration racing teardown; chromium OOM-killed during memory-heavy tests; too many simultaneous targets; remote/browsercloud endpoint briefly unavailable; version-mismatched chrome binary.

Related errors


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