{"record":{"id":"e20b82b10c5a2407","repo":"grafana/k6","slug":"disposing-document-element-while-getting-owner-fra","errorCode":null,"errorMessage":"disposing document element while getting owner frame: %w","messagePattern":"disposing document element while getting owner frame: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":740,"sourceCode":"\t// share context between each other due to CORS.\n\t//\n\t// Instead here we use the element's session to retrieve the description of\n\t// itself, which works even when we're handling elements that are not in\n\t// an iframe.\n\tnode, err := action.Do(cdp.WithExecutor(p.ctx, h.session))\n\tif err != nil {\n\t\tp.logger.Debugf(\"Page:getOwnerFrame:DescribeNode:return\", \"sid:%v err:%v\", p.sessionID(), err)\n\t\treturn \"\", nil\n\t}\n\n\tif node == nil {\n\t\tp.logger.Debugf(\"Page:getOwnerFrame:node:nil:return\", \"sid:%v err:%v\", p.sessionID(), err)\n\t\treturn \"\", nil\n\t}\n\n\tframeID := node.FrameID\n\tif err := documentElement.Dispose(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"disposing document element while getting owner frame: %w\", err)\n\t}\n\n\treturn frameID, nil\n}\n\n// errPageClosing is returned when a frame-session attachment is rejected\n// because the page has started closing.\nvar errPageClosing = errors.New(\"page is closing\")\n\nfunc (p *Page) attachFrameSession(fid cdp.FrameID, fs *FrameSession) error {\n\tp.logger.Debugf(\"Page:attachFrameSession\", \"sid:%v fid=%v\", p.session.ID(), fid)\n\n\tif fs == nil {\n\t\treturn errors.New(\"internal error: FrameSession is nil\")\n\t}\n\n\t// This prevents a TOCTOU race where Close() snapshots owned sessions\n\t// and then a new session is inserted outside that snapshot.","sourceCodeStart":722,"sourceCodeEnd":758,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/page.go#L722-L758","documentation":"Thrown by Page.getOwnerFrame when the JSHandle for the document element cannot be disposed after its node was successfully described via DOM.describeNode. The frame ID retrieval itself succeeded; only the cleanup of the temporary remote object handle failed, meaning the CDP session was likely tearing down mid-call. This is a rare, race-adjacent internal error in the browser module.","triggerScenarios":"Calling an API that resolves a frame's owner (e.g., getting the content frame of an iframe element handle) while the page is navigating, closing, or the CDP session is being detached. The documentElement.Dispose() RPC returns an error because the target or session is already gone.","commonSituations":"Script closes the page or browser context (or the test iteration ends) at the same moment an iframe/owner-frame lookup is in flight; tab crashed by the page itself (OOM) during the lookup.","solutions":["Ensure the page is fully loaded and not being closed before accessing iframe/frame ownership APIs","Check page.isClosed() before performing frame queries","Retry the frame lookup once after a short delay if the page was mid-navigation","If it reproduces deterministically, report it as a k6 browser module issue with the script and browser logs"],"exampleFix":"// before\nconst frame = elementHandle.contentFrame(); // may race with page close\n\n// after\nif (!page.isClosed()) {\n  const frame = await elementHandle.contentFrame();\n}","handlingStrategy":"try-catch","validationCode":"if (page.isClosed()) throw new Error('page closed before frame lookup');","typeGuard":null,"tryCatchPattern":"try {\n  const frame = await elementHandle.contentFrame();\n} catch (e) {\n  if (/disposing document element/.test(e.message)) { /* retry once or skip */ }\n  else throw e;\n}","preventionTips":["Avoid iframe/frame ownership lookups while navigation or close is in progress","Close pages deterministically in finally blocks so lookups never race teardown"],"tags":["browser","cdp","frame","cleanup","race-condition"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}