{"record":{"id":"cf9c1286b112ea59","repo":"grafana/k6","slug":"setting-offline-status-to-t-for-the-browser-conte","errorCode":null,"errorMessage":"setting offline status to %t for the browser context ID %s: %w","messagePattern":"setting offline status to %t for the browser context ID (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":336,"sourceCode":"\n\tb.opts.HTTPCredentials = hc\n\tfor _, p := range b.browser.getPages() {\n\t\tif err := p.updateHTTPCredentials(); err != nil {\n\t\t\treturn fmt.Errorf(\"setting HTTP credentials in target ID %s: %w\", p.targetID, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// SetOffline toggles the browser's connectivity on/off.\nfunc (b *BrowserContext) SetOffline(offline bool) error {\n\tb.logger.Debugf(\"BrowserContext:SetOffline\", \"bctxid:%v offline:%t\", b.id, offline)\n\n\tb.opts.Offline = offline\n\tfor _, p := range b.browser.getPages() {\n\t\tif err := p.updateOffline(); err != nil {\n\t\t\treturn fmt.Errorf(\n\t\t\t\t\"setting offline status to %t for the browser context ID %s: %w\",\n\t\t\t\toffline, b.id, err,\n\t\t\t)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// Timeout will return the default timeout or the one set by the user.\nfunc (b *BrowserContext) Timeout() time.Duration {\n\treturn b.timeoutSettings.timeout()\n}\n\n// WaitForEvent waits for event.\nfunc (b *BrowserContext) WaitForEvent(event string, f func(p *Page) (bool, error), timeout time.Duration) (any, error) {\n\tb.logger.Debugf(\"BrowserContext:WaitForEvent\", \"bctxid:%v event:%q\", b.id, event)\n","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L318-L354","documentation":"SetOffline stored the offline flag on the context, then ran updateOffline (Network.emulateNetworkConditions) on every existing page; one page rejected the command. The message records both the requested state (%t: true/false) and the context ID, and the wrapped error is the per-page CDP failure.","triggerScenarios":"browserContext.setOffline(true) (or false) while a page in the context is closed/closing, its target crashed, or the browser WebSocket connection is down.","commonSituations":"Offline/online simulation mid-test racing page teardown; toggling offline in teardown; a crashed tab from an earlier part of the run still registered in the context.","solutions":["Set offline at context creation with browser.newContext({ offline: true }) instead of toggling later","Call setOffline only when all pages in the context are alive and not being closed","Reopen pages after toggling if a page is stale, then retry","If the wrapped error shows a dead connection, restart the browser rather than retrying"],"exampleFix":"// before\nconst ctx = browser.newContext();\nconst page = ctx.newPage();\n// page crashed/closed meanwhile\nctx.setOffline(true); // fails updating that page\n\n// after\nconst ctx = browser.newContext({ offline: true });\nconst page = ctx.newPage();\n// or toggle later only while pages are alive:\n// ctx.setOffline(false);","handlingStrategy":"try-catch","validationCode":"// deterministic offline simulation: set at context creation\nconst ctx = browser.newContext({ offline: true });\nconst page = ctx.newPage();\n// later: ctx.setOffline(false) only while all pages are alive","typeGuard":null,"tryCatchPattern":"try {\n  ctx.setOffline(true);\n} catch (e) {\n  if (String(e.message).includes('setting offline status')) {\n    // stale page session: close pages, toggle, then reopen what you need\n    for (const p of ctx.pages()) { try { p.close(); } catch (_) {} }\n    ctx.setOffline(true);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Use the offline context option for a known-good starting state","Only toggle offline when no page in the context is being closed concurrently","Reopen pages after toggling rather than keeping stale ones around"],"tags":["browser","cdp","offline","network-emulation"],"backgroundTag":"cdp-protocol-error","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}