lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
8.89k stars 906 forks source link

write: connection reset by peer should close the connection #1028

Open Sytten opened 3 years ago

Sytten commented 3 years ago

Setup

I am using a serverless setup on GCP Cloud Run (knative) with a database in VPC accessed via a serverless connector. This is a similar to a setup where a server communicates with a database behind an HAProxy or an NGINX. The only difference is that if the server is not receiving requests, its CPU is heavily throttled.

Problem

I started receiving errors like:

write tcp 169.254.8.1:33834->10.1.0.2:14157: write: connection reset by peer

Where 10.1.0.2 is the database, that tells me that most likely the connection to the proxy was maintained but the connection to the host was killed. I would expect pq to be able to detect that, kill the connection and retry on a new one. But it seems it is not the case and the pool will happily continue using the connection even if it is effectively dead.

This is patchable with a SetConnMaxIdleTime or SetConnMaxLifetime, but I feel a network error like that should be able to detected and acted upon directly.

Lekensteyn commented 3 years ago

Possibly a duplicate of #835.

Sytten commented 3 years ago

Thanks for linking issues! @Lekensteyn The answer to it is not super great though: move to pgx :/

Lekensteyn commented 3 years ago

Hey @Sytten, can you share which pq version you are running? Is the Postgres connection secured with TLS?

1013 (>= v1.9.0) should have addressed error handling when sending commands over a closed connection.

Sytten commented 3 years ago

For some reason I am using an old version (1.7.0), unsure why goland resolved to that (most likely I already had it downloaded). I will try with a more recent version, thanks a LOT for that.

Sytten commented 3 years ago

I still have issue with the latest version, either driver: bad connection or read: connection reset by peer.