grafana/k6 · error

setting offline status to %t for the browser context ID

Error message

setting offline status to %t for the browser context ID %s: %w

What it means

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.

Solutions

  1. Set offline at context creation with browser.newContext({ offline: true }) instead of toggling later
  2. Call setOffline only when all pages in the context are alive and not being closed
  3. Reopen pages after toggling if a page is stale, then retry
  4. If the wrapped error shows a dead connection, restart the browser rather than retrying

Example fix

// before
const ctx = browser.newContext();
const page = ctx.newPage();
// page crashed/closed meanwhile
ctx.setOffline(true); // fails updating that page

// after
const ctx = browser.newContext({ offline: true });
const page = ctx.newPage();
// or toggle later only while pages are alive:
// ctx.setOffline(false);
Defensive patterns

Strategy: try-catch

Validate before calling

// deterministic offline simulation: set at context creation
const ctx = browser.newContext({ offline: true });
const page = ctx.newPage();
// later: ctx.setOffline(false) only while all pages are alive

Try / catch

try {
  ctx.setOffline(true);
} catch (e) {
  if (String(e.message).includes('setting offline status')) {
    // stale page session: close pages, toggle, then reopen what you need
    for (const p of ctx.pages()) { try { p.close(); } catch (_) {} }
    ctx.setOffline(true);
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: browserContext.setOffline(true) (or false) while a page in the context is closed/closing, its target crashed, or the browser WebSocket connection is down.

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

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/cf9c1286b112ea59. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/browser_context.go:336

	b.opts.HTTPCredentials = hc
	for _, p := range b.browser.getPages() {
		if err := p.updateHTTPCredentials(); err != nil {
			return fmt.Errorf("setting HTTP credentials in target ID %s: %w", p.targetID, err)
		}
	}

	return nil
}

// SetOffline toggles the browser's connectivity on/off.
func (b *BrowserContext) SetOffline(offline bool) error {
	b.logger.Debugf("BrowserContext:SetOffline", "bctxid:%v offline:%t", b.id, offline)

	b.opts.Offline = offline
	for _, p := range b.browser.getPages() {
		if err := p.updateOffline(); err != nil {
			return fmt.Errorf(
				"setting offline status to %t for the browser context ID %s: %w",
				offline, b.id, err,
			)
		}
	}

	return nil
}

// Timeout will return the default timeout or the one set by the user.
func (b *BrowserContext) Timeout() time.Duration {
	return b.timeoutSettings.timeout()
}

// WaitForEvent waits for event.
func (b *BrowserContext) WaitForEvent(event string, f func(p *Page) (bool, error), timeout time.Duration) (any, error) {
	b.logger.Debugf("BrowserContext:WaitForEvent", "bctxid:%v event:%q", b.id, event)

View on GitHub (pinned to 01ffac6f24)