From a8c868a99b3834be083c0fcc6ded0f3d21dcb060 Mon Sep 17 00:00:00 2001 From: Nino Kodabande Date: Fri, 9 Aug 2024 11:18:05 -0700 Subject: [PATCH] Ensure proper closure of upstream connection in Pipe function - Added explicit error handling for closing the upstream connection after data is copied. - Ensured that the upstream connection is closed properly in both the goroutine and the main function. - Improved logging for connection closure errors. Signed-off-by: Nino Kodabande --- pkg/utils/pipe.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/utils/pipe.go b/pkg/utils/pipe.go index 43eafd3cd4f..4f504451613 100644 --- a/pkg/utils/pipe.go +++ b/pkg/utils/pipe.go @@ -27,15 +27,19 @@ func Pipe(conn net.Conn, upstreamAddr string) { logrus.Errorf("Failed to dial upstream %s: %s", upstreamAddr, err) return } - defer upstream.Close() - go func() { if _, err := io.Copy(upstream, conn); err != nil { logrus.Debugf("Error copying to upstream: %s", err) } + if err = upstream.Close(); err != nil { + logrus.Debugf("error closing connection while writing to upstream: %s", err) + } }() if _, err := io.Copy(conn, upstream); err != nil { logrus.Debugf("Error copying from upstream: %s", err) } + if err = upstream.Close(); err != nil { + logrus.Debugf("error closing connection: %s", err) + } }