[tor-commits] [snowflake/master] Let copyLoop exit when either direction finishes.

dcf at torproject.org dcf at torproject.org
Thu Apr 23 22:43:24 UTC 2020


commit 904af9cb8aa6aa25e094da1c025c7afed55d46ea
Author: David Fifield <david at bamsoftware.com>
Date:   Fri Feb 21 14:47:34 2020 -0700

    Let copyLoop exit when either direction finishes.
    
    Formerly we waiting until *both* directions finished. What this meant in
    practice is that when the remote connection ended, copyLoop would become
    useless but would continue blocking its caller until something else
    finally closed the socks connection.
---
 client/lib/snowflake.go | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/client/lib/snowflake.go b/client/lib/snowflake.go
index 199f8a4..409ce14 100644
--- a/client/lib/snowflake.go
+++ b/client/lib/snowflake.go
@@ -5,7 +5,6 @@ import (
 	"io"
 	"log"
 	"net"
-	"sync"
 	"time"
 )
 
@@ -41,20 +40,19 @@ func Handler(socks net.Conn, snowflakes SnowflakeCollector) error {
 // Exchanges bytes between two ReadWriters.
 // (In this case, between a SOCKS and WebRTC connection.)
 func copyLoop(socks, webRTC io.ReadWriter) {
-	var wg sync.WaitGroup
-	wg.Add(2)
+	done := make(chan struct{}, 2)
 	go func() {
 		if _, err := io.Copy(socks, webRTC); err != nil {
 			log.Printf("copying WebRTC to SOCKS resulted in error: %v", err)
 		}
-		wg.Done()
+		done <- struct{}{}
 	}()
 	go func() {
 		if _, err := io.Copy(webRTC, socks); err != nil {
 			log.Printf("copying SOCKS to WebRTC resulted in error: %v", err)
 		}
-		wg.Done()
+		done <- struct{}{}
 	}()
-	wg.Wait()
+	<-done
 	log.Println("copy loop ended")
 }





More information about the tor-commits mailing list