{"record":{"id":"7a902f4fff3b6a78","repo":"grafana/k6","slug":"internal-error-while-auto-attaching-to-browser-pag","errorCode":null,"errorMessage":"internal error while auto-attaching to browser pages: %w","messagePattern":"internal error while auto-attaching to browser pages: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/js/modules/k6/browser/common/browser.go","lineNumber":252,"sourceCode":"\t\t\t\tif ev, ok := event.data.(*target.EventAttachedToTarget); ok {\n\t\t\t\t\tb.logger.Debugf(\"Browser:initEvents:onAttachedToTarget\", \"sid:%v tid:%v\", ev.SessionID, ev.TargetInfo.TargetID)\n\t\t\t\t\tif err := b.onAttachedToTarget(ev); err != nil {\n\t\t\t\t\t\tk6ext.Panicf(b.vuCtx, \"browser is attaching to target: %w\", err)\n\t\t\t\t\t}\n\t\t\t\t} else if ev, ok := event.data.(*target.EventDetachedFromTarget); ok {\n\t\t\t\t\tb.logger.Debugf(\"Browser:initEvents:onDetachedFromTarget\", \"sid:%v\", ev.SessionID)\n\t\t\t\t\tb.onDetachedFromTarget(ev)\n\t\t\t\t} else if event.typ == EventConnectionClose {\n\t\t\t\t\tb.logger.Debugf(\"Browser:initEvents:EventConnectionClose\", \"\")\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}()\n\n\taction := target.SetAutoAttach(true, true).WithFlatten(true)\n\tif err := action.Do(cdp.WithExecutor(b.vuCtx, b.conn)); err != nil {\n\t\treturn fmt.Errorf(\"internal error while auto-attaching to browser pages: %w\", err)\n\t}\n\n\t// Target.setAutoAttach has a bug where it does not wait for new Targets being attached.\n\t// However making a dummy call afterwards fixes this.\n\t// This can be removed after https://chromium-review.googlesource.com/c/chromium/src/+/2885888 lands in stable.\n\taction2 := target.GetTargetInfo()\n\tif _, err := action2.Do(cdp.WithExecutor(b.vuCtx, b.conn)); err != nil {\n\t\treturn fmt.Errorf(\"internal error while getting browser target info: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// connectionOnAttachedToTarget is called when Connection receives an attachedToTarget\n// event. Returning false makes the connection release the target instead of\n// adopting it. Targets from the connection's own browser context and from\n// the default browser context are accepted.\nfunc (b *Browser) connectionOnAttachedToTarget(eva *target.EventAttachedToTarget) bool {","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/browser.go#L234-L270","documentation":"During connect(), initEvents() sends Target.setAutoAttach(autoAttach=true, waitForDebuggerOnStart=true, flatten=true) so every new target (tab, popup, iframe session) streams attachedToTarget events. This error means that CDP command failed, which almost always means the fresh connection is broken or the endpoint does not implement the expected CDP surface.","triggerScenarios":"The WS connection drops between connect and setAutoAttach; the browser process crashes or is OOM-killed at startup; K6_BROWSER_WS_URL points at a non-Chromium or partial CDP implementation that rejects auto-attach or the flatten option.","commonSituations":"Remote debugging endpoints behind proxies that kill idle/new sessions; browsers killed by cgroup/OOM limits during test startup; mismatched Chromium/headless-shell versions.","solutions":["Confirm the browser process survives startup (container logs, exit codes) and fix launch flags/memory if it crashes","Verify the WS URL targets a genuine Chromium browser-level DevTools endpoint","Use a Chromium/headless-shell version supported by the k6 browser module","Re-run with K6_BROWSER_LOG=debug to capture the underlying CDP error text"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Verify the endpoint is a Chromium browser-level DevTools endpoint before launch\nconst res = http.get(`${__ENV.K6_BROWSER_CDP}/json/version`);\nconst ok = res.status === 200 && /Chrome|HeadlessChrome/.test(res.json('Browser') || '');\nif (!ok) throw new Error('Endpoint is not a Chromium DevTools browser');","typeGuard":null,"tryCatchPattern":"try {\n  const browser = chromium.launch();\n} catch (e) {\n  if (String(e).includes('auto-attaching to browser pages')) {\n    // browser died at startup or endpoint is incompatible:\n    // check container logs/OOM, confirm Chromium version, then retry launch\n  }\n}","preventionTips":["Pin Chromium/headless-shell versions known to work with your k6 release","Keep browser memory limits above Chromium startup requirements","Avoid CDP proxies that drop sessions mid-handshake"],"tags":["cdp","browser","auto-attach","connection","startup"],"backgroundTag":"cdp-protocol-error","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}