{"record":{"id":"77fde1b4f3706610","repo":"grafana/k6","slug":"internal-error-while-auto-attaching-to-browser-pag-77fde1","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/page.go","lineNumber":354,"sourceCode":"\tp.frameManager = NewFrameManager(ctx, s, &p, p.timeoutSettings, p.logger)\n\tp.mainFrameSession, err = NewFrameSession(p.ctx, p.teardownCtx, s, &p, nil, tid, p.logger, true)\n\tif err != nil {\n\t\tp.logger.Debugf(\"Page:NewPage:NewFrameSession:return\", \"sid:%v tid:%v err:%v\",\n\t\t\tp.sessionID(), tid, err)\n\n\t\treturn nil, err\n\t}\n\tp.frameSessionsMu.Lock()\n\tp.frameSessions[cdp.FrameID(tid)] = p.mainFrameSession\n\tp.frameSessionsMu.Unlock()\n\tp.Mouse = NewMouse(ctx, s, p.frameManager.MainFrame(), bctx.timeoutSettings, p.Keyboard)\n\tp.Touchscreen = NewTouchscreen(ctx, s, p.Keyboard)\n\n\tp.initEvents()\n\n\taction := target.SetAutoAttach(true, true).WithFlatten(true)\n\tif err := action.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {\n\t\treturn nil, fmt.Errorf(\"internal error while auto attaching to browser pages: %w\", err)\n\t}\n\n\tadd := runtime.AddBinding(webVitalBinding)\n\tif err := add.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {\n\t\treturn nil, fmt.Errorf(\"internal error while adding binding to page: %w\", err)\n\t}\n\n\tif err := bctx.applyAllInitScripts(&p); err != nil {\n\t\treturn nil, fmt.Errorf(\"internal error while applying init scripts to page: %w\", err)\n\t}\n\n\treturn &p, nil\n}\n\nfunc (p *Page) initEvents() {\n\tp.logger.Debugf(\"Page:initEvents\",\n\t\t\"sid:%v tid:%v\", p.session.ID(), p.targetID)\n","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/page.go#L336-L372","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check for an earlier crash/disconnect in the logs — this error is a symptom; fix the root cause (memory, binary, network to the browser)","Create pages early in the iteration and close them explicitly before it ends, avoiding creation/teardown races","If using a remote browser, verify the endpoint URL and its availability/stability","Reproduce with DEBUG=k6-browser to see the underlying CDP error, and report/version-upgrade if it persists on a healthy browser"],"exampleFix":"// before\nexport default async function () {\n  // ... hours of work ...\n  const page = await browser.newPage(); // races iteration teardown\n}\n\n// after\nexport default async function () {\n  const page = await browser.newPage(); // create early\n  await page.goto('https://test.k6.io/');\n  await page.close(); // close deterministically\n}","handlingStrategy":"retry","validationCode":"// Cheap guard: ensure browser is usable before creating pages\nif (browser === undefined) { throw new Error('browser module unavailable in this k6 build'); }","typeGuard":null,"tryCatchPattern":"async function newPageSafe(browser, tries = 2) {\n  for (let i = 0; i < tries; i++) {\n    try { return await browser.newPage(); }\n    catch (e) {\n      if (!/auto attaching to browser pages/.test(String(e)) || i === tries - 1) throw e;\n    }\n  }\n}","preventionTips":["Create pages at the start of the iteration; never race creation with teardown","Close pages explicitly before the iteration ends","On recurrence, read earlier log lines: this wraps a browser crash/disconnect that must be fixed first"],"tags":["cdp","browser","page-creation","target","lifecycle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}