chenhg5/cc-connect · error

subscribe: %w

Error message

subscribe: %w

What it means

After dialing, runConnection sends a subscribe frame containing bot_id and secret to register the bot on the WebSocket; this error wraps a failure writing that frame to the socket. writeJSON fails when the connection is already broken (write to closed conn, broken pipe) or serialization fails.

Source

Thrown at platform/wecom/websocket.go:238

		})
		for _, k := range staleKeys {
			p.pendingAcks.Delete(k)
		}
	}()

	// Send subscribe (auth) frame
	// Format: { cmd: "aibot_subscribe", headers: { req_id }, body: { bot_id, secret } }
	subReqID := p.generateReqID("aibot_subscribe")
	subFrame := map[string]any{
		"cmd":     "aibot_subscribe",
		"headers": map[string]string{"req_id": subReqID},
		"body": map[string]string{
			"bot_id": p.botID,
			"secret": p.secret,
		},
	}
	if err := p.writeJSON(subFrame); err != nil {
		return fmt.Errorf("subscribe: %w", err)
	}

	// Read subscribe response: { headers: { req_id }, errcode: 0, errmsg: "ok" }
	var subResp wsFrame
	if err := conn.ReadJSON(&subResp); err != nil {
		return fmt.Errorf("subscribe response: %w", err)
	}
	if subResp.ErrCode == nil || *subResp.ErrCode != 0 {
		errCode := 0
		if subResp.ErrCode != nil {
			errCode = *subResp.ErrCode
		}
		return fmt.Errorf("subscribe failed: errcode=%d errmsg=%s", errCode, subResp.ErrMsg)
	}
	slog.Info("wecom-ws: subscribed successfully", "bot_id", p.botID)
	p.missedPong.Store(0)

	// Start heartbeat goroutine

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Check the wrapped cause: broken pipe/reset means the server dropped the socket — verify bot_id/secret are valid so the server doesn't reject immediately.
  2. Re-check connectivity and firewall rules to the wss endpoint.
  3. If 'context canceled', ignore — the platform is shutting down.
  4. Let connectLoop retry; if it recurs every attempt, capture a packet trace or check WeCom service status.

Example fix

null
Defensive patterns

Strategy: retry

Validate before calling

if err := p.conn.Ping(); err != nil { reconnect() } // socket liveness check before writing

Type guard

null

Try / catch

if err := platform.Start(ctx); err != nil {
	if errors.Is(err, context.Canceled) { return nil }
	retryWithBackoff(err)
}

Prevention

When it happens

Trigger: The underlying conn write fails right after dial — server closed the connection immediately, TLS/network issue between dial and subscribe, or the context was canceled mid-write.

Common situations: WeCom gateway rejecting/closing connections from blocked IPs; very unstable network where the socket dies within milliseconds; shutdown racing with connect (p.ctx canceled during write).

Related errors


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