{"record":{"id":"a9dd6cdf56ba65f9","repo":"grafana/k6","slug":"attaching-new-page-w","errorCode":null,"errorMessage":"attaching new page: %w","messagePattern":"attaching new page: %w","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/js/modules/k6/browser/common/browser.go","lineNumber":332,"sourceCode":"\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,\n\t\t\t)\n\t\t}\n\n\t\tdetachSession(session)\n\n\t\treturn nil\n\t}","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/browser.go#L314-L350","documentation":"After a new Page is created, attachNewPage registers it in the browser's pages map; the only error it can return is errBrowserClosing, and the caller explicitly filters that case out (logging and closing the page instead). This residual error therefore fires only if attachNewPage fails for a non-closing reason, which the current code cannot produce — it is a defensive near-unreachable branch guarding internal invariants.","triggerScenarios":"Not producible by current code paths: attachPage only returns errBrowserClosing, which is filtered above. Seeing it implies an internal state corruption, a k6 bug, or a forked build violating the invariant.","commonSituations":"Not seen on stock builds; if it appears, it usually accompanies a k6 regression or heavily patched fork.","solutions":["Upgrade to the latest k6 release in case it is a fixed regression","Capture K6_BROWSER_LOG=debug output and the script that triggers it","Reduce concurrency to rule out resource exhaustion corrupting teardown ordering","Open a k6 issue with the reproduction and debug log"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await page.goto(url);\n} catch (e) {\n  if (String(e).includes('attaching new page')) {\n    // internal invariant break; capture logs, retry the step once\n    await sleep(1000);\n    await page.goto(url);\n  }\n}","preventionTips":["Run the latest k6 release (defensive branch; older builds may have races)","Capture K6_BROWSER_LOG=debug output when it appears","Report reproductions upstream"],"tags":["browser","page","internal","race-condition"],"backgroundTag":"race-condition","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"}