{"record":{"id":"e18f163ac632c3d7","repo":"grafana/k6","slug":"creating-a-new-s-w","errorCode":null,"errorMessage":"creating a new %s: %w","messagePattern":"creating a new (.+?): %w","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser.go","lineNumber":325,"sourceCode":"\t)\n\t// Opener is nil for the initial page.\n\tif isPage {\n\t\tb.pagesMu.RLock()\n\t\tif t, ok := b.pages[targetPage.OpenerID]; ok {\n\t\t\topener = t\n\t\t}\n\t\tb.pagesMu.RUnlock()\n\t}\n\tp, err := NewPage(b.vuCtx, session, browserCtx, targetPage.TargetID, opener, isPage, b.logger)\n\tif err != nil && b.isPageAttachmentErrorIgnorable(ev, session, err) {\n\t\t// Always release: isPageAttachmentErrorIgnorable can also return\n\t\t// true when only this VU's context ended, while the browser\n\t\t// instance stays alive and shared with other VUs.\n\t\tdetachSession(session)\n\t\treturn nil // Ignore this page.\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"creating a new %s: %w\", targetPage.Type, err)\n\t}\n\n\t// This prevents a race where Close() sets closing and snapshots\n\t// pages, but a new page is inserted outside that snapshot.\n\tif err := b.attachNewPage(p, ev); err != nil {\n\t\tif !errors.Is(err, errBrowserClosing) {\n\t\t\treturn fmt.Errorf(\"attaching new page: %w\", err)\n\t\t}\n\n\t\tb.logger.Debugf(\n\t\t\t\"Browser:onAttachedToTarget\",\n\t\t\t\"rejected page attachment; browser is closing: sid:%v tid:%v\",\n\t\t\tev.SessionID, ev.TargetInfo.TargetID,\n\t\t)\n\t\tif closeErr := p.Close(); closeErr != nil {\n\t\t\tb.logger.Debugf(\n\t\t\t\t\"Browser:onAttachedToTarget\",\n\t\t\t\t\"closing rejected page: %v\", closeErr,","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/browser.go#L307-L343","documentation":"When Chromium attaches a new target (page or background_page, and filtered types such as iframes/workers), onAttachedToTarget builds a Page object for it via NewPage. This error wraps NewPage failing; %s is the CDP target type string (e.g. \"page\"). Typical root causes are a target that is torn down as fast as it appears, or page-initialization CDP calls exceeding the browser timeout.","triggerScenarios":"A popup or window.open target attaching and closing immediately; service workers/iframes attaching near iteration teardown; NewPage's internal CDP round-trips exceeding the browser 'timeout' option under heavy load; attaching while the browser is being closed (the closing case is separately ignored).","commonSituations":"Navigations that trigger popups the script never handles; sites spawning many iframes/workers at the end of a test; overloaded browsers where initialization exceeds the default 30s timeout.","solutions":["Raise the browser timeout option, e.g. options.browser = { timeout: '60s' }","Close or ignore popups you do not use instead of letting them attach during teardown","Retry the navigation/step that triggered the transient target","Reduce per-browser page/iframe churn or increase browser memory if targets fail under pressure"],"exampleFix":"// before\nexport const options = { browser: { type: 'chromium' } }; // default 30s timeout\n\n// after\nexport const options = {\n  browser: { type: 'chromium', timeout: '60s' },\n};","handlingStrategy":"retry","validationCode":"// Guard popup-heavy navigation with an explicit timeout budget\nexport const options = { browser: { type: 'chromium', timeout: '60s' } };","typeGuard":null,"tryCatchPattern":"try {\n  await page.goto(url);\n} catch (e) {\n  if (/creating a new (page|background_page)/.test(String(e))) {\n    // transient target attach failure (popup appeared and vanished)\n    await sleep(500);\n    await page.goto(url); // retry once\n  } else throw e;\n}","preventionTips":["Set an explicit, generous browser timeout option for popup/iframe-heavy apps","Close unhandled popups promptly instead of leaving them to attach during teardown","Avoid navigating right at iteration boundaries"],"tags":["cdp","page","popup","iframe","timeout","browser"],"backgroundTag":"page-initialization-failed","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"}