grafana/k6 · error · SyntaxError

SyntaxError: Failed to execute 'close' on 'WebSocket': The…

Error message

SyntaxError: Failed to execute 'close' on 'WebSocket': The message must not be greater than 123 bytes

What it means

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.

Solutions

  1. Truncate the reason to at most 123 UTF-8 bytes before calling close()
  2. Count bytes with new TextEncoder().encode(reason).length when the reason may contain non-ASCII text
  3. Drop the reason entirely — ws.close(1000) — and send long diagnostics as a regular message before closing

Example fix

// before
ws.close(1000, `closing because of error: ${err.stack}`);

// after
const reason = `closing because of error: ${err.message}`;
ws.close(1000, reason.length > 123 ? reason.slice(0, 123) : reason);
Defensive patterns

Strategy: validation

Validate before calling

const enc = new TextEncoder();

function clampCloseReason(reason, maxBytes = 123) {
  let r = String(reason ?? '');
  while (enc.encode(r).length > maxBytes) r = r.slice(0, -1);
  return r;
}

ws.close(1000, clampCloseReason(longDiagnostic));

Try / catch

try {
  ws.close(1000, reason);
} catch (err) {
  if (/must not be greater than 123 bytes/.test(String(err))) {
    ws.close(1000, clampCloseReason(reason));
  } else {
    throw err;
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/6fe10af870762d35. Report an issue: GitHub.

Appendix: source

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

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 code != 0 && !isValidClientCloseCode(code) {
		return fmt.Errorf(
			"InvalidAccessError: Failed to execute 'close' on 'WebSocket': "+
				"The close code must be either 1000, or between 3000 and 4999. %d is neither",
			code,
		)
	}

	if !isValidCloseReason(reason) {
		return errors.New(
			`SyntaxError: Failed to execute 'close' on 'WebSocket': The message must not be greater than 123 bytes`,
		)
	}
	if w.readyState == CLOSED || w.readyState == CLOSING {
		return nil
	}
	if code == 0 {
		code = websocket.CloseNormalClosure
	}
	w.closeCode = code

	wasConnecting := w.readyState == CONNECTING
	w.readyState = CLOSING
	if wasConnecting {
		w.connectionMu.Lock()
		w.connectCancel()
		pendingConnection := w.pendingConnection
		w.pendingConnection = nil

View on GitHub (pinned to 3fcf5388d7)