grafana/k6 · critical

on create page event failed to return a page: %w

Error message

on create page event failed to return a page: %w

What it means

Internal invariant error from BrowserContext.runWaitForEventHandler: when the 'page' event fired, its payload could not be asserted to a *Page, so the handler wraps k6error.ErrFatal and ships it to the error channel of waitForEvent. ErrFatal marks unrecoverable failures in k6 and aborts the iteration; in practice this path should never execute because the event source always emits a *Page.

Source

Thrown at internal/js/modules/k6/browser/common/browser_context.go:421

	defer func() {
		close(out)
		close(errOut)
	}()

	for {
		select {
		case <-ctx.Done():
			b.logger.Debugf("BrowserContext:runWaitForEventHandler:go():ctx:done", "bctxid:%v", b.id)
			return
		case ev := <-chEvHandler:
			if ev.typ != EventBrowserContextPage {
				continue
			}

			b.logger.Debugf("BrowserContext:runWaitForEventHandler:go():EventBrowserContextPage", "bctxid:%v", b.id)
			p, ok := ev.data.(*Page)
			if !ok {
				errOut <- fmt.Errorf("on create page event failed to return a page: %w", k6error.ErrFatal)
				return
			}

			if predicateFn == nil {
				b.logger.Debugf("BrowserContext:runWaitForEventHandler:go():EventBrowserContextPage:return", "bctxid:%v", b.id)
				out <- p
				return
			}

			retVal, err := predicateFn(p)
			if err != nil {
				errOut <- fmt.Errorf("predicate function failed: %w", err)
				return
			}

			if retVal {
				b.logger.Debugf(
					"BrowserContext:runWaitForEventHandler:go():EventBrowserContextPage:predicateFn:return",

View on GitHub (pinned to 93accf6570)

Solutions

  1. Upgrade (or roll back) k6 to a version where the browser module event dispatch is stable.
  2. Report it as a bug to k6 (https://github.com/grafana/k6/issues) including the script and debug logs, since it indicates an internal invariant violation.
  3. As a workaround, poll for new pages via context.pages() on a timer instead of waitForEvent.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const page = await context.waitForEvent('page');
} catch (e) {
  if (e.message.includes('failed to return a page')) {
    // internal fatal invariant: abort and report a k6 bug with debug logs
    throw new Error(`k6 internal error, please report: ${e.message}`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Only reachable if the internal event emitter dispatches an EventBrowserContextPage whose data is not a *Page — i.e. a bug or an unexpected internal state in k6's browser module. No user input can produce it directly.

Common situations: Essentially never seen in normal use; if it appears, it is after upgrading to a k6 version with a regression in the browser event dispatch code, or in a custom build/extension that emits context events with the wrong payload type.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/e70a0ac2b68d5407. Report an issue: GitHub.