grafana/k6 · error

updating extra HTTP headers: %w

Error message

updating extra HTTP headers: %w

What it means

Extra HTTP headers configured on the browser context (extraHTTPHeaders option) or set on a page are merged (page headers win) and pushed to the browser through the network manager via Network.setExtraHTTPHeaders. This error means that underlying call failed while applying the merged header map.

Source

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

		return fmt.Errorf("internal error while updating emulated media: %w", err)
	}
	return nil
}

func (fs *FrameSession) updateExtraHTTPHeaders(initial bool) error {
	fs.logger.Debugf("NewFrameSession:updateExtraHTTPHeaders", "sid:%v tid:%v", fs.session.ID(), fs.targetID)

	// Merge extra headers from browser context and page, where page specific headers ake precedence.
	mergedHeaders := make(network.Headers)
	for k, v := range fs.page.browserCtx.opts.ExtraHTTPHeaders {
		mergedHeaders[k] = v
	}
	for k, v := range fs.page.extraHTTPHeaders {
		mergedHeaders[k] = v
	}
	if !initial || len(mergedHeaders) > 0 {
		if err := fs.networkManager.SetExtraHTTPHeaders(mergedHeaders); err != nil {
			return fmt.Errorf("updating extra HTTP headers: %w", err)
		}
	}

	return nil
}

func (fs *FrameSession) updateGeolocation(initial bool) error {
	fs.logger.Debugf("NewFrameSession:updateGeolocation", "sid:%v tid:%v", fs.session.ID(), fs.targetID)

	geolocation := fs.page.browserCtx.opts.Geolocation
	if !initial || geolocation != nil {
		action := emulation.SetGeolocationOverride().
			WithLatitude(geolocation.Latitude).
			WithLongitude(geolocation.Longitude).
			WithAccuracy(geolocation.Accuracy)
		if err := action.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {
			return fmt.Errorf("%w", err)
		}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Make every header value a plain string (String(value)) before passing it
  2. Apply extraHTTPHeaders at context/page creation time rather than after navigation, and guard page-level calls with page.isClosed()
  3. Retry once if the failure coincided with setup or navigation
  4. If persistent, look for accompanying CDP errors indicating the browser process died

Example fix

// before
const ctx = await browser.newContext({ extraHTTPHeaders: { 'X-Count': 42 } });

// after
const ctx = await browser.newContext({ extraHTTPHeaders: { 'X-Count': '42' } });
Defensive patterns

Strategy: validation

Validate before calling

const headers = { 'X-Tenant': 'acme', 'X-Count': String(42) };
for (const [k, v] of Object.entries(headers)) {
  if (typeof v !== 'string') throw new Error(`header ${k} must be a string`);
}
await page.setExtraHTTPHeaders(headers);

Prevention

When it happens

Trigger: Creating a context/page with extraHTTPHeaders, or calling page.setExtraHTTPHeaders(), when the frame's network session is unavailable: page closing or closed, browser disconnected, or a header map the browser rejects (for example non-string values marshalled to objects/arrays).

Common situations: Injecting auth, tenant or trace headers into every request; passing numbers/objects as header values from the k6 script; applying headers during initial page setup racing with target attachment; browser crash under load.

Related errors


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