grafana/k6 · error

internal error while adding binding to page: %w

Error message

internal error while adding binding to page: %w

What it means

During page creation the CDP command Runtime.addBinding — used to register the web-vitals binding so the page can report Web Vitals back to k6 — failed on the new page's session. It is an infrastructure failure at page-init time, immediately after SetAutoAttach in NewPage.

Source

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

		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)

	events := []string{
		cdproto.EventRuntimeConsoleAPICalled,
	}
	p.session.on(p.ctx, events, p.eventCh)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Treat it as a symptom: find the first CDP failure in DEBUG=k6-browser output and fix that (crash, disconnect, binary mismatch)
  2. Ensure page creation is not racing context/browser close
  3. If a remote browser is used, check its health and network path between k6 and the endpoint
  4. Upgrade k6 so module and bundled chromium stay compatible
Defensive patterns

Strategy: retry

Try / catch

// Same pattern as page creation: bounded retry on transient session failure
try {
  page = await browser.newPage();
} catch (e) {
  if (/adding binding to page/.test(String(e))) {
    page = await browser.newPage(); // one retry on a fresh target
  } else throw e;
}

Prevention

When it happens

Trigger: Same class as 953: page creation proceeding while the target/session dies — browser crash, connection loss, or the target destroyed between creation and AddBinding. Any page creation after the browser module's connection is already broken hits this.

Common situations: First page creation after chromium died in a previous iteration but the module reused the connection; page churn racing close at iteration end; flaky remote browser connections.

Related errors


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