grafana/k6 · error

retrieving cookies: %w

Error message

retrieving cookies: %w

What it means

Thrown by BrowserContext.Cookies (the cookies() API) when the CDP Storage.getCookies command fails, preventing retrieval of the context's cookies. Like the other cookie endpoints, the error wraps the underlying DevTools protocol failure with the 'retrieving cookies' prefix.

Source

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

}

// 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),
	)
	if err != nil {
		return nil, fmt.Errorf("retrieving cookies: %w", err)
	}
	// return if no cookies found so we don't have to needlessly convert them.
	// users can still work with cookies using the empty slice.
	// like this: cookies.length === 0.
	if len(networkCookies) == 0 {
		return nil, nil
	}

	// convert the received CDP cookies to the browser API format.
	cookies := make([]*Cookie, len(networkCookies))
	for i, c := range networkCookies {
		cookies[i] = &Cookie{
			Name:     c.Name,
			Value:    c.Value,
			Domain:   c.Domain,
			Path:     c.Path,
			Expires:  int64(c.Expires),
			HTTPOnly: c.HTTPOnly,

View on GitHub (pinned to 93accf6570)

Solutions

  1. Ensure the context and browser are alive when calling cookies(); capture cookies before closing anything.
  2. Check the wrapped CDP error: if it says the target/browser is gone, restart the browser rather than retrying on the dead context.
  3. Stabilize flaky environments (raise container memory, disable unnecessary pages) if Chrome is dying during the run.

Example fix

// before
await context.close();
const cookies = context.cookies(); // throws: context gone

// after
const cookies = context.cookies();
await context.close();
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const cookies = context.cookies();
} catch (e) {
  if (/Target closed|disconnected/.test(e.message)) {
    // browser died mid-run: restart browser/context before retrying
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling browserContext.cookies([urls]) on a closed context or with a dead browser connection; Chrome failing the Storage.getCookies call for the context ID, e.g. after the context was disposed concurrently from another code path.

Common situations: Harvesting cookies for reuse after navigation, but doing it in a hook that runs after teardown; long-running scripts where the browser crashed mid-test (OOM) and later calls to cookies() fail; races between context.close() and final assertions.

Related errors


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