ch.Dial: the TCP connection stays open when the handshake fails (for example a wrong password)

Open Beginner friendly
#1,264 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
clickhouse, go

Research direction

Read client.go around DialWithBuffer (lines 619–621) and ConnectWithBuffer (lines 541–542), where the issue reports that handshake errors return without closing the connection. Trace the error path from ch.Dial and check existing connection cleanup tests. Done when a failed handshake closes the opened connection, as verified by the reproduction reporting connection closed: true.

Written by the indexing model from the issue text.

Description

bug connectivity
Describe the bug

When ch.Dial opens the TCP connection but the handshake then fails (for example a wrong password or an unknown database), Dial returns the error and leaves the connection open. The caller gets no *ch.Client, so it cannot close it. The socket stays open until the Go garbage collector runs its finalizer. chpool dials with ch.Dial, so each failed Pool.Acquire leaves one more socket open.

The same defect was reported for clickhouse-go in https://github.com/ClickHouse/clickhouse-go/issues/2202.

Measured on the server below:

  • With GC off (debug.SetGCPercent(-1)): 100 failed ch.Dial calls add 100 open file descriptors; 50 failed Pool.Acquire calls add 50.
  • With GC on: 5000 failed ch.Dial calls had at most 24 open descriptors at a time. The server ends its side after the exception, so these sockets wait in CLOSE_WAIT.

A handshake timeout does not have the problem: the watchdog in handshake closes the connection.

Steps to reproduce
  1. Use a server where the user default has a password other than wrong.
  2. Run the program below with CH_ADDR=<host>:9000.
Expected behaviour

When Dial returns an error, the connection that it opened is closed: the program prints connection closed: true.

Code example
Reproduction program (Go, 34 lines)
package main

import (
	"context"
	"errors"
	"fmt"
	"net"
	"os"

	"github.com/ClickHouse/ch-go"
	"github.com/ClickHouse/ch-go/proto"
)

type recordDialer struct{ conn net.Conn }

func (d *recordDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
	c, err := (&net.Dialer{}).DialContext(ctx, network, addr)
	d.conn = c
	return c, err
}

func main() {
	d := &recordDialer{}
	_, err := ch.Dial(context.Background(), ch.Options{
		Address:  os.Getenv("CH_ADDR"), // for example 127.0.0.1:9000
		User:     "default",
		Password: "wrong",
		Dialer:   d,
	})
	fmt.Println("authentication failed:", ch.IsErr(err, proto.ErrAuthenticationFailed))

	_, err = d.conn.Write([]byte{0})
	fmt.Println("connection closed:", errors.Is(err, net.ErrClosed))
}
Error log
authentication failed: true
connection closed: false

With Database: "no_such_db" in place of the wrong password, Dial fails with UNKNOWN_DATABASE (81), and the connection also stays open.

Cause

DialWithBuffer returns the error of ConnectWithBuffer without closing conn (client.go:619-621). ConnectWithBuffer does not close it either when c.handshake fails (client.go:541-542).

The fix is one conn.Close() on that error return in DialWithBuffer. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Configuration
Environment
  • Client version: main at d9ee203 (v0.76.0-35), same code in v0.75.0 and v0.76.0
  • Language version: Go 1.26.11
  • OS: Linux (amd64)
ClickHouse server
  • ClickHouse Server version: 26.11.7.10
  • ClickHouse Server non-default settings, if any: none

Classification: impact: none · area: Connectivity (connect and handshake)


Found by automated analysis (Polyglot AI) while checking clickhouse-go#2202 against the other client repos. Verified against a live server.

Dominant language
Go
Stars
435
Forks
101
Avg merge
22h 1m
Merged PRs (30d)
15

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ClickHouse/ch-go

All issues in ClickHouse/ch-go

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.