grafana/k6 · error · InvalidStateError

InvalidStateError

Error message

InvalidStateError

What it means

Error "InvalidStateError" thrown in grafana/k6.

Source

Thrown at internal/js/modules/k6/websockets/websockets.go:756

		TimeSeries: metrics.TimeSeries{
			Metric: w.builtinMetrics.WSPing,
			Tags:   w.tagsAndMeta.Tags,
		},
		Time:     pongTimestamp,
		Metadata: w.tagsAndMeta.Metadata,
		Value:    metrics.D(pongTimestamp.Sub(pingTimestamp)),
	})
}

// assertStateOpen checks if the websocket is in the OPEN state
// otherwise it throws an error (panic)
func (w *webSocket) assertStateOpen() {
	if w.readyState == OPEN {
		return
	}

	// TODO figure out if we should give different error while being closed/closed/connecting
	common.Throw(w.vu.Runtime(), errors.New("InvalidStateError"))
}

func isValidClientCloseCode(code int) bool {
	return code == 1000 || (code >= 3000 && code <= 4999)
}

func isValidCloseReason(reason string) bool {
	return len([]byte(reason)) <= 123
}

func (w *webSocket) close(code int, reason string) error {
	if w.readyState == CLOSED || w.readyState == CLOSING {
		return nil
	}

	if code != 0 && !isValidClientCloseCode(code) {
		return fmt.Errorf(
			"InvalidAccessError: Failed to execute 'close' on 'WebSocket': "+

View on GitHub (pinned to 93accf6570)

When it happens

Trigger: Thrown at internal/js/modules/k6/websockets/websockets.go:756 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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