grafana/k6 · error

emulating network conditions: %w

Error message

emulating network conditions: %w

What it means

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.

Source

Thrown at internal/js/modules/k6/browser/common/network_manager.go:1061

	return nil
}

// SetOfflineMode toggles offline mode on/off.
func (m *NetworkManager) SetOfflineMode(offline bool) error {
	if m.offline == offline {
		return nil
	}
	m.offline = offline

	action := network.EmulateNetworkConditions(
		m.offline,
		m.networkProfile.Latency,
		m.networkProfile.Download,
		m.networkProfile.Upload,
	)
	if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
		return fmt.Errorf("emulating network conditions: %w", err)
	}

	return nil
}

// ThrottleNetwork changes the network attributes in chrome to simulate slower
// networks e.g. a slow 3G connection.
func (m *NetworkManager) ThrottleNetwork(networkProfile NetworkProfile) error {
	if m.networkProfile == networkProfile {
		return nil
	}
	m.networkProfile = networkProfile

	action := network.EmulateNetworkConditions(
		m.offline,
		m.networkProfile.Latency,
		m.networkProfile.Download,
		m.networkProfile.Upload,

View on GitHub (pinned to 93accf6570)

Solutions

  1. Toggle offline while the page is definitely open, before navigation or after it settles, not around close()
  2. Do the flip synchronously and await it before issuing goto()/reload()
  3. If it recurs, look upstream in the logs for the browser/session failure that made the command undeliverable
  4. Consider context-level network emulation options instead of repeated mid-flight toggles

Example fix

// before
await ctx.setOffline(true);
await page.goto('https://test.k6.io/'); // offline already raced a closed target

// after
await ctx.setOffline(true);
await page.reload(); // page/target alive; emulate applies to live session
Defensive patterns

Strategy: try-catch

Validate before calling

if (page.isClosed()) { /* skip: toggling offline on a dead session will fail */ }

Try / catch

try {
  await ctx.setOffline(true);
} catch (e) {
  if (/emulating network conditions/.test(String(e))) return;
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/81275bd4ef4c7138. Report an issue: GitHub.