grafana/k6 · error

creating a new

Error message

creating a new %s: %w

What it means

When Chromium attaches a new target (page or background_page, and filtered types such as iframes/workers), onAttachedToTarget builds a Page object for it via NewPage. This error wraps NewPage failing; %s is the CDP target type string (e.g. "page"). Typical root causes are a target that is torn down as fast as it appears, or page-initialization CDP calls exceeding the browser timeout.

Solutions

  1. Raise the browser timeout option, e.g. options.browser = { timeout: '60s' }
  2. Close or ignore popups you do not use instead of letting them attach during teardown
  3. Retry the navigation/step that triggered the transient target
  4. Reduce per-browser page/iframe churn or increase browser memory if targets fail under pressure

Example fix

// before
export const options = { browser: { type: 'chromium' } }; // default 30s timeout

// after
export const options = {
  browser: { type: 'chromium', timeout: '60s' },
};
Defensive patterns

Strategy: retry

Validate before calling

// Guard popup-heavy navigation with an explicit timeout budget
export const options = { browser: { type: 'chromium', timeout: '60s' } };

Try / catch

try {
  await page.goto(url);
} catch (e) {
  if (/creating a new (page|background_page)/.test(String(e))) {
    // transient target attach failure (popup appeared and vanished)
    await sleep(500);
    await page.goto(url); // retry once
  } else throw e;
}

Prevention

When it happens

Trigger: A popup or window.open target attaching and closing immediately; service workers/iframes attaching near iteration teardown; NewPage's internal CDP round-trips exceeding the browser 'timeout' option under heavy load; attaching while the browser is being closed (the closing case is separately ignored).

Common situations: Navigations that trigger popups the script never handles; sites spawning many iframes/workers at the end of a test; overloaded browsers where initialization exceeds the default 30s timeout.

Related errors


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

Appendix: source

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

	)
	// Opener is nil for the initial page.
	if isPage {
		b.pagesMu.RLock()
		if t, ok := b.pages[targetPage.OpenerID]; ok {
			opener = t
		}
		b.pagesMu.RUnlock()
	}
	p, err := NewPage(b.vuCtx, session, browserCtx, targetPage.TargetID, opener, isPage, b.logger)
	if err != nil && b.isPageAttachmentErrorIgnorable(ev, session, err) {
		// Always release: isPageAttachmentErrorIgnorable can also return
		// true when only this VU's context ended, while the browser
		// instance stays alive and shared with other VUs.
		detachSession(session)
		return nil // Ignore this page.
	}
	if err != nil {
		return fmt.Errorf("creating a new %s: %w", targetPage.Type, err)
	}

	// This prevents a race where Close() sets closing and snapshots
	// pages, but a new page is inserted outside that snapshot.
	if err := b.attachNewPage(p, ev); err != nil {
		if !errors.Is(err, errBrowserClosing) {
			return fmt.Errorf("attaching new page: %w", err)
		}

		b.logger.Debugf(
			"Browser:onAttachedToTarget",
			"rejected page attachment; browser is closing: sid:%v tid:%v",
			ev.SessionID, ev.TargetInfo.TargetID,
		)
		if closeErr := p.Close(); closeErr != nil {
			b.logger.Debugf(
				"Browser:onAttachedToTarget",
				"closing rejected page: %v", closeErr,

View on GitHub (pinned to 3fcf5388d7)