chenhg5/cc-connect · error

dial: %w

Error message

dial: %w

What it means

runConnection dials the WeCom WebSocket endpoint with websocket.DefaultDialer.DialContext; this error wraps any dial failure. The connectLoop caller typically retries with backoff, so this error is an intermediate, retryable failure describing why the TCP/TLS/WebSocket handshake did not complete.

Source

Thrown at platform/wecom/websocket.go:193

		case <-time.After(backoff):
		case <-p.ctx.Done():
			return
		}

		backoff *= 2
		if backoff > wsMaxBackoff {
			backoff = wsMaxBackoff
		}
	}
}

// runConnection dials, subscribes, and processes messages until disconnection.
func (p *WSPlatform) runConnection() error {
	slog.Info("wecom-ws: connecting", "endpoint", wsEndpoint)

	conn, _, err := websocket.DefaultDialer.DialContext(p.ctx, wsEndpoint, nil)
	if err != nil {
		return fmt.Errorf("dial: %w", err)
	}

	p.mu.Lock()
	p.conn = conn
	p.mu.Unlock()

	defer func() {
		p.mu.Lock()
		p.conn = nil
		p.mu.Unlock()
		conn.Close()

		// Drain pending ACK channels so waiting goroutines are unblocked
		// and stale entries do not accumulate across reconnections.
		// Collect keys first, then delete — Range+Delete in callback is
		// not guaranteed safe by the sync.Map contract.
		var staleKeys []any
		p.pendingAcks.Range(func(key, value any) bool {

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Check basic connectivity to the WeCom websocket endpoint (DNS, firewall, proxy).
  2. Inspect the wrapped cause: 'connection refused' vs 'no such host' vs 'context canceled' point to different fixes.
  3. If it is 'context canceled', the platform was shutting down — no action needed; the connect loop should stop.
  4. Add a proxy configuration if the host requires one to reach external wss:// endpoints.
  5. Rely on connectLoop backoff for transient network blips; alert only if it never recovers.

Example fix

null
Defensive patterns

Strategy: retry

Validate before calling

conn, err := net.DialTimeout("tcp", hostPort, 5*time.Second)
if err != nil { return fmt.Errorf("endpoint unreachable: %w", err) } // pre-check before starting the platform

Type guard

null

Try / catch

if err := platform.Start(ctx); err != nil {
	if errors.Is(err, context.Canceled) { return nil } // expected during shutdown
	backoffRetry(err)
}

Prevention

When it happens

Trigger: DialContext fails due to DNS resolution failure, unreachable host, refused TCP connection, TLS handshake error, or context cancellation (p.ctx done) during the dial.

Common situations: Corporate firewall/proxy blocking wss:// traffic to the WeCom endpoint; DNS misconfiguration; machine offline or network flapping; p.ctx canceled during shutdown producing 'context canceled' wrapped here.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


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