From 060038c29b99249090b5b257fc954ea4346e5955 Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 12 Dec 2025 22:14:13 -0500 Subject: [PATCH] Try to fix mem leak --- proxy/proxy.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 4d27bc4..f29878e 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -758,14 +758,20 @@ func (p *SNIProxy) pipe(clientConn, targetConn net.Conn, clientReader io.Reader) var wg sync.WaitGroup wg.Add(2) + // closeOnce ensures we only close connections once + var closeOnce sync.Once + closeConns := func() { + closeOnce.Do(func() { + // Close both connections to unblock any pending reads + clientConn.Close() + targetConn.Close() + }) + } + // Copy data from client to target (using the buffered reader) go func() { defer wg.Done() - defer func() { - if tcpConn, ok := targetConn.(*net.TCPConn); ok { - tcpConn.CloseWrite() - } - }() + defer closeConns() // Use a large buffer for better performance buf := make([]byte, 32*1024) @@ -778,11 +784,7 @@ func (p *SNIProxy) pipe(clientConn, targetConn net.Conn, clientReader io.Reader) // Copy data from target to client go func() { defer wg.Done() - defer func() { - if tcpConn, ok := clientConn.(*net.TCPConn); ok { - tcpConn.CloseWrite() - } - }() + defer closeConns() // Use a large buffer for better performance buf := make([]byte, 32*1024)