grafana/k6 · error

attaching iframe target ID %v to session ID %v: %w

Error message

attaching iframe target ID %v to session ID %v: %w

What it means

When chromium auto-attaches a new iframe target, FrameSession.attachIFrameToTarget (frame_session.go:1054) builds a child FrameSession for it; if that constructor fails, the error is wrapped with the iframe's target ID and the parent session ID. The inner error is whatever the child session hit during its own init (any of the debugger-attach, window, domain-enable, frame-tree, or emulation failures), so treat this as a wrapper around iframe initialization problems.

Source

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

		return nil
	}
	// Remove all children of the previously attached frame.
	err := fs.manager.removeChildFramesRecursively(fr)
	if err != nil {
		fs.logger.Debugf("FrameSession:attachIFrameToTarget:return",
			"sid:%v tid:%v esid:%v etid:%v ebctxid:%v type:%q, can't remove child frames recursively: %q",
			fs.session.ID(), fs.targetID, sid, ti.TargetID, ti.BrowserContextID, ti.Type, err)
	}

	nfs, err := NewFrameSession(
		fs.ctx,
		fs.teardownCtx,
		session,
		fs.page, fs, ti.TargetID,
		fs.logger,
		false)
	if err != nil {
		return fmt.Errorf("attaching iframe target ID %v to session ID %v: %w",
			ti.TargetID, sid, err)
	}

	if err := fs.page.attachFrameSession(cdp.FrameID(ti.TargetID), nfs); err != nil {
		if errors.Is(err, errPageClosing) {
			fs.logger.Debugf("FrameSession:attachIFrameToTarget",
				"rejected frame; page is closing: tid=%v", ti.TargetID)
			detachSession(fs.teardownCtx, session)
			return nil
		}
		return err
	}

	return nil
}

// attachWorkerToTarget attaches a Worker target to a given session.
func (fs *FrameSession) attachWorkerToTarget(ti *target.Info, session *Session) error {

View on GitHub (pinned to 93accf6570)

Solutions

  1. Unwrap and read the inner error — it names the actual failing init step; fix that root cause
  2. Let the page finish loading (waitUntil networkidle) before closing it, so iframe attach completes cleanly
  3. Block heavy third-party iframe domains if they are irrelevant to the test (route interception) to reduce attach churn

Example fix

// before
const page = browser.newPage();
page.goto('https://example.com/embeds');
page.close(); // races iframe attach

// after
const page = browser.newPage();
page.goto('https://example.com/embeds', { waitUntil: 'networkidle' });
page.close();
Defensive patterns

Strategy: try-catch

Try / catch

try {
  page.goto(url, { waitUntil: 'networkidle' });
} catch (e) {
  if (/attaching iframe target ID/i.test(String(e))) {
    // unwrap the inner error — it names the real init failure; retry navigation once
  }
}

Prevention

When it happens

Trigger: A page loads iframes while the page is being closed (errPageClosing is handled specially only for the later attach step, not for construction), chromium crashes while iframe targets attach, or heavy SPA pages with rapid iframe churn under load.

Common situations: Third-party ad/embed iframes appearing as pages navigate, tests that close pages while resources are still loading, and memory pressure causing renderer/target crashes during attach storms.

Related errors


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