grafana/k6 · error

disposing element handles

Error message

disposing element handles: %w

What it means

Produced in a deferred cleanup in ElementHandle.queryAll: after obtaining the array-like JSHandle of query results, the code disposes it via handles.Dispose(); if disposal fails the error is wrapped this way and joined with the function's return error via errors.Join. Dispose failures here almost always mean the execution context or target was already destroyed, making cleanup moot.

Solutions

  1. Treat dispose-during-cleanup failures as benign when the page navigated or closed
  2. Avoid reacting to this error; the primary query error (if any) is joined alongside
  3. Ensure you are not closing the page concurrently with pending queries
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/common/element_handle.go:1285 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: 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 3fcf5388d7)