{"record":{"id":"81275bd4ef4c7138","repo":"grafana/k6","slug":"emulating-network-conditions-w","errorCode":null,"errorMessage":"emulating network conditions: %w","messagePattern":"emulating network conditions: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/network_manager.go","lineNumber":1061,"sourceCode":"\n\treturn nil\n}\n\n// SetOfflineMode toggles offline mode on/off.\nfunc (m *NetworkManager) SetOfflineMode(offline bool) error {\n\tif m.offline == offline {\n\t\treturn nil\n\t}\n\tm.offline = offline\n\n\taction := network.EmulateNetworkConditions(\n\t\tm.offline,\n\t\tm.networkProfile.Latency,\n\t\tm.networkProfile.Download,\n\t\tm.networkProfile.Upload,\n\t)\n\tif err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {\n\t\treturn fmt.Errorf(\"emulating network conditions: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// ThrottleNetwork changes the network attributes in chrome to simulate slower\n// networks e.g. a slow 3G connection.\nfunc (m *NetworkManager) ThrottleNetwork(networkProfile NetworkProfile) error {\n\tif m.networkProfile == networkProfile {\n\t\treturn nil\n\t}\n\tm.networkProfile = networkProfile\n\n\taction := network.EmulateNetworkConditions(\n\t\tm.offline,\n\t\tm.networkProfile.Latency,\n\t\tm.networkProfile.Download,\n\t\tm.networkProfile.Upload,","sourceCodeStart":1043,"sourceCodeEnd":1079,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/network_manager.go#L1043-L1079","documentation":"The CDP command Network.emulateNetworkConditions failed while toggling offline mode. SetOfflineMode is a no-op when the state is unchanged; the error means the command itself could not be delivered or executed on the page's session.","triggerScenarios":"browserContext.setOffline(true/false) (or creating a context with offline: true) when the page session is closed, the browser has crashed, or the target is being destroyed — e.g. toggling offline inside a handler or after close started.","commonSituations":"Testing offline behavior by flipping setOffline around actions, racing page close at iteration end; offline toggles in loops for flaky-network simulation on a target that navigated cross-process.","solutions":["Toggle offline while the page is definitely open, before navigation or after it settles, not around close()","Do the flip synchronously and await it before issuing goto()/reload()","If it recurs, look upstream in the logs for the browser/session failure that made the command undeliverable","Consider context-level network emulation options instead of repeated mid-flight toggles"],"exampleFix":"// before\nawait ctx.setOffline(true);\nawait page.goto('https://test.k6.io/'); // offline already raced a closed target\n\n// after\nawait ctx.setOffline(true);\nawait page.reload(); // page/target alive; emulate applies to live session","handlingStrategy":"try-catch","validationCode":"if (page.isClosed()) { /* skip: toggling offline on a dead session will fail */ }","typeGuard":null,"tryCatchPattern":"try {\n  await ctx.setOffline(true);\n} catch (e) {\n  if (/emulating network conditions/.test(String(e))) return;\n  throw e;\n}","preventionTips":["Toggle offline only while pages in the context are open and idle","Avoid toggling inside event handlers or near close()","Pair each setOffline(true) with a deterministic setOffline(false) before iteration end"],"tags":["cdp","browser","offline","network-emulation","lifecycle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}