{"record":{"id":"1f7539650d4605de","repo":"grafana/k6","slug":"cannot-set-cookies-w","errorCode":null,"errorMessage":"cannot set cookies: %w","messagePattern":"cannot set cookies: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":499,"sourceCode":"\t\t}\n\t\tcookiesToSet = append(cookiesToSet, &network.CookieParam{\n\t\t\tName:     c.Name,\n\t\t\tValue:    c.Value,\n\t\t\tDomain:   c.Domain,\n\t\t\tPath:     c.Path,\n\t\t\tURL:      c.URL,\n\t\t\tExpires:  ts,\n\t\t\tHTTPOnly: c.HTTPOnly,\n\t\t\tSecure:   c.Secure,\n\t\t\tSameSite: network.CookieSameSite(c.SameSite),\n\t\t})\n\t}\n\n\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","sourceCodeStart":481,"sourceCodeEnd":517,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L481-L517","documentation":"Thrown by BrowserContext.AddCookies when the CDP Storage.setCookies command fails after local validation passed. The context ID is attached and the underlying CDP error is wrapped, so the actual cause (protocol error) is in the tail of the message. This is a runtime browser-side failure, not an input-shape problem.","triggerScenarios":"The DevTools connection rejecting the cookie set: browser context already closed (context.close() raced), browser process crashed or disconnected, or a cookie that is structurally valid to k6 but rejected by Chrome (e.g. invalid sameSite value for a cookie without the Secure flag, domain mismatch semantics, or an expires value Chrome refuses).","commonSituations":"Calling addCookies on a context that was closed earlier in the test; headless Chrome dying under memory pressure in CI; setting SameSite=None cookies without secure:true, which modern Chrome rejects; intermittent CDP websocket drops in long test runs.","solutions":["Read the wrapped CDP error text: 'Target closed'/'browser disconnected' points to lifecycle issues; a cookie-policy message points to the cookie fields.","Ensure the context is still open and the browser healthy before the call; re-create the context if it was closed.","For SameSite cookies, set sameSite: 'None' together with secure: true, or use 'Lax'/'Strict'.","Retry once after re-establishing the context if the failure is a transient CDP drop."],"exampleFix":"// before\ncontext.addCookies([{ name: 'tracking', value: '1', domain: 'example.com', path: '/', sameSite: 'None' }]);\n\n// after\ncontext.addCookies([{ name: 'tracking', value: '1', domain: 'example.com', path: '/', sameSite: 'None', secure: true }]);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  context.addCookies(cookies);\n} catch (e) {\n  if (/Target closed|disconnected/.test(e.message)) {\n    // context/browser gone: recreate and re-add once\n  } else if (/sameSite|SameSite|Secure/i.test(e.message)) {\n    // fix cookie flags (sameSite:'None' requires secure:true)\n  } else {\n    throw e;\n  }\n}","preventionTips":["Keep the context open and the browser healthy when installing cookies.","Pair sameSite:'None' with secure:true.","Install cookies once at context setup rather than repeatedly during the run."],"tags":["cookies","cdp","browser-context","runtime"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}