ch.Dial: the TCP connection stays open when the handshake fails (for example a wrong password)
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
- Domain
- backend, networking
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
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 failedch.Dialcalls add 100 open file descriptors; 50 failedPool.Acquirecalls add 50. - With GC on: 5000 failed
ch.Dialcalls had at most 24 open descriptors at a time. The server ends its side after the exception, so these sockets wait inCLOSE_WAIT.
A handshake timeout does not have the problem: the watchdog in handshake closes the connection.
Steps to reproduce
- Use a server where the user
defaulthas a password other thanwrong. - 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:
mainat 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ClickHouse/ch-go
-
proto: ColTime32 has the type name "Time32", which the server does not know, so a Time column cannot be written or readPossibly taken @shafiktanbir claimed this 7 days ago. Openbug data-types native-format
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
ClickHouse/ch-go#1250 ·
Maintainers usually reply within 1 day
-
proto: Time64(4), (5), (7) and (8) values are cut to milliseconds on write and readPossibly taken @AmanBoronbaev claimed this 8 days ago. Openbug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/ch-go#1248 ·
Maintainers usually reply within 1 day
-
compress: block buffers get re-zeroed on every call, and the zeroing is immediately overwrittenPossibly taken @deepanshu75 claimed this 11 days ago. Openconnectivity enhancement native-protocol performance
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/ch-go#1218 · 1 comment ·
Maintainers usually reply within 1 day
-
ch.Parameters does not escape quotes/backslashes in string valuesPossibly taken @Laotree claimed this 85 days ago. Openbug native-protocol queries
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/ch-go#1198 · 1 comment ·
Maintainers usually reply within 1 day
-
compress: a stream that ends right after a block header reads as io.EOF, the same as a clean endPossibly taken @AmanBoronbaev claimed this 1 day ago. Openbug connectivity native-protocol
Difficulty 3/5 1-2 days Newbie friendliness 69/100
ClickHouse/ch-go#1256 ·
Maintainers usually reply within 1 day
All issues in ClickHouse/ch-go
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
seriousm4x/UpSnap#1786 ·
-
bug feedback ui/ux
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
peasant-labs/peasant#1307 ·
Maintainers usually reply within 1 day
-
Reformat test names for single-protocol tests to start with Protocol/, i.e. gRPC/ or Websocket/Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
connectrpc/conformance#1211 ·
Maintainers usually reply within 1 day
-
bug effort:s impact:med raised-by:desk risk:low
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
medici-finance/assay#8342 · 1 comment ·
Maintainers usually reply within 1 day
-
hardening
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
inference-sim/inference-sim#2821 ·
Maintainers usually reply within 1 day