Skip to content

Commit

Permalink
fix(query): always close client.conn in cancelQuery (issue ClickHouse…
Browse files Browse the repository at this point in the history
…#405)

When the server closes the connection unexpectedly, the client
will call cancelQuery (e.g. when client.packet fails). In
client.cancelQuery, if client.flushBuf has data to flush, it will
return a non-nil error and return early without calling conn.Close.
This prevents the chpool from removing the client after client.Do
and leaves the client.conn in a bad state such that future writes will
always fail with a "broken pipe" error.
  • Loading branch information
charredlot committed Jun 26, 2024
1 parent d4dca7a commit b916fde
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ func (c *Client) cancelQuery() error {
Buf: make([]byte, 1),
}
proto.ClientCodeCancel.Encode(&b)

var retErr error
if err := c.flushBuf(ctx, &b); err != nil {
return errors.Wrap(err, "flush")
retErr = errors.Join(retErr, errors.Wrap(err, "flush"))
}

// Closing connection to prevent further queries.
// Always close connection to prevent further queries.
if err := c.Close(); err != nil {
return errors.Wrap(err, "close")
retErr = errors.Join(retErr, errors.Wrap(err, "close"))
}

return nil
return retErr
}

func (c *Client) querySettings(q Query) []proto.Setting {
Expand Down

0 comments on commit b916fde

Please sign in to comment.