From b3739c1289f345f325f163dfea603d8d9d963a3f Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 26 May 2023 06:03:25 -0500 Subject: [PATCH] pgconn.CheckConn locks connection This ensures that a closed connection at the pgconn layer is not considered okay when the background closing of the net.Conn is still in progress. This also means that CheckConn cannot be called when the connection is locked (for example, by in an progress query). But that seems reasonable. It's not exactly clear that that would have ever worked anyway. https://github.com/jackc/pgx/issues/1618#issuecomment-1563702231 --- pgconn/pgconn.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pgconn/pgconn.go b/pgconn/pgconn.go index ca4e88ed..2e07344d 100644 --- a/pgconn/pgconn.go +++ b/pgconn/pgconn.go @@ -1644,6 +1644,11 @@ func (pgConn *PgConn) EscapeString(s string) (string, error) { // connection. If this is done immediately before sending a query it reduces the chances a query will be sent that fails // without the client knowing whether the server received it or not. func (pgConn *PgConn) CheckConn() error { + if err := pgConn.lock(); err != nil { + return err + } + defer pgConn.unlock() + err := pgConn.conn.BufferReadUntilBlock() if err != nil && !errors.Is(err, nbconn.ErrWouldBlock) { return err