grafana/k6 · error
cannot close context as none is active in browser
Error message
cannot close context as none is active in browser
What it means
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.
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()
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at internal/js/modules/k6/browser/common/browser.go:691 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-09-20).
Data as JSON: /api/errors/2bff042345cb6da2.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/browser.go:687
deadline := time.After(timeout)
for len(b.getPages()) > 0 {
select {
case <-deadline:
b.logger.Debugf("Browser:Close",
"timed out after %s waiting for %d page(s) to detach; closing anyway",
timeout, len(b.getPages()))
return
case <-ticker.C:
}
}
}
// CloseContext is a short-cut function to close the current browser's context.
// If there is no active browser context, it returns an error.
func (b *Browser) CloseContext() error {
context := b.Context()
if context == nil {
return errors.New("cannot close context as none is active in browser")
}
return context.Close()
}
// Context returns the current browser context or nil.
func (b *Browser) Context() *BrowserContext {
b.contextMu.RLock()
defer b.contextMu.RUnlock()
return b.context
}
// IsConnected returns whether the WebSocket connection to the browser process
// is active or not.
func (b *Browser) IsConnected() bool {
return !b.closing.Load() && b.browserProc.isConnected()
}
// NewContext creates a new incognito-like browser context.View on GitHub (pinned to 3fcf5388d7)