grafana/k6 · error
clearing cookies: %w
Error message
clearing cookies: %w
What it means
Thrown by BrowserContext.ClearCookies when the CDP Storage.clearCookies command fails for this browser context. The method has no local validation; it solely reports whether Chrome accepted the command, so the wrapped error is a protocol/connection failure.
Source
Thrown at internal/js/modules/k6/browser/common/browser_context.go:513
setCookies := storage.
SetCookies(cookiesToSet).
WithBrowserContextID(b.id)
if err := setCookies.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil {
return fmt.Errorf("cannot set cookies: %w", err)
}
return nil
}
// ClearCookies clears cookies.
func (b *BrowserContext) ClearCookies() error {
b.logger.Debugf("BrowserContext:ClearCookies", "bctxid:%v", b.id)
clearCookies := storage.
ClearCookies().
WithBrowserContextID(b.id)
if err := clearCookies.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil {
return fmt.Errorf("clearing cookies: %w", err)
}
return nil
}
// Cookies returns all cookies.
// Some of them can be added with the AddCookies method and some of them are
// automatically taken from the browser context when it is created. And some of
// them are set by the page, i.e., using the Set-Cookie HTTP header or via
// JavaScript like document.cookie.
func (b *BrowserContext) Cookies(urls ...string) ([]*Cookie, error) {
b.logger.Debugf("BrowserContext:Cookies", "bctxid:%v", b.id)
// get cookies from this browser context.
getCookies := storage.
GetCookies().
WithBrowserContextID(b.id)
networkCookies, err := getCookies.Do(
cdp.WithExecutor(b.ctx, b.browser.conn),View on GitHub (pinned to 93accf6570)
Solutions
- Call clearCookies only while the context is open, before context.close()/browser.close().
- Treat 'Target closed'/'disconnected' wrapped errors as benign during teardown and skip retrying.
- Enable debug logging to capture the underlying CDP error if the failure is persistent.
Example fix
// before // ... in teardown, after browser already closed: context.clearCookies(); // throws // after context.clearCookies(); context.close();
Defensive patterns
Strategy: try-catch
Try / catch
try {
context.clearCookies();
} catch (e) {
if (/Target closed|disconnected/.test(e.message)) {
// teardown race: context already gone, safe to ignore
} else {
throw e;
}
} Prevention
- Clear cookies before closing the context, not in post-close cleanup.
- Make teardown idempotent so a failed clearCookies doesn't cascade.
- Don't retry against a dead context — recreate it instead.
When it happens
Trigger: Calling browserContext.clearCookies() after the context (or the whole browser) has been closed, or while the CDP websocket to the browser is broken; occasionally when the browser is mid-shutdown during test teardown.
Common situations: Cleanup code running in a finally/teardown path after the browser already closed; test abort racing the clearCookies call; CI containers killing Chrome on timeout before cleanup finishes.
Related errors
- retrieving cookies: %w
- disposing browser context: %w
- cannot set cookies: %w
- adding k6 object to new browser context: %w
- browser connect: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/477aadfead88e24a.
Report an issue: GitHub.