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
Returned by Browser.initEvents (during NewBrowser setup) when the CDP call target.SetAutoAttach(true, true).WithFlatten(true) fails. Auto-attach is what lets k6 receive events for new pages/targets, so its failure during setup aborts browser creation. The 'internal error' prefix signals causes outside the script's control: a broken/closing connection or a Chrome that rejects the call.
Source
Thrown at internal/js/modules/k6/browser/common/browser.go:254
if ev, ok := event.data.(*target.EventAttachedToTarget); ok {
b.logger.Debugf("Browser:initEvents:onAttachedToTarget", "sid:%v tid:%v", ev.SessionID, ev.TargetInfo.TargetID)
if err := b.onAttachedToTarget(ev); err != nil {
k6ext.Panicf(b.vuCtx, "browser is attaching to target: %w", err)
}
} else if ev, ok := event.data.(*target.EventDetachedFromTarget); ok {
b.logger.Debugf("Browser:initEvents:onDetachedFromTarget", "sid:%v", ev.SessionID)
b.onDetachedFromTarget(ev)
} else if event.typ == EventConnectionClose {
b.logger.Debugf("Browser:initEvents:EventConnectionClose", "")
return
}
}
}
}()
action := target.SetAutoAttach(true, true).WithFlatten(true)
if err := action.Do(cdp.WithExecutor(b.vuCtx, b.conn)); err != nil {
return fmt.Errorf("internal error while auto-attaching to browser pages: %w", err)
}
// Target.setAutoAttach has a bug where it does not wait for new Targets being attached.
// However making a dummy call afterwards fixes this.
// This can be removed after https://chromium-review.googlesource.com/c/chromium/src/+/2885888 lands in stable.
action2 := target.GetTargetInfo()
if _, err := action2.Do(cdp.WithExecutor(b.vuCtx, b.conn)); err != nil {
return fmt.Errorf("internal error while getting browser target info: %w", err)
}
return nil
}
// connectionOnAttachedToTarget is called when Connection receives an attachedToTarget
// event. Returning false will stop the event from being processed by the connection.
func (b *Browser) connectionOnAttachedToTarget(eva *target.EventAttachedToTarget) bool {
// This allows to attach targets to the same browser context as the current
// one, and to the default browser context.View on GitHub (pinned to 93accf6570)
Solutions
- Verify the remote Chrome instance stays alive through the whole connect (no restarts/orchestrator kills)
- Use a Chrome/Chromium version compatible with your k6 version (modern stable Chrome)
- Raise K6_BROWSER_TIMEOUT so setup CDP calls are not cut off on slow links
- Remove WebSocket-hostile proxies between k6 and the browser
- Retry the launch/connect once; transient mid-setup drops often succeed on retry
Example fix
# before K6_BROWSER_TIMEOUT=15s k6 run test.js # after K6_BROWSER_TIMEOUT=60s k6 run test.js
Defensive patterns
Strategy: retry
Try / catch
try {
const browser = chromium.connectOverCDP(wsUrl);
} catch (e) {
const m = String(e.message);
if (m.includes('auto-attaching to browser pages')) {
// setup-time CDP failure: retry with a fresh connection and a stable browser instance
console.error('SetAutoAttach failed during setup; check browser stability and timeout:', m);
}
throw e;
} Prevention
- Keep the remote Chrome instance alive and un-recycled through connect
- Use a recent stable Chrome compatible with k6's CDP layer
- Raise K6_BROWSER_TIMEOUT on slow links so setup calls complete
When it happens
Trigger: The CDP WebSocket connection drops between connect and this call (remote Chrome restarting); Chrome version does not support flatten/auto-attach semantics k6 uses; connection closed due to timeout during setup; another debugger client detached the browser.
Common situations: Remote browser pools recycling instances mid-connect; very old Chromium builds predating flattened sessions; proxies killing the WebSocket early; rarely on local stock Chrome — if reproducible there, suspect resource starvation slowing setup past the timeout.
Related errors
- browser connect: %w
- internal error while getting browser target info: %w
- initializing networking %T: %w
- can't fetch the page for unknown reason
- errorText
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/7a902f4fff3b6a78.
Report an issue: GitHub.