From fc9c75f44b61ce5fa87a2157f2e4f9ac9b3ce85c Mon Sep 17 00:00:00 2001 From: Pavel Skuratovich Date: Wed, 18 Jan 2023 00:02:51 +0300 Subject: [PATCH] Try to ensure both ends of a connection are correctly closed at the end --- main.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 08ae39f..aa3cc7d 100644 --- a/main.go +++ b/main.go @@ -185,11 +185,13 @@ func proxy(local io.ReadWriteCloser, remoteAddr *net.TCPAddr) { go pipe(remote, local) } -func pipe(r io.Reader, w io.WriteCloser) { - atomic.AddUint32(&globalStats.pipesActive, 1) +func pipe(r io.ReadCloser, w io.WriteCloser) { + atomic.AddUint32(&globalStats.pipesActive, 1) // increase by 1 + defer atomic.AddUint32(&globalStats.pipesActive, ^uint32(0)) // decrease by 1 + + defer r.Close() + defer w.Close() io.Copy(w, r) - w.Close() - atomic.AddUint32(&globalStats.pipesActive, ^uint32(0)) } func getMasterAddr(port string, timeout int) *net.TCPAddr {