grafana/k6 · warning

disposing element handles: %w

Error message

disposing element handles: %w

What it means

In queryAll, a deferred Dispose() of the result handle array failed after the main path errored, and the dispose error is joined onto the returned error (element_handle.go:1285). Secondary cleanup noise: dispose fails because the target/context is already destroyed; the primary wrapped error is what to fix.

Source

Thrown at internal/js/modules/k6/browser/common/element_handle.go:1285

		evalOptions{forceCallable: true, returnByValue: false},
		js.QueryAll,
		parsedSelector,
	)
	if err != nil {
		return nil, fmt.Errorf("querying all selectors %q: %w", selector, err)
	}
	if result == nil {
		// it is ok to return a nil slice because it means we didn't find any elements.
		return nil, nil
	}

	handles, ok := result.(JSHandleAPI)
	if !ok {
		return nil, fmt.Errorf("getting element handle for selector %q: %w", selector, ErrJSHandleInvalid)
	}
	defer func() {
		if err := handles.Dispose(); err != nil {
			err = fmt.Errorf("disposing element handles: %w", err)
			rerr = errors.Join(err, rerr)
		}
	}()

	props, err := handles.GetProperties()
	if err != nil {
		// GetProperties has a rich error already, so we don't need to wrap it.
		return nil, err //nolint:wrapcheck
	}

	type indexedElem struct {
		index int
		elem  *ElementHandle
	}

	indexedElems := make([]indexedElem, 0, len(props))
	for key, prop := range props {
		if el := prop.AsElement(); el != nil {

View on GitHub (pinned to 93accf6570)

Solutions

  1. Address the primary error first; this rides along
  2. Avoid closing the page/browser while $$ results are pending
  3. Safe to ignore when the target is known-closed
  4. Upgrade k6 — close-race handling has improved
Defensive patterns

Strategy: try-catch

Try / catch

try { return await el.$$(sel); }
catch (e) { /* primary error is actionable; the joined 'disposing element handles' error is teardown noise */ if (isTargetClosed(e)) return []; throw e; }

Prevention

When it happens

Trigger: $$ evaluation returns, the function then errors (or finishes), and the browser page/target closes before the deferred handles.Dispose() runs.

Common situations: Iteration teardown: k6 closes the browser while $$ is unwinding; joined with a navigation-race primary error.

Related errors


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