{"record":{"id":"2bff042345cb6da2","repo":"grafana/k6","slug":"cannot-close-context-as-none-is-active-in-browser","errorCode":null,"errorMessage":"cannot close context as none is active in browser","messagePattern":"cannot close context as none is active in browser","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser.go","lineNumber":687,"sourceCode":"\tdeadline := time.After(timeout)\n\tfor len(b.getPages()) > 0 {\n\t\tselect {\n\t\tcase <-deadline:\n\t\t\tb.logger.Debugf(\"Browser:Close\",\n\t\t\t\t\"timed out after %s waiting for %d page(s) to detach; closing anyway\",\n\t\t\t\ttimeout, len(b.getPages()))\n\t\t\treturn\n\t\tcase <-ticker.C:\n\t\t}\n\t}\n}\n\n// CloseContext is a short-cut function to close the current browser's context.\n// If there is no active browser context, it returns an error.\nfunc (b *Browser) CloseContext() error {\n\tcontext := b.Context()\n\tif context == nil {\n\t\treturn errors.New(\"cannot close context as none is active in browser\")\n\t}\n\treturn context.Close()\n}\n\n// Context returns the current browser context or nil.\nfunc (b *Browser) Context() *BrowserContext {\n\tb.contextMu.RLock()\n\tdefer b.contextMu.RUnlock()\n\treturn b.context\n}\n\n// IsConnected returns whether the WebSocket connection to the browser process\n// is active or not.\nfunc (b *Browser) IsConnected() bool {\n\treturn !b.closing.Load() && b.browserProc.isConnected()\n}\n\n// NewContext creates a new incognito-like browser context.","sourceCodeStart":669,"sourceCodeEnd":705,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/browser.go#L669-L705","documentation":"Browser.CloseContext was called when b.context is nil — no browser context is currently active (it was closed, or was never created via browser.newContext()). The guard prevents a nil-pointer dereference and tells the user there is nothing to close.","triggerScenarios":"Thrown at internal/js/modules/k6/browser/common/browser.go:691 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Create a context first with browser.newContext() (or newPage()) before calling browser.context().close()","Check that the context wasn't already closed earlier in the script","Guard the call: if (browser.context()) browser.context().close()"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-09-20T22:01:45.163Z","contentChangedAt":"2026-09-20T22:01:45.163Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}