Skip to content

PgConn.CheckConn() does not detect broken connections after v5.4.0 #1710

Open
@chacmool

Description

@chacmool

Describe the bug

We have detected that one of our workloads with pgx v5.4.1 does not handle well database node restarts.
When a node on our Cockroachdb cluster is restarted, first queries on that workload fails with "write: broken pipe".
We have another workload with pgx v5.3.0 using the same database cluster and we don't see these kinds of errors.

We have debugged the issue and we have found that the problem is that PgConn.CheckConn() does not detect a broken connections after v5.4.0.

Could this commit be related: 3ea2f57

To Reproduce

It is easy to reproduce it locally with this example:

package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"os"

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

func main() {

	db, err := sql.Open("pgx", "postgres://root@localhost:26257/defaultdb?sslmode=disable")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
		os.Exit(1)
	}
	defer db.Close()

	http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
		var greeting string
		err = db.QueryRow("select 'It works!'").Scan(&greeting)
		if err != nil {
			fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
			fmt.Fprintf(w, "QueryRow failed: %v\n", err)
		}
		fmt.Println(greeting)
		fmt.Fprintf(w, greeting)
	})

	fmt.Printf("Starting server at port 8080\n")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}

}
1. Start a cockroachdb cluster `/usr/local/bin/cockroach start-single-node --insecure --listen-addr=0.0.0.0:26257 --http-addr=:9008`
2. Run the go example
3. Check that http://localhost:8080/hi responds correctly
4. Kill and start again the database cluster `CTLR+C and /usr/local/bin/cockroach start-single-node --insecure --listen-addr=`
5. Refresh the http://localhost:8080/hi page
6a. If the go example uses pgx v5.3.0, it works as before
6b. If the go example uses pgx v5.4.0, "unexpected EOF" error is shown

Expected behavior

Retry a query if the connection used is not longer valid.

Actual behavior

The query fails if the connection used is not longer valid.

Version

  • Cockroachdb: 23.1
  • pgx: v5.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions