{"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/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L181-L217","documentation":"ClearPermissions runs the CDP command Browser.resetPermissions scoped to this context's ID (cdpbrowser.ResetPermissions().WithBrowserContextID(b.id)); this error means the CDP call itself failed. Typical wrapped causes: the browser context ID is unknown to the browser (already disposed) or the command channel to Chromium is gone.","triggerScenarios":"Calling browserContext.clearPermissions() after the context was closed/disposed, after browser.close() or a browser crash, or while the CDP WebSocket connection is down.","commonSituations":"Permission cleanup code in a finally block or teardown() that runs after k6 already closed the context; browser process dying mid-test; double cleanup (user code plus framework teardown).","solutions":["Call clearPermissions() during the test, before closing the context, not in teardown after close","Wrap cleanup-path calls in try/catch and treat failures on already-closed contexts as non-fatal","Confirm the browser and context are still open before the call","If the wrapped error indicates a dead connection, restart the browser instead of retrying"],"exampleFix":"// before\n// teardown: context already closed by k6\nctx.clearPermissions(); // throws 'clearing permissions'\n\n// after\n// grant and clear within the test body, before close\nctx.grantPermissions(['geolocation']);\n// ... assertions ...\nctx.clearPermissions();\nctx.close();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// cleanup-path call: tolerate failure on already-closed contexts\ntry {\n  ctx.clearPermissions();\n} catch (e) {\n  console.warn(`clearPermissions ignored during cleanup: ${e.message}`);\n}","preventionTips":["Call clearPermissions during the test body, before ctx.close()","Never run permission cleanup after browser.close() or in end-of-test teardown","Keep permission grant/clear paired inside the same lifecycle scope"],"tags":["browser","cdp","permissions","browser-context"],"backgroundTag":"cdp-protocol-error","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}