grafana/k6 · error

connection closed with websocket code

Error message

connection closed with websocket code: %d

What it means

Connection.close uses this error as the context-cancellation reason after the websocket to the browser is closed with the given close code. It propagates through cancelCtx so pending CDP calls fail fast instead of hanging; it is the standard teardown signal for the browser websocket connection.

Solutions

  1. Treat as expected if it appears during browser shutdown; avoid using the browser/connection after close
  2. Check the websocket close code for abnormal reasons (e.g. 1006) indicating a crash of the browser process
  3. Ensure browser.close() is called and not raced with active operations
Defensive patterns

Strategy: fallback

When it happens

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

Common situations: See trigger scenarios.

Understand the failure class


AI-assisted analysis of grafana/k6@94169daab2 (2026-08-18). Data as JSON: /api/errors/00c7d1bf10e33547. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/connection.go:204

		errorCh:                  make(chan error),
		done:                     make(chan struct{}),
		closing:                  make(chan struct{}),
		msgIDGen:                 &msgID{},
		sessions:                 make(map[target.SessionID]*Session),
		onTargetAttachedToTarget: onTargetAttachedToTarget,
	}

	go c.recvLoop()
	go c.sendLoop()

	return &c, nil
}

func (c *Connection) close(code int) error {
	c.logger.Debugf("Connection:close", "code:%d", code)

	defer func() {
		c.cancelCtx(fmt.Errorf("connection closed with websocket code: %d", code))
	}()

	var err error
	c.shutdownOnce.Do(func() {
		defer func() {
			// Stop the main control loop
			close(c.done)
			_ = c.conn.Close()
		}()

		c.closeAllSessions()

		err = c.conn.WriteControl(websocket.CloseMessage,
			websocket.FormatCloseMessage(code, ""),
			time.Now().Add(time.Second),
		)

		// According to the WS RFC[1], we might want to wait for a response

View on GitHub (pinned to 94169daab2)