{"record":{"id":"a294ba84beae405a","repo":"grafana/k6","slug":"invalidstateerror","errorCode":null,"errorMessage":"InvalidStateError","messagePattern":"InvalidStateError","errorType":"exception","errorClass":"InvalidStateError","httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/websockets/websockets.go","lineNumber":756,"sourceCode":"\t\tTimeSeries: metrics.TimeSeries{\n\t\t\tMetric: w.builtinMetrics.WSPing,\n\t\t\tTags:   w.tagsAndMeta.Tags,\n\t\t},\n\t\tTime:     pongTimestamp,\n\t\tMetadata: w.tagsAndMeta.Metadata,\n\t\tValue:    metrics.D(pongTimestamp.Sub(pingTimestamp)),\n\t})\n}\n\n// assertStateOpen checks if the websocket is in the OPEN state\n// otherwise it throws an error (panic)\nfunc (w *webSocket) assertStateOpen() {\n\tif w.readyState == OPEN {\n\t\treturn\n\t}\n\n\t// TODO figure out if we should give different error while being closed/closed/connecting\n\tcommon.Throw(w.vu.Runtime(), errors.New(\"InvalidStateError\"))\n}\n\nfunc isValidClientCloseCode(code int) bool {\n\treturn code == 1000 || (code >= 3000 && code <= 4999)\n}\n\nfunc isValidCloseReason(reason string) bool {\n\treturn len([]byte(reason)) <= 123\n}\n\nfunc (w *webSocket) close(code int, reason string) error {\n\tif w.readyState == CLOSED || w.readyState == CLOSING {\n\t\treturn nil\n\t}\n\n\tif code != 0 && !isValidClientCloseCode(code) {\n\t\treturn fmt.Errorf(\n\t\t\t\"InvalidAccessError: Failed to execute 'close' on 'WebSocket': \"+","sourceCodeStart":738,"sourceCodeEnd":774,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/websockets/websockets.go#L738-L774","documentation":"Error \"InvalidStateError\" thrown in grafana/k6.","triggerScenarios":"Thrown at internal/js/modules/k6/websockets/websockets.go:756 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}