grafana/k6 · error

creating a new blank page

Error message

creating a new blank page: %w

What it means

newPageInContext opens a new tab by sending Target.createTarget("about:blank") with the context id over CDP. This error wraps that createTarget call failing. The usual causes are target/memory exhaustion in the browser, the context already being disposed, or the browser closing/lost connection.

Solutions

  1. Call page.close() when done with each page and reuse a single context across iterations
  2. Reduce simultaneous pages or raise the browser container's memory
  3. Retry page creation once after a short delay for transient failures
  4. Ensure the context and browser are still open before calling newPage()

Example fix

// before
for (const url of urls) {
  const p = context.newPage();
  p.goto(url); // pages never closed -> target exhaustion
}

// after
const p = context.newPage();
for (const url of urls) {
  await p.goto(url);
}
await p.close();
Defensive patterns

Strategy: retry

Validate before calling

// Bound page creation and always release pages
const MAX_PAGES = 10;
let openPages = 0;
function newPage(ctx) {
  if (openPages >= MAX_PAGES) throw new Error(`Page budget exhausted (${MAX_PAGES}); close pages first`);
  openPages++;
  return ctx.newPage();
}

Try / catch

try {
  page = context.newPage();
} catch (e) {
  if (String(e).includes('creating a new blank page')) {
    // target/memory exhaustion or closing race: close stale pages, brief pause, retry once
    await sleep(1000);
    page = context.newPage();
  } else throw e;
}

Prevention

When it happens

Trigger: Calling context.newPage() when the browser has too many targets or is out of memory; creating a page after the context or browser began closing; the WS connection dying before the command completes.

Common situations: Loops that open pages per iteration without closing them until Chromium hits its target/memory ceiling; memory-limited containers where tab creation fails; page creation racing test teardown.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/ba01873cd023125e. Report an issue: GitHub.

Appendix: source

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

		bc, // browser context will emit the following event:
		[]string{EventBrowserContextPage},
		func(e any) bool {
			tid := <-targetID

			b.logger.Debugf("Browser:newPageInContext:createWaitForEventHandler",
				"tid:%v ptid:%v bctxid:%v", tid, e.(*Page).targetID, id) //nolint:forcetypeassert

			// we are only interested in the new page.
			return e.(*Page).targetID == tid //nolint:forcetypeassert
		},
	)
	defer removeEventHandler()

	// create a new page.
	action := target.CreateTarget(BlankPage).WithNewWindow(true).WithBrowserContextID(id)
	tid, err := action.Do(cdp.WithExecutor(ctx, b.conn))
	if err != nil {
		return nil, fmt.Errorf("creating a new blank page: %w", err)
	}
	// let the event handler know about the new page.
	targetID <- tid
	var page *Page
	select {
	case <-waitForPage:
		b.logger.Debugf("Browser:newPageInContext:<-waitForPage", "tid:%v bctxid:%v", tid, id)
		b.pagesMu.RLock()
		page = b.pages[tid]
		b.pagesMu.RUnlock()
	case <-ctx.Done():
		b.logger.Debugf("Browser:newPageInContext:<-ctx.Done", "tid:%v bctxid:%v err:%v", tid, id, ContextErr(ctx))
	}

	if err = ContextErr(ctx); err != nil {
		err = &k6ext.UserFriendlyError{
			Err:     err,
			Timeout: b.browserOpts.Timeout,

View on GitHub (pinned to 3fcf5388d7)