{"record":{"id":"4e49dd7feaffdfae","repo":"grafana/k6","slug":"default-browser-context-can-t-be-closed","errorCode":null,"errorMessage":"default browser context can't be closed","messagePattern":"default browser context can't be closed","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":210,"sourceCode":"\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\n// GrantPermissions enables the specified permissions, all others will be disabled.\nfunc (b *BrowserContext) GrantPermissions(permissions []string, opts GrantPermissionsOptions) error {\n\tb.logger.Debugf(\"BrowserContext:GrantPermissions\", \"bctxid:%v\", b.id)\n\n\tpermsToProtocol := map[string]cdpbrowser.PermissionType{\n\t\t\"geolocation\":          cdpbrowser.PermissionTypeGeolocation,\n\t\t\"midi\":                 cdpbrowser.PermissionTypeMidi,\n\t\t\"midi-sysex\":           cdpbrowser.PermissionTypeMidiSysex,\n\t\t\"notifications\":        cdpbrowser.PermissionTypeNotifications,\n\t\t\"camera\":               cdpbrowser.PermissionTypeVideoCapture,\n\t\t\"microphone\":           cdpbrowser.PermissionTypeAudioCapture,","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L192-L228","documentation":"browser.context() returns chromium's implicit default browser context, whose internal ID is the empty string. BrowserContext.Close (browser_context.go:210) explicitly refuses to close it: 'default browser context can't be closed'. The default context is owned by the browser process and is torn down when the browser closes, so k6 rejects the call outright.","triggerScenarios":"const ctx = browser.context(); await ctx.close(); — the only way to hit it. Also generic helpers that blindly close every context they are handed, including the default one.","commonSituations":"Code ported from Playwright, where closing the default context is permitted; scripts that iterate all contexts and close them; shared teardown closing browser.context() alongside user contexts.","solutions":["Only close contexts you created with browser.newContext()","Guard the close: skip it when the context is browser.context()","Rely on browser.close() to tear down the default context and its pages"],"exampleFix":"// before\nconst ctx = browser.context()\nawait ctx.close() // throws: default browser context can't be closed\n\n// after\nconst ctx = browser.newContext()\nawait ctx.close() // OK\n// or: let browser.close() clean up the default context","handlingStrategy":"type-guard","validationCode":"const isDefault = (ctx) => ctx === browser.context()\nif (!isDefault(ctx)) await ctx.close()","typeGuard":"function isDefaultContext(ctx, browser) {\n  return ctx === browser.context()\n}","tryCatchPattern":"try {\n  await ctx.close()\n} catch (e) {\n  if (/default browser context can't be closed/.test(String(e))) {\n    // expected when handed the default context — ignore\n  } else throw e\n}","preventionTips":["Only close contexts created via newContext()","Guard shared close helpers against browser.context()","Let browser.close() clean up the default context"],"tags":["api-misuse","browser-context","lifecycle","playwright-port"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}