grafana/k6 · warning
browser.on promise rejected
Error message
browser.on promise rejected: %w
What it means
browser.on('disconnected') blocks until the browser's WS connection is lost, but it also watches the VU context. This error fires when the iteration context finishes first — the test ended, was aborted, or hit its deadline — wrapping the context.Canceled/DeadlineExceeded via ContextErr. It usually means the script was still waiting for a disconnect that never came before teardown.
Solutions
- Do not await 'disconnected' unbounded — close the browser explicitly so the event fires before iteration end
- Treat this error during teardown as a normal exit (the test is over), not a failure
- Raise scenario/iteration timeouts if waiting for disconnect is intentional
- Structure scripts to await browser.close() rather than waiting for external disconnects
Example fix
// before
browser.on('disconnected'); // blocks; VU canceled first -> promise rejected
// script ends without closing browser
// after
await browser.close(); // deterministic teardown; disconnect fires immediately Defensive patterns
Strategy: try-catch
Try / catch
try {
browser.on('disconnected');
} catch (e) {
if (/browser\.on promise rejected/.test(String(e))) {
// VU context ended before disconnect — normal during teardown; exit gracefully
console.warn('iteration ended while waiting for browser disconnect');
} else throw e;
} Prevention
- Always await browser.close() at script end instead of waiting for external disconnects
- Do not block on 'disconnected' in iterations with tight deadlines
- Treat context-cancellation errors during teardown as expected
When it happens
Trigger: Awaiting browser.on('disconnected') in a script whose iteration is canceled: end of scenario duration, aborted run (SIGINT), graceful-stop timeout, or a per-iteration timeout firing while the browser stays connected.
Common situations: Scripts that block on disconnected indefinitely while the browser stays alive; long tests stopped by CI timeouts; k6 aborting the VU during teardown while a promise is pending.
Related errors
- browser is closing
- "handler" argument cannot be nil
- incorrect event , is the only event supported
- page.route(' '): iteration ended before route completed
- unknown browser event
AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18).
Data as JSON: /api/errors/91615bebb56e5bc0.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/browser.go:778
if err != nil {
return nil, spanRecordError(span, err)
}
return page, nil
}
// On returns a Promise that is resolved when the browser process is disconnected.
// The only accepted event value is "disconnected".
func (b *Browser) On(event string) (bool, error) {
if event != EventBrowserDisconnected {
return false, fmt.Errorf("unknown browser event: %q, must be %q", event, EventBrowserDisconnected)
}
select {
case <-b.browserProc.lostConnection:
return true, nil
case <-b.vuCtx.Done():
return false, fmt.Errorf("browser.on promise rejected: %w", ContextErr(b.vuCtx))
}
}
// UserAgent returns the controlled browser's user agent string.
func (b *Browser) UserAgent() string {
return b.version.userAgent
}
// Version returns the controlled browser's version.
func (b *Browser) Version() string {
product := b.version.product
_, after, ok := strings.Cut(product, "/")
if !ok {
return product
}
return after
}
View on GitHub (pinned to 3fcf5388d7)