From 3db7d1774e338277d4a6e20228c3620d7aa4fa43 Mon Sep 17 00:00:00 2001 From: Dmitry K Date: Sat, 18 Mar 2023 22:17:08 +0300 Subject: [PATCH] Set socket to non-blocking mode before `doneChan` is allocated to avoid that channel leaked in case when `SetBlockingMode` will return error --- copy_from.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/copy_from.go b/copy_from.go index 37351848..e87bb94a 100644 --- a/copy_from.go +++ b/copy_from.go @@ -132,9 +132,6 @@ func (ct *copyFrom) run(ctx context.Context) (int64, error) { return 0, fmt.Errorf("unknown QueryExecMode: %v", ct.mode) } - r, w := io.Pipe() - doneChan := make(chan struct{}) - if realNbConn, ok := ct.conn.pgConn.Conn().(*nbconn.NetConn); ok { if err := realNbConn.SetBlockingMode(false); err != nil { return 0, fmt.Errorf("cannot set socket non-blocking mode: %w", err) @@ -146,6 +143,9 @@ func (ct *copyFrom) run(ctx context.Context) (int64, error) { }() } + r, w := io.Pipe() + doneChan := make(chan struct{}) + go func() { defer close(doneChan)