grafana/k6 · error

lost connection while closing the browser

Error message

lost connection while closing the browser (websocket url: %s)

What it means

BrowserProcess.handleClose detected that the CDP WebSocket connection to the browser was lost while the process was not in a graceful, browser-initiated shutdown. To clean up, the process cancels the iteration context with this error, including the lost WebSocket URL.

Solutions

  1. Investigate why the browser crashed or disconnected (OOM, crash, container eviction)
  2. Avoid killing the browser process from outside the test
  3. Handle the aborted iteration in the script or CI and re-run
Defensive patterns

Strategy: try-catch

When it happens

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

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/e475d4f5d0772158. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/browser_process.go:105

	}

	go p.handleClose(ctx)

	return &p, nil
}

func (p *BrowserProcess) handleClose(ctx context.Context) {
	// If we lose connection to the browser and we're not in-progress with clean
	// browser-initiated termination then cancel the context to clean up.
	select {
	case <-p.lostConnection:
	case <-ctx.Done():
	}

	select {
	case <-p.processIsGracefullyClosing:
	default:
		p.cancel(fmt.Errorf("lost connection while closing the browser (websocket url: %s)", p.wsURL))
	}
}

func (p *BrowserProcess) didLoseConnection() {
	close(p.lostConnection)
}

func (p *BrowserProcess) isConnected() bool {
	var ok bool
	select {
	case _, ok = <-p.lostConnection:
	default:
		ok = true
	}
	return ok
}

// GracefulClose triggers a graceful closing of the browser process.

View on GitHub (pinned to 01ffac6f24)