grafana/k6 · warning
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 closes the context k6 tracks as the active one, which is created lazily when the first page is opened. The mapping browser.closeContext() is backed by a promise; if b.context is nil — no page was ever created in this browser, or the context was already closed/cleared — CloseContext returns this error instead of panicking.
Source
Thrown at internal/js/modules/k6/browser/common/browser.go:691
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 {
if b.context == nil {
return errors.New("cannot close context as none is active in browser")
}
return b.context.Close()
}
// Context returns the current browser context or nil.
func (b *Browser) Context() *BrowserContext {
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.
func (b *Browser) NewContext(opts *BrowserContextOptions) (*BrowserContext, error) {
_, span := TraceAPICall(b.vuCtx, "", "browser.newContext")View on GitHub (pinned to 93accf6570)
Solutions
- Guard the call: if (browser.context()) await browser.closeContext()
- Create at least one page before closing the context in each iteration that closes it
- Don't double-close — browser.close() already tears contexts down; drop the manual closeContext in that path
Example fix
// before
await browser.closeContext(); // throws when no context was ever opened
// after
if (browser.context()) {
await browser.closeContext();
} Defensive patterns
Strategy: type-guard
Validate before calling
if (browser.context()) {
await browser.closeContext();
} Type guard
function hasActiveContext(b) {
return b.context() !== undefined && b.context() !== null;
} Try / catch
try {
await browser.closeContext();
} catch (e) {
if (!e.message.includes('cannot close context as none is active')) throw e;
// no context was open — nothing to do
} Prevention
- Pair every closeContext with the page creation that opened the context (same code path)
- Let browser.close() handle final teardown instead of manual closeContext at end-of-test
- Track whether the iteration actually created a page before running cleanup helpers
When it happens
Trigger: Calling await browser.closeContext() before any browser.newPage() in the iteration; calling it twice (the second call finds context already nil); calling it after browser.close() has torn everything down.
Common situations: Setup/teardown helpers that unconditionally close the context per iteration even when setup failed early; conditional test flows where page creation was skipped (early exit); reused scripts where a guard was removed.
Related errors
- iteration ended before page.on handler completed executing
- ErrFatal
- attaching new page: %w
- browser.on promise rejected: %w
- could not find frame with id %s
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/2bff042345cb6da2.
Report an issue: GitHub.