grafana/k6 · error

disposing document element while getting owner frame: %w

Error message

disposing document element while getting owner frame: %w

What it means

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.

Source

Thrown at internal/js/modules/k6/browser/common/page.go:740

	// share context between each other due to CORS.
	//
	// Instead here we use the element's session to retrieve the description of
	// itself, which works even when we're handling elements that are not in
	// an iframe.
	node, err := action.Do(cdp.WithExecutor(p.ctx, h.session))
	if err != nil {
		p.logger.Debugf("Page:getOwnerFrame:DescribeNode:return", "sid:%v err:%v", p.sessionID(), err)
		return "", nil
	}

	if node == nil {
		p.logger.Debugf("Page:getOwnerFrame:node:nil:return", "sid:%v err:%v", p.sessionID(), err)
		return "", nil
	}

	frameID := node.FrameID
	if err := documentElement.Dispose(); err != nil {
		return "", fmt.Errorf("disposing document element while getting owner frame: %w", err)
	}

	return frameID, nil
}

// errPageClosing is returned when a frame-session attachment is rejected
// because the page has started closing.
var errPageClosing = errors.New("page is closing")

func (p *Page) attachFrameSession(fid cdp.FrameID, fs *FrameSession) error {
	p.logger.Debugf("Page:attachFrameSession", "sid:%v fid=%v", p.session.ID(), fid)

	if fs == nil {
		return errors.New("internal error: FrameSession is nil")
	}

	// This prevents a TOCTOU race where Close() snapshots owned sessions
	// and then a new session is inserted outside that snapshot.

View on GitHub (pinned to 93accf6570)

Solutions

  1. Ensure the page is fully loaded and not being closed before accessing iframe/frame ownership APIs
  2. Check page.isClosed() before performing frame queries
  3. Retry the frame lookup once after a short delay if the page was mid-navigation
  4. If it reproduces deterministically, report it as a k6 browser module issue with the script and browser logs

Example fix

// before
const frame = elementHandle.contentFrame(); // may race with page close

// after
if (!page.isClosed()) {
  const frame = await elementHandle.contentFrame();
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (page.isClosed()) throw new Error('page closed before frame lookup');

Try / catch

try {
  const frame = await elementHandle.contentFrame();
} catch (e) {
  if (/disposing document element/.test(e.message)) { /* retry once or skip */ }
  else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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