googleapis/mcp-toolbox · error

errors encountered by results.Scan: %w

Error message

errors encountered by results.Scan: %w

What it means

Wraps any error returned by sql.Rows.Err() after all rows of a ClickHouse query have been iterated in RunSQL. It indicates the row iteration terminated abnormally — a connection drop, query cancellation, or driver-level failure encountered mid-scan — rather than a per-row scan error. The library surfaces it after processing whatever partial rows it could collect.

Source

Thrown at internal/sources/clickhouse/clickhouse.go:165

				if rawValues[i] != nil {
					// Handle potential []byte to string conversion if needed
					if b, ok := rawValues[i].([]byte); ok {
						vMap[name] = string(b)
					} else {
						vMap[name] = rawValues[i]
					}
				} else {
					vMap[name] = nil
				}
			default:
				vMap[name] = rawValues[i]
			}
		}
		out = append(out, vMap)
	}

	if err := results.Err(); err != nil {
		return nil, fmt.Errorf("errors encountered by results.Scan: %w", err)
	}

	return out, nil
}

func validateConfig(protocol string) error {
	validProtocols := map[string]bool{"http": true, "https": true}

	if protocol != "" && !validProtocols[protocol] {
		return fmt.Errorf("invalid protocol: %s, must be one of: http, https", protocol)
	}
	return nil
}

func initClickHouseConnectionPool(ctx context.Context, tracer trace.Tracer, name, host, port, user, pass, dbname, protocol string, secure bool) (*sql.DB, error) {
	//nolint:all // Reassigned ctx
	ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceType, name)
	defer span.End()

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Re-run the query; transient connection drops usually resolve on retry
  2. Check ClickHouse server logs for query errors or restarts during execution
  3. Verify network stability between toolbox and ClickHouse, especially proxy/load-balancer idle timeouts
  4. Reduce result set size (LIMIT, pagination) to shorten streaming time
  5. Ensure the parent context is not cancelled while the query runs

Example fix

// before
out, err := source.RunSQL(ctx, "SELECT * FROM huge_table")
// after
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
out, err := source.RunSQL(ctx, "SELECT id, ts, value FROM huge_table WHERE ts > now() - INTERVAL 1 DAY")
Defensive patterns

Strategy: retry

Validate before calling

// pre-check connectivity
import "database/sql"
func pingCH(db *sql.DB) error { return db.PingContext(context.Background()) }

Try / catch

// Go: inspect wrapped error and retry transient failures
for i := 0; i < 3; i++ {
    out, err := src.RunSQL(ctx, q)
    if err == nil { break }
    var netErr net.Error
    if errors.As(err, &netErr) || errors.Is(err, context.Canceled) {
        time.Sleep(time.Duration(i+1) * time.Second)
        continue
    }
    return err
}

Prevention

When it happens

Trigger: Calling run_sql against a ClickHouse instance where the HTTP/HTTPS connection drops mid-result-stream, the context is cancelled while rows are being consumed, or the clickhouse driver returns an error after the last row.

Common situations: Long-running analytical queries timing out, ClickHouse server restarting under load, network proxies killing idle HTTP keep-alive connections during large result sets.

Related errors


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