grafana/k6 · error

sending a message to browser

Error message

sending a message to browser: %w

What it means

Connection.send aborted because the connection's error channel reported a failure (recv/send loop crash or connection teardown) before the message could be queued on sendCh. The wrapped error is the underlying connection failure; the CDP message was never delivered.

Solutions

  1. Check the wrapped error and browser logs for the underlying connection failure
  2. Avoid sending CDP commands after the browser or page is closed
  3. Restart the browser/test if the connection dropped unexpectedly (crash, timeout)
Defensive patterns

Strategy: try-catch

When it happens

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

Common situations: See trigger scenarios.


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

Appendix: source

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

		msg := &cdproto.Message{
			ID:     c.msgIDGen.newID(),
			Method: cdproto.MethodType(target.CommandDetachFromTarget),
			Params: buf,
		}
		if err := c.send(c.ctx, msg, nil, nil); err != nil {
			c.logger.Errorf("Connection:detachSession", "sid:%v wsURL:%q, err:%v", session.id, c.wsURL, err)
		}
	}()
}

func (c *Connection) send(
	ctx context.Context, msg *cdproto.Message, recvCh chan *cdproto.Message, res any,
) error {
	select {
	case c.sendCh <- msg:
	case err := <-c.errorCh:
		c.logger.Debugf("Connection:send:<-c.errorCh", "wsURL:%q sid:%v, err:%v", c.wsURL, msg.SessionID, err)
		return fmt.Errorf("sending a message to browser: %w", err)
	case code := <-c.closeCh:
		c.logger.Debugf("Connection:send:<-c.closeCh", "wsURL:%q sid:%v, websocket code:%v", c.wsURL, msg.SessionID, code)
		_ = c.close(code)
		return fmt.Errorf("closing communication with browser: %w", &websocket.CloseError{Code: code})
	case <-ctx.Done():
		c.logger.Debugf("Connection:send:<-ctx.Done", "wsURL:%q sid:%v err:%v", c.wsURL, msg.SessionID, ContextErr(ctx))
		return nil
	case <-c.done:
		c.logger.Debugf("Connection:send:<-c.done", "wsURL:%q sid:%v", c.wsURL, msg.SessionID)
		return nil
	}

	// Block waiting for response.
	if recvCh == nil {
		return nil
	}
	tid := c.findTargetIDForLog(msg.SessionID)
	select {

View on GitHub (pinned to 94169daab2)