{"record":{"id":"477aadfead88e24a","repo":"grafana/k6","slug":"clearing-cookies-w","errorCode":null,"errorMessage":"clearing cookies: %w","messagePattern":"clearing cookies: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":513,"sourceCode":"\tsetCookies := storage.\n\t\tSetCookies(cookiesToSet).\n\t\tWithBrowserContextID(b.id)\n\tif err := setCookies.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil {\n\t\treturn fmt.Errorf(\"cannot set cookies: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// ClearCookies clears cookies.\nfunc (b *BrowserContext) ClearCookies() error {\n\tb.logger.Debugf(\"BrowserContext:ClearCookies\", \"bctxid:%v\", b.id)\n\n\tclearCookies := storage.\n\t\tClearCookies().\n\t\tWithBrowserContextID(b.id)\n\tif err := clearCookies.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil {\n\t\treturn fmt.Errorf(\"clearing cookies: %w\", err)\n\t}\n\treturn nil\n}\n\n// Cookies returns all cookies.\n// Some of them can be added with the AddCookies method and some of them are\n// automatically taken from the browser context when it is created. And some of\n// them are set by the page, i.e., using the Set-Cookie HTTP header or via\n// JavaScript like document.cookie.\nfunc (b *BrowserContext) Cookies(urls ...string) ([]*Cookie, error) {\n\tb.logger.Debugf(\"BrowserContext:Cookies\", \"bctxid:%v\", b.id)\n\n\t// get cookies from this browser context.\n\tgetCookies := storage.\n\t\tGetCookies().\n\t\tWithBrowserContextID(b.id)\n\tnetworkCookies, err := getCookies.Do(\n\t\tcdp.WithExecutor(b.ctx, b.browser.conn),","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L495-L531","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n// ... in teardown, after browser already closed:\ncontext.clearCookies(); // throws\n\n// after\ncontext.clearCookies();\ncontext.close();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  context.clearCookies();\n} catch (e) {\n  if (/Target closed|disconnected/.test(e.message)) {\n    // teardown race: context already gone, safe to ignore\n  } else {\n    throw e;\n  }\n}","preventionTips":["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."],"tags":["cookies","cdp","browser-context","lifecycle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}