grafana/k6 · error

closing a page

Error message

closing a page: %w

What it means

Failure of the CDP target.closeTarget command during page close. Context-canceled errors are ignored (the browser is already shutting down and the close response may race); other failures — e.g. the target is already closed or the session died — are collected into closeErrs and surfaced after teardown completes.

Solutions

  1. Usually benign when the browser/context is already closing; inspect the wrapped error
  2. Avoid closing the same page twice
  3. Ensure the browser process is healthy if closes consistently fail
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/common/page.go:1021 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/185a1a5fc9941dc1. Report an issue: GitHub.

Appendix: source

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

		add := runtime.RemoveBinding(webVitalBinding)
		if err := add.Do(cdp.WithExecutor(teardownTimeoutCtx, p.session)); err != nil {
			// continue so that we can shutdown the page even if we fail to remove the binding.
			closeErrs = append(closeErrs, fmt.Errorf("internal error while removing binding from page: %w", err))
		}

		err := target.CloseTarget(p.targetID).Do(cdp.WithExecutor(teardownTimeoutCtx, p.session))
		if err != nil && !errors.Is(err, context.Canceled) {
			// When a close target command is sent to the browser via CDP,
			// the browser will start to cleanup and the first thing it
			// will do is return a target.EventDetachedFromTarget, which in
			// our implementation will close the session connection (this
			// does not close the CDP websocket, just removes the session
			// so no other CDP calls can be made with the session ID).
			// This can result in the session's context being closed while
			// we're waiting for the response to come back from the browser
			// for this current command (it's racey).
			closeErrs = append(closeErrs, fmt.Errorf("closing a page: %w", err))
		}

		// Start the teardown of the page's resources (FrameSessions, NetworkManagers, etc)
		// and wait for them to finish their teardown. This allows for a graceful cleanup
		// of resources and ensures that all events are processed before the page is closed.
		p.cancelCtx()
		p.waitForFrameSessions()

		if len(closeErrs) > 0 {
			p.closeErr = spanRecordError(span, errors.Join(closeErrs...))
		}
	})

	return p.closeErr
}

// Content returns the HTML content of the page.
func (p *Page) Content() (string, error) {

View on GitHub (pinned to 3fcf5388d7)