{"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":"error","filePath":"internal/js/modules/k6/browser/common/browser.go","lineNumber":254,"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 will stop the event from being processed by the connection.\nfunc (b *Browser) connectionOnAttachedToTarget(eva *target.EventAttachedToTarget) bool {\n\t// This allows to attach targets to the same browser context as the current\n\t// one, and to the default browser context.","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser.go#L236-L272","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nK6_BROWSER_TIMEOUT=15s k6 run test.js\n\n# after\nK6_BROWSER_TIMEOUT=60s k6 run test.js","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const browser = chromium.connectOverCDP(wsUrl);\n} catch (e) {\n  const m = String(e.message);\n  if (m.includes('auto-attaching to browser pages')) {\n    // setup-time CDP failure: retry with a fresh connection and a stable browser instance\n    console.error('SetAutoAttach failed during setup; check browser stability and timeout:', m);\n  }\n  throw e;\n}","preventionTips":["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"],"tags":["cdp","internal","setup","network","version-compatibility"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}