grafana/k6 · error
setting offline status to %t for the browser context ID %s:
Error message
setting offline status to %t for the browser context ID %s: %w
What it means
Thrown by BrowserContext.SetOffline when toggling offline mode fails for one of the open pages. The method stores the flag in the context options, then calls updateOffline on every page, which sends a Network emulation CDP command to each frame session; a failing CDP call is wrapped with the requested offline value and the browser context ID.
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 93accf6570)
Solutions
- Make sure the browser context and all its pages are still open when calling setOffline (don't call it after page.close() or during teardown).
- Check k6 debug logs for the underlying CDP error to see whether the browser target died ('Target closed', 'websocket disconnected') and restart the browser if so.
- If the failure is a race with closing pages, restructure the script to toggle offline before opening pages or after all pages you need are confirmed open.
Example fix
// before page.close(); context.setOffline(true); // page target already gone // after context.setOffline(true); // ... run offline assertions ... context.setOffline(false); page.close();
Defensive patterns
Strategy: try-catch
Try / catch
try {
context.setOffline(true);
} catch (e) {
if (/Target closed|disconnected/.test(e.message)) {
// browser/page gone: recreate context instead of retrying
} else {
throw e;
}
} Prevention
- Toggle offline only while the context and all pages are open.
- Order teardown so offline toggles happen before close() calls.
- Log the wrapped CDP error to distinguish lifecycle failures from emulation failures.
When it happens
Trigger: Calling browserContext.setOffline(true) or setOffline(false) when the CDP connection to a page's frame session is dead: browser crashed or is shutting down, target was closed concurrently, or the websocket was dropped. The offline flag itself is validated only by whether the underlying emulation command succeeds.
Common situations: Offline-mode tests that call setOffline around navigation steps and race with page.close() or test teardown; running headless Chrome in resource-starapped containers where the target process dies; calling setOffline after the browser context has already been disposed.
Related errors
- updating offline mode for frame %v: %w
- errorText
- adding k6 object to new browser context: %w
- connecting to browser: %w
- connecting to browser DevTools URL: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/cf9c1286b112ea59.
Report an issue: GitHub.