{"record":{"id":"dd82c57938a81323","repo":"grafana/k6","slug":"clearing-permissions-w","errorCode":null,"errorMessage":"clearing permissions: %w","messagePattern":"clearing permissions: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":199,"sourceCode":"\t\t\treturn fmt.Errorf(\"adding init script to browser context: %w\", err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// Browser returns the browser instance that this browser context belongs to.\nfunc (b *BrowserContext) Browser() *Browser {\n\treturn b.browser\n}\n\n// ClearPermissions clears any permission overrides.\nfunc (b *BrowserContext) ClearPermissions() error {\n\tb.logger.Debugf(\"BrowserContext:ClearPermissions\", \"bctxid:%v\", b.id)\n\n\taction := cdpbrowser.ResetPermissions().WithBrowserContextID(b.id)\n\tif err := action.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil {\n\t\treturn fmt.Errorf(\"clearing permissions: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// Close shuts down the browser context.\nfunc (b *BrowserContext) Close() error {\n\tb.logger.Debugf(\"BrowserContext:Close\", \"bctxid:%v\", b.id)\n\n\tif b.id == \"\" {\n\t\treturn fmt.Errorf(\"default browser context can't be closed\")\n\t}\n\tif err := b.browser.disposeContext(b.id); err != nil {\n\t\treturn fmt.Errorf(\"disposing browser context: %w\", err)\n\t}\n\treturn nil\n}\n","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L181-L217","documentation":"browserContext.clearPermissions() issues the CDP Browser.resetPermissions command scoped to the context (browser_context.go:199). If the CDP round-trip fails, the transport/protocol error is wrapped as 'clearing permissions: %w' — almost always because the context or browser is already closed, or the connection is gone.","triggerScenarios":"Calling clearPermissions() after context.close() or browser.close(); connection dropping mid-call; concurrent teardown from multiple VUs touching the same context.","commonSituations":"Cleanup code in teardown running after the browser already disconnected; helpers that reset permissions unconditionally at iteration end.","solutions":["Call clearPermissions() while the context is open — before close()","Check the wrapped error to distinguish a dead connection from a protocol refusal","Wrap permission reset in a guard that skips it when the browser/context is already closed","Restructure so permission changes happen in setup, not teardown"],"exampleFix":"// before\nawait ctx.close()\nawait ctx.clearPermissions() // throws: clearing permissions\n\n// after\nawait ctx.clearPermissions()\nawait ctx.close()","handlingStrategy":"validation","validationCode":"// reset permissions only while the context is open\nif (ctxOpen) await ctx.clearPermissions() // track open/closed yourself","typeGuard":null,"tryCatchPattern":"try {\n  await ctx.clearPermissions()\n} catch (e) {\n  if (/clearing permissions/.test(String(e)) && /connection|closed/i.test(String(e))) {\n    // context/browser already gone — nothing to clear\n  } else throw e\n}","preventionTips":["Call clearPermissions() before close(), never in teardown after the browser is gone","Track context open/closed state in your harness"],"tags":["cdp","permissions","lifecycle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}