{"record":{"id":"ef7d68107ba04bd1","repo":"grafana/k6","slug":"can-t-fetch-the-page-for-unknown-reason","errorCode":null,"errorMessage":"can't fetch the page for unknown reason","messagePattern":"can't fetch the page for unknown reason","errorType":"exception","errorClass":"UserFriendlyError","httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser.go","lineNumber":564,"sourceCode":"\tcase <-waitForPage:\n\t\tb.logger.Debugf(\"Browser:newPageInContext:<-waitForPage\", \"tid:%v bctxid:%v\", tid, id)\n\t\tb.pagesMu.RLock()\n\t\tpage = b.pages[tid]\n\t\tb.pagesMu.RUnlock()\n\tcase <-ctx.Done():\n\t\tb.logger.Debugf(\"Browser:newPageInContext:<-ctx.Done\", \"tid:%v bctxid:%v err:%v\", tid, id, ContextErr(ctx))\n\t}\n\n\tif err = ContextErr(ctx); err != nil {\n\t\terr = &k6ext.UserFriendlyError{\n\t\t\tErr:     err,\n\t\t\tTimeout: b.browserOpts.Timeout,\n\t\t}\n\t}\n\n\tif err == nil && page == nil {\n\t\terr = &k6ext.UserFriendlyError{\n\t\t\tErr: errors.New(\"can't fetch the page for unknown reason\"),\n\t\t}\n\t}\n\n\treturn page, err\n}\n\n// Close shuts down the browser.\nfunc (b *Browser) Close() {\n\tif !b.closing.CompareAndSwap(false, true) {\n\t\tb.logger.Warnf(\n\t\t\t\"Browser:Close\",\n\t\t\t\"Please call browser.close only once, and do not use the browser after calling close.\",\n\t\t)\n\t\treturn\n\t}\n\t// This will help with some cleanup in the connection and event loop above in\n\t// initEvents().\n\tdefer b.browserCancelFn(errors.New(\"browser closed\"))","sourceCodeStart":546,"sourceCodeEnd":582,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser.go#L546-L582","documentation":"newPageInContext wraps CDP page creation and enforces an internal invariant: if the call returns neither an error nor a page, k6 wraps this generic UserFriendlyError ('can't fetch the page for unknown reason') instead of returning a nil page that would panic later. It typically indicates the browser died or the CDP connection dropped at exactly the wrong moment — or a k6 bug — since normal failures carry a real error.","triggerScenarios":"browser.newPage() racing browser.close() or the iteration teardown; the browser process being killed (OOM, docker stop, sandbox kills) while the page is being created; rare CDP/protocol anomalies between k6 and unusual Chrome builds.","commonSituations":"Flaky end-of-test page creation; container memory limits reaping Chrome; parallel VUs stressing a single browser; k6/Chrome version skew after an image upgrade.","solutions":["Retry browser.newPage() once — this state is almost always transient","If it recurs, recreate the whole browser (close and relaunch) inside the retry path rather than reusing the dead one","Check container/host for OOM kills (dmesg, docker events) starving Chrome","Upgrade k6 and Chrome together; if it persists, capture --log-output=stdout and report it with the script"],"exampleFix":"// before\nconst page = await browser.newPage();\n\n// after\nasync function newPageWithRetry(browser) {\n  try {\n    return await browser.newPage();\n  } catch (e) {\n    if (!e.message.includes(\"can't fetch the page\")) throw e;\n    return await browser.newPage(); // one retry\n  }\n}\nconst page = await newPageWithRetry(browser);","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"async function safeNewPage(browser, attempts = 2) {\n  let lastErr;\n  for (let i = 0; i < attempts; i++) {\n    try {\n      return await browser.newPage();\n    } catch (e) {\n      lastErr = e;\n      if (!e.message.includes(\"can't fetch the page\")) break;\n    }\n  }\n  throw lastErr;\n}","preventionTips":["Create pages early in the iteration, not concurrently with teardown/close","Give Chrome enough memory in containers so the process is not OOM-killed mid-call","Keep k6 and the browser binary updated in lockstep; pin both in CI images"],"tags":["browser","newpage","cdp","internal","race","retry"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}