pgx icon indicating copy to clipboard operation
pgx copied to clipboard

tls: failed to send closeNotify alert (but connection was closed anyway): write tcp xxx.xxx.xxx.xxx:57192->xxx.xxx.xxx.xxx:5432: write: broken pipe

Open s-parthiban opened this issue 3 years ago • 8 comments

Hi Guys,

In our application I am using the pgx postgresql driver along with golang version 1.16.2 to update Azure Postgres DB(its support TLS 1.0, 1.1 and 1.2). The data is updated properly in the Azure postgresql DB, but the problem is while closing the DB (i.e sql.DB.Close()) I am getting the below error from the database/sql package.

tls: failed to send closeNotify alert (but connection was closed anyway): write tcp xxx.xxx.xxx.xxx:57192->xxx.xxx.xxx.xxx:5432: write: broken pipe
The pgx driver with golang version 1.15.10, I am getting a different error

write tcp x.x.x.x:56162->x.x.x.x:5432: write: broken pipe

This problem is not coming in the pq postgres driver. My queries are

Do I need to do anything related to TLS configuration in pgx driver? if so how I to do it? Is pgx incompatible with Azure Postgresql DB? Is any configuration I need to do in the Azure postgresql DB server? which TLS version the pgx postgres client is using?

Sample code

import (
        "fmt"
        "sync"
        "context"
        "database/sql"
        "github.com/cenkalti/backoff"
        _ "github.com/jackc/pgx/v4/stdlib"

        log "github.com/sirupsen/logrus"
        "os"
        "os/signal"
        "syscall"
        "time"
)

func main ()  {
        ctx, cancel := context.WithCancel(context.Background())
        var wg sync.WaitGroup
        wg.Add(1)

        go func() {
                defer wg.Done()
                DBupdateTest(ctx)
                <-ctx.Done()
                log.Info("Done signal received")
        }()
        sigs := make(chan os.Signal, 1)
        signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
        select {
        case <-sigs:
                cancel()
                log.Info("Stop Signal received")
        }
        wg.Wait()

        os.Exit(0)
}

func DBupdateTest(ctx context.Context){
        db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
        if err != nil {
                return
        }

        defer func() {
                if err := db.Close(); err != nil {
                        log.Errorf("DB close failed: %v", err)
                } else {
                        log.Info("DB successfully closed")
                }
        }()
        log.Info("set open 1 to set idle 1")
        db.SetMaxOpenConns(1)
        db.SetMaxIdleConns(1)

        err = db.Ping()
        if err != nil {
                log.Errorf("ping failed %v", err)
                return
        }
}
```

s-parthiban avatar Apr 05 '21 08:04 s-parthiban

One more finding is database/sql client with pgx driver is not sending the [FIN, ACK] packet while terminating the DB connection, It forcefully close the socket, so the Postgersql DB server sent the [RST, ACK]

s-parthiban avatar Apr 05 '21 10:04 s-parthiban

@jackc do you have any update on this issue, please

s-parthiban avatar Apr 12 '21 15:04 s-parthiban

We have the same problem

immunochomik avatar Apr 13 '21 18:04 immunochomik

Any update on this issue.

s-parthiban avatar May 07 '21 16:05 s-parthiban

Hitting the exact same error but with logstash:

error closing connection to logstash host example.com: tls: failed to send closeNotify alert (but connection was closed anyway): write tcp xxxxx:55830->xxxxxxx:5000: write: connection reset by peer, reconnecting...

mdebord-dlr avatar Dec 15 '21 21:12 mdebord-dlr

We are seeing the same error.

danj565 avatar Jul 25 '22 14:07 danj565

funny, I see same error with azure mysql too

ebuildy avatar Apr 20 '24 12:04 ebuildy

Happens with AWS Aurora as well. With Aurora MySQL too, even though it's a different driver.

Kind of makes one think it's Go TLS issue.

oakad avatar May 14 '24 08:05 oakad