{"record":{"id":"b9c8638708710fb9","repo":"grafana/k6","slug":"attaching-iframe-target-id-v-to-session-id-v-w","errorCode":null,"errorMessage":"attaching iframe target ID %v to session ID %v: %w","messagePattern":"attaching iframe target ID (.+?) to session ID (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame_session.go","lineNumber":1054,"sourceCode":"\t\treturn nil\n\t}\n\t// Remove all children of the previously attached frame.\n\terr := fs.manager.removeChildFramesRecursively(fr)\n\tif err != nil {\n\t\tfs.logger.Debugf(\"FrameSession:attachIFrameToTarget:return\",\n\t\t\t\"sid:%v tid:%v esid:%v etid:%v ebctxid:%v type:%q, can't remove child frames recursively: %q\",\n\t\t\tfs.session.ID(), fs.targetID, sid, ti.TargetID, ti.BrowserContextID, ti.Type, err)\n\t}\n\n\tnfs, err := NewFrameSession(\n\t\tfs.ctx,\n\t\tfs.teardownCtx,\n\t\tsession,\n\t\tfs.page, fs, ti.TargetID,\n\t\tfs.logger,\n\t\tfalse)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"attaching iframe target ID %v to session ID %v: %w\",\n\t\t\tti.TargetID, sid, err)\n\t}\n\n\tif err := fs.page.attachFrameSession(cdp.FrameID(ti.TargetID), nfs); err != nil {\n\t\tif errors.Is(err, errPageClosing) {\n\t\t\tfs.logger.Debugf(\"FrameSession:attachIFrameToTarget\",\n\t\t\t\t\"rejected frame; page is closing: tid=%v\", ti.TargetID)\n\t\t\tdetachSession(fs.teardownCtx, session)\n\t\t\treturn nil\n\t\t}\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// attachWorkerToTarget attaches a Worker target to a given session.\nfunc (fs *FrameSession) attachWorkerToTarget(ti *target.Info, session *Session) error {","sourceCodeStart":1036,"sourceCodeEnd":1072,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame_session.go#L1036-L1072","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Unwrap and read the inner error — it names the actual failing init step; fix that root cause","Let the page finish loading (waitUntil networkidle) before closing it, so iframe attach completes cleanly","Block heavy third-party iframe domains if they are irrelevant to the test (route interception) to reduce attach churn"],"exampleFix":"// before\nconst page = browser.newPage();\npage.goto('https://example.com/embeds');\npage.close(); // races iframe attach\n\n// after\nconst page = browser.newPage();\npage.goto('https://example.com/embeds', { waitUntil: 'networkidle' });\npage.close();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  page.goto(url, { waitUntil: 'networkidle' });\n} catch (e) {\n  if (/attaching iframe target ID/i.test(String(e))) {\n    // unwrap the inner error — it names the real init failure; retry navigation once\n  }\n}","preventionTips":["Let pages finish loading (networkidle) before closing them so iframe attach completes","Block third-party iframe domains that are irrelevant to the test","Read the wrapped error chain: the iframe target/session IDs plus inner cause pinpoint the failing init step"],"tags":["browser","iframe","session","attach","race-condition"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}