{"record":{"id":"6fe10af870762d35","repo":"grafana/k6","slug":"syntaxerror-failed-to-execute-close-on-websock","errorCode":null,"errorMessage":"SyntaxError: Failed to execute 'close' on 'WebSocket': The message must not be greater than 123 bytes","messagePattern":"SyntaxError: Failed to execute 'close' on 'WebSocket': The message must not be greater than 123 bytes","errorType":"validation","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/websockets/websockets.go","lineNumber":881,"sourceCode":"func 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 code != 0 && !isValidClientCloseCode(code) {\n\t\treturn fmt.Errorf(\n\t\t\t\"InvalidAccessError: Failed to execute 'close' on 'WebSocket': \"+\n\t\t\t\t\"The close code must be either 1000, or between 3000 and 4999. %d is neither\",\n\t\t\tcode,\n\t\t)\n\t}\n\n\tif !isValidCloseReason(reason) {\n\t\treturn errors.New(\n\t\t\t`SyntaxError: Failed to execute 'close' on 'WebSocket': The message must not be greater than 123 bytes`,\n\t\t)\n\t}\n\tif w.readyState == CLOSED || w.readyState == CLOSING {\n\t\treturn nil\n\t}\n\tif code == 0 {\n\t\tcode = websocket.CloseNormalClosure\n\t}\n\tw.closeCode = code\n\n\twasConnecting := w.readyState == CONNECTING\n\tw.readyState = CLOSING\n\tif wasConnecting {\n\t\tw.connectionMu.Lock()\n\t\tw.connectCancel()\n\t\tpendingConnection := w.pendingConnection\n\t\tw.pendingConnection = nil","sourceCodeStart":863,"sourceCodeEnd":899,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/websockets/websockets.go#L863-L899","documentation":"Thrown by close() on k6/experimental/websockets sockets when the close reason exceeds 123 bytes. The WHATWG WebSocket spec limits a close frame to 125 bytes, 2 of which carry the status code, leaving 123 for the reason; isValidCloseReason (internal/js/modules/k6/websockets/websockets.go:781) enforces the UTF-8 byte limit and throws the browser-identical SyntaxError message.","triggerScenarios":"ws.close(1000, longDiagnostic) where the reason's UTF-8 encoding exceeds 123 bytes. A reason of 123 characters containing multibyte characters (e.g. accented letters or CJK) also fails, because the limit counts bytes, not characters.","commonSituations":"Stuffing error messages or stack traces into the close reason; i18n scripts where the reason is built from localized text; concatenating a prefix plus a server message with no length budget.","solutions":["Truncate the reason to at most 123 UTF-8 bytes before calling close()","Count bytes with new TextEncoder().encode(reason).length when the reason may contain non-ASCII text","Drop the reason entirely — ws.close(1000) — and send long diagnostics as a regular message before closing"],"exampleFix":"// before\nws.close(1000, `closing because of error: ${err.stack}`);\n\n// after\nconst reason = `closing because of error: ${err.message}`;\nws.close(1000, reason.length > 123 ? reason.slice(0, 123) : reason);","handlingStrategy":"validation","validationCode":"const enc = new TextEncoder();\n\nfunction clampCloseReason(reason, maxBytes = 123) {\n  let r = String(reason ?? '');\n  while (enc.encode(r).length > maxBytes) r = r.slice(0, -1);\n  return r;\n}\n\nws.close(1000, clampCloseReason(longDiagnostic));","typeGuard":null,"tryCatchPattern":"try {\n  ws.close(1000, reason);\n} catch (err) {\n  if (/must not be greater than 123 bytes/.test(String(err))) {\n    ws.close(1000, clampCloseReason(reason));\n  } else {\n    throw err;\n  }\n}","preventionTips":["Budget close reasons to 123 UTF-8 bytes, not 123 characters","Send long diagnostics as a normal message before close(), not in the reason","Validate the close code too: 1000 or 3000-4999, or k6 throws InvalidAccessError"],"tags":["websocket","close","reason-length","syntax-error","validation"],"backgroundTag":"websocket-close-reason-too-long","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}