chenhg5/cc-connect · error

wecom-ws: not connected

Error message

wecom-ws: not connected

What it means

Guard in writeJSON: the WebSocket connection field p.conn is nil while a frame is being sent, meaning the WeCom AI-Bot socket is not currently established — either the initial handshake has not completed yet, a reconnect is in progress, or Stop() has torn the connection down. It is a state guard, not a network failure: the write never reached the wire.

Source

Thrown at platform/wecom/websocket.go:567

func (p *WSPlatform) Stop() error {
	if p.cancel != nil {
		p.cancel()
	}
	p.mu.Lock()
	conn := p.conn
	p.mu.Unlock()
	if conn != nil {
		return conn.Close()
	}
	return nil
}

// writeJSON sends a JSON message over the WebSocket connection with mutex protection.
func (p *WSPlatform) writeJSON(v any) error {
	p.mu.Lock()
	defer p.mu.Unlock()
	if p.conn == nil {
		return fmt.Errorf("wecom-ws: not connected")
	}
	return p.conn.WriteJSON(v)
}

// writeAndWaitAck sends a frame and waits for the server ack before returning.
// Falls back to non-blocking on timeout to avoid deadlocks.
func (p *WSPlatform) writeAndWaitAck(ctx context.Context, frame map[string]any, reqID string) error {
	return p.writeAndWaitAckWithTimeout(ctx, frame, reqID, wsAckTimeout)
}

func (p *WSPlatform) writeAndWaitAckWithTimeout(ctx context.Context, frame map[string]any, reqID string, timeout time.Duration) error {
	result, err := p.writeAndWaitResult(ctx, frame, reqID, timeout)
	if errors.Is(err, errWSAckTimeout) {
		slog.Debug("wecom-ws: ack timeout, proceeding", "req_id", reqID)
		return nil
	}
	if err != nil {
		return err

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Have callers (Reply, heartbeat, writeAndWaitResult) wait for the connection-ready signal from runConnection before sending
  2. Buffer or re-queue outgoing frames so they flush after reconnection instead of being dropped
  3. Ensure Stop() drains pending sends before nil-ing conn
  4. Surface the error so the engine can retry the message once the socket is back
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at platform/wecom/websocket.go:567 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/3557298f979cf12b. Report an issue: GitHub.