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

  1. Do not await 'disconnected' unbounded — close the browser explicitly so the event fires before iteration end
  2. Treat this error during teardown as a normal exit (the test is over), not a failure
  3. Raise scenario/iteration timeouts if waiting for disconnect is intentional
  4. 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

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


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)