grafana/k6 · error

internal error while initializing frame %T: %w

Error message

internal error while initializing frame %T: %w

What it means

FrameSession.initOptions (frame_session.go:573) replays browser-context emulation settings onto each new frame session (viewport, device metrics, touch, media emulation, JS/CSP overrides, user agent, offline/credentials state — the accumulated optActions). If any single CDP action fails, the loop stops with the action's type (%T) wrapped in this error. The root cause is either an invalid emulation value accepted earlier but rejected by chromium, or a target that died during init.

Source

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

		reqIntercept = true
	}
	if err := fs.updateRequestInterception(reqIntercept); err != nil {
		return err
	}

	if err := fs.updateOffline(true); err != nil {
		return err
	}
	if err := fs.updateHTTPCredentials(true); err != nil {
		return err
	}
	if err := fs.updateEmulateMedia(); err != nil {
		return err
	}

	for _, action := range optActions {
		if err := action.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {
			return fmt.Errorf("internal error while initializing frame %T: %w", action, err)
		}
	}

	return nil
}

func (fs *FrameSession) initRendererEvents() {
	fs.logger.Debugf("NewFrameSession:initEvents:initRendererEvents",
		"sid:%v tid:%v", fs.session.ID(), fs.targetID)

	events := []string{
		cdproto.EventLogEntryAdded,
		cdproto.EventPageFileChooserOpened,
		cdproto.EventPageFrameAttached,
		cdproto.EventPageFrameDetached,
		cdproto.EventPageFrameNavigated,
		cdproto.EventPageFrameRequestedNavigation,
		cdproto.EventPageFrameStartedLoading,

View on GitHub (pinned to 93accf6570)

Solutions

  1. Read the wrapped %T type and inner error to identify which emulation action failed, then simplify that context option
  2. Prefer a built-in device descriptor over hand-assembled viewport/touch/mobile combos
  3. If the target is dying rather than rejecting, apply the same resource/stability fixes as for other init CDP errors

Example fix

// before
const ctx = browser.newContext({
  viewport: { width: 375, height: 812 },
  deviceScaleFactor: 3, isMobile: true, hasTouch: true,
  userAgent: 'MyAgent',
});

// after — verify each emulation option against a stable target first
const ctx = browser.newContext({
  viewport: { width: 375, height: 812 },
  deviceScaleFactor: 3, isMobile: true, hasTouch: true,
  userAgent:
    'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
});
Defensive patterns

Strategy: validation

Validate before calling

function checkContextOpts(o = {}) {
  if (o.viewport && !(Number.isInteger(o.viewport.width) && Number.isInteger(o.viewport.height))) {
    throw new Error('viewport must have integer width/height');
  }
  if (o.userAgent !== undefined && typeof o.userAgent !== 'string') {
    throw new Error('userAgent must be a string');
  }
  return o;
}
browser.newContext(checkContextOpts(cfg));

Type guard

interface Viewport { width: number; height: number }
function isViewport(v: unknown): v is Viewport {
  return !!v && typeof (v as Viewport).width === 'number' && typeof (v as Viewport).height === 'number';
}

Try / catch

try {
  browser.newContext(emulationOpts);
  const page = browser.newPage();
} catch (e) {
  if (/internal error while initializing frame/i.test(String(e))) {
    // read the wrapped action type; simplify/fix that context option and retry
  }
}

Prevention

When it happens

Trigger: Setting multiple context options (viewport, deviceScaleFactor, isMobile, hasTouch, colorScheme, reducedMotion, media, userAgent) and opening a page whose target crashes or closes mid-init; some chromium builds reject unusual device metric combinations.

Common situations: Emulating mobile devices with hand-rolled option combinations, remote browsers with different emulation support, and resource-exhausted chromium dying during page setup.

Related errors


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