googleapis/mcp-toolbox · error

failed to connect to CockroachDB after %d retries: %w

Error message

failed to connect to CockroachDB after %d retries: %w

What it means

This error is returned by initCockroachDBConnectionPoolWithRetry when the toolbox fails to establish a connection pool to CockroachDB after exhausting maxRetries attempts with exponential backoff (baseDelay * 2^attempt). The wrapped error (%w) is the last underlying connection failure, typically a dial timeout, bad DSN, or unreachable host. It surfaces during source Initialize, so the server cannot start the CockroachDB source.

Source

Thrown at internal/sources/cockroachdb/cockroachdb.go:516

	var pool *pgxpool.Pool
	for attempt := 0; attempt <= maxRetries; attempt++ {
		pool, err = pgxpool.New(ctx, connURL.String())
		if err == nil {
			err = pool.Ping(ctx)
		}

		if err == nil {
			return pool, nil
		}

		if attempt < maxRetries {
			backoff := baseDelay * time.Duration(math.Pow(2, float64(attempt)))
			time.Sleep(backoff)
		}
	}

	return nil, fmt.Errorf("failed to connect to CockroachDB after %d retries: %w", maxRetries, err)
}

func ConvertParamMapToRawQuery(queryParams map[string]string) string {
	values := url.Values{}
	for k, v := range queryParams {
		values.Add(k, v)
	}
	return values.Encode()
}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Verify the CockroachDB host, port, database, and credentials in the source config and test them with cockroach sql or psql (CockroachDB is wire-compatible)
  2. Check network reachability: ping/curl the cluster host and confirm firewall/VPC rules allow outbound TCP to port 26257
  3. Confirm the cluster is running (CockroachCloud console) and not in maintenance; if it is transient, simply restarting the toolbox after the cluster recovers resolves it
  4. If using TLS, verify certificate paths and CA trust; try sslmode=disable locally to isolate TLS issues

Example fix

// before (config)
kind: source
name: crdb-src
type: cockroachdb
host: crdb-internal.example.com // unreachable internal host
port: "26257"
// after
kind: source
name: crdb-src
type: cockroachdb
host: your-cluster.g8.cockroachlabs.cloud
port: "26257"
Defensive patterns

Strategy: validation

Validate before calling

// Before starting the toolbox, verify CockroachDB connectivity
package main

import (
	"context"
	"database/sql"
	"fmt"
	"time"

	_ "github.com/jackc/pgx/v5/stdlib"
)

func checkCockroach(dsn string) error {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	db, err := sql.Open("pgx", dsn)
	if err != nil {
		return err
	}
	defer db.Close()
	return db.PingContext(ctx)
}

func main() {
	if err := checkCockroach("postgresql://user:pass@host:26257/defaultdb?sslmode=verify-full"); err != nil {
		panic(fmt.Sprintf("CockroachDB unreachable: %v", err))
	}
}

Prevention

When it happens

Trigger: Calling Initialize on the CockroachDB source when every pooled connection attempt fails: wrong host/port in the connection config, network/firewall blocking the cluster, invalid credentials, or the CockroachDB cluster being down. The retry loop sleeps backoff = baseDelay * 2^attempt between attempts and returns this error only after the final attempt.

Common situations: Typo in the CockroachDB connection string or port (default 26257), cluster paused/deleted in CockroachCloud, VPC/firewall rules blocking egress, DNS resolution failures, TLS certificate misconfiguration, or a briefly unavailable cluster during maintenance that outlasts the retry window.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/08bf8ea330b0f71c. Report an issue: GitHub.